This should be REALLY straightforward . I have a gridview that allows
editing, deleting and inserting (from the footer row). There is a Drop
Down ListBox in the one column, which needs to ALWAYS set to a default
value (but not always the same value, so I can't set it declaritively.
It will ultimately depend on a session variable as to which default to
use).
However, the point is that the DDL in the footer (insert) row must
ALWAYS be set, even after inserts, edits & deletes.
I dont think this is possible, though. I have tried everything!!!
Here is the simplified code, using Northwind, so easy to set up:
--------------------- .aspx page -----------------------------
<%@ Page Language="C#" AutoEventWireup ="true"
CodeFile="testg rid.aspx.cs" Inherits="testg rid" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Grid View Add Update Delete</title>
</head>
<body>
<form id="form1" runat="server">
<div>
&nb sp;
<asp:GridView ID="GridView1" runat="server"
AutoGenerateCol umns="False" DataKeyNames="C ategoryID"
DataSourceID="S qlDataSource1" ShowFooter="tru e"
AllowPaging="Tr ue" AllowSorting="T rue"
OnRowCommand="G ridView1_RowCom mand">
<Columns>
<asp:CommandFie ld ShowDeleteButto n="True"
ShowEditButton= "True"/>
<asp:TemplateFi eld HeaderText="DDL B"
InsertVisible=" True">
<EditItemTempla te>
</EditItemTemplat e>
<ItemTemplate >
</ItemTemplate>
<FooterTemplate >
<asp:DropDownLi st ID="DDLB" runat="server">
<asp:ListItem Text="" Value=""></asp:ListItem>
<asp:ListItem Text="Top Menu" Value="Top
Menu"></asp:ListItem>
<asp:ListItem Text="Side Menu" Value="Side
Menu"></asp:ListItem>
</asp:DropDownLis t>
</FooterTemplate>
</asp:TemplateFie ld>
<asp:TemplateFi eld HeaderText="Cat egoryID"
InsertVisible=" False" SortExpression= "CategoryID ">
<EditItemTempla te>
<asp:Label ID="Label1" runat="server" Text='<
%# Eval("CategoryI D") %>'></asp:Label>
</EditItemTemplat e>
<ItemTemplate >
<asp:Label ID="Label1" runat="server" Text='<
%# Bind("CategoryI D") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateFie ld>
<asp:TemplateFi eld HeaderText="Cat egoryName"
SortExpression= "CategoryNa me">
<EditItemTempla te>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("CategoryN ame") %>'></asp:TextBox>
</EditItemTemplat e>
<ItemTemplate >
<asp:Label ID="Label2" runat="server" Text='<
%# Bind("CategoryN ame") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate >
<asp:TextBox ID="CategoryNam eTextBox"
Runat="server"> </asp:TextBox>
</FooterTemplate>
</asp:TemplateFie ld>
<asp:TemplateFi eld HeaderText="Des cription"
SortExpression= "Descriptio n">
<EditItemTempla te>
<asp:TextBox ID="TextBox2" runat="server"
Text='<%# Bind("Descripti on") %>'></asp:TextBox>
</EditItemTemplat e>
<ItemTemplate >
<asp:Label ID="Label3" runat="server" Text='<
%# Bind("Descripti on") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate >
<asp:TextBox ID="Description TextBox"
Runat="server"> </asp:TextBox>
</FooterTemplate>
</asp:TemplateFie ld>
<asp:templatefi eld>
<footertemplate >
<asp:linkbutt on id="btnNew"
runat="server" commandname="Ne w" text="New" />
</footertemplate>
</asp:templatefie ld>
</Columns>
</asp:GridView>
<asp:SqlDataSou rce ID="SqlDataSour ce1" runat="server"
ConnectionStrin g="Data Source=(local); Initial
Catalog=Northwi nd;Integrated Security=True"
DeleteCommand=" DELETE FROM [Categories] WHERE [CategoryID]
= @CategoryID" InsertCommand=" INSERT INTO [Categories]
([CategoryName], [Description]) VALUES (@CategoryName, @Description)"
ProviderName="S ystem.Data.SqlC lient" SelectCommand=" SELECT
[CategoryID], [CategoryName], [Description] FROM [Categories]"
UpdateCommand=" UPDATE [Categories] SET [CategoryName] =
@CategoryName, [Description] = @Description WHERE [CategoryID] =
@CategoryID">
<DeleteParamete rs>
<asp:Paramete r Name="CategoryI D" Type="Int32" />
</DeleteParameter s>
<UpdateParamete rs>
<asp:Paramete r Name="CategoryN ame" Type="String" />
<asp:Paramete r Name="Descripti on" Type="String" />
<asp:Paramete r Name="CategoryI D" Type="Int32" />
</UpdateParameter s>
<InsertParamete rs>
<asp:Paramete r Name="CategoryN ame" Type="String" />
<asp:Paramete r Name="Descripti on" Type="String" />
</InsertParameter s>
</asp:SqlDataSour ce>
</div>
</form>
</body>
</html>
--------------------- .aspx.cs page -----------------------------
using System;
using System.Collecti ons;
using System.Configur ation;
using System.Data;
using System.Web;
using System.Web.Secu rity;
using System.Web.UI;
using System.Web.UI.H tmlControls;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
using System.Data.Sql Client;
using System.Collecti ons.Generic;
public partial class testgrid : System.Web.UI.P age
{
protected void Page_Load(objec t sender, EventArgs e)
{
SetDefault();
}
protected void Page_LoadComple te(object sender, EventArgs e)
{
SetDefault();
}
protected void SetDefault()
{
DropDownList DDLB = GridView1.Foote rRow.FindContro l("DDLB") as
DropDownList;
DDLB.ClearSelec tion();
DDLB.SelectedVa lue = "Top Menu";
DDLB.DataBind() ;
}
protected void GridView1_RowCo mmand(object sender,
GridViewCommand EventArgs e)
{
SqlConnection conn = new SqlConnection(" Data
Source=(local); Initial Catalog=Northwi nd;Integrated Security=True") ;
try
{
if (e.CommandName. Equals("New"))
{
LinkButton btnNew = e.CommandSource as LinkButton;
GridViewRow row = btnNew.NamingCo ntainer as
GridViewRow;
if (row == null)
{
return;
}
TextBox txtCatName =
row.FindControl ("CategoryNameT extBox") as TextBox;
TextBox txtDescription =
row.FindControl ("DescriptionTe xtBox") as TextBox;
SqlCommand cmd = new SqlCommand(
"INSERT INTO [Categories] ([CategoryName],
[Description]) VALUES (@CategoryName, @Description)",
conn);
cmd.Parameters. AddWithValue("C ategoryName",
txtCatName.Text );
cmd.Parameters. AddWithValue("D escription",
txtDescription. Text);
conn.Open();
if (cmd.ExecuteNon Query() == 1)
{
GridView1.DataB ind();
}
}
}
catch (Exception ex)
{
}
finally
{
conn.Close();
}
}
}
editing, deleting and inserting (from the footer row). There is a Drop
Down ListBox in the one column, which needs to ALWAYS set to a default
value (but not always the same value, so I can't set it declaritively.
It will ultimately depend on a session variable as to which default to
use).
However, the point is that the DDL in the footer (insert) row must
ALWAYS be set, even after inserts, edits & deletes.
I dont think this is possible, though. I have tried everything!!!
Here is the simplified code, using Northwind, so easy to set up:
--------------------- .aspx page -----------------------------
<%@ Page Language="C#" AutoEventWireup ="true"
CodeFile="testg rid.aspx.cs" Inherits="testg rid" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Grid View Add Update Delete</title>
</head>
<body>
<form id="form1" runat="server">
<div>
&nb sp;
<asp:GridView ID="GridView1" runat="server"
AutoGenerateCol umns="False" DataKeyNames="C ategoryID"
DataSourceID="S qlDataSource1" ShowFooter="tru e"
AllowPaging="Tr ue" AllowSorting="T rue"
OnRowCommand="G ridView1_RowCom mand">
<Columns>
<asp:CommandFie ld ShowDeleteButto n="True"
ShowEditButton= "True"/>
<asp:TemplateFi eld HeaderText="DDL B"
InsertVisible=" True">
<EditItemTempla te>
</EditItemTemplat e>
<ItemTemplate >
</ItemTemplate>
<FooterTemplate >
<asp:DropDownLi st ID="DDLB" runat="server">
<asp:ListItem Text="" Value=""></asp:ListItem>
<asp:ListItem Text="Top Menu" Value="Top
Menu"></asp:ListItem>
<asp:ListItem Text="Side Menu" Value="Side
Menu"></asp:ListItem>
</asp:DropDownLis t>
</FooterTemplate>
</asp:TemplateFie ld>
<asp:TemplateFi eld HeaderText="Cat egoryID"
InsertVisible=" False" SortExpression= "CategoryID ">
<EditItemTempla te>
<asp:Label ID="Label1" runat="server" Text='<
%# Eval("CategoryI D") %>'></asp:Label>
</EditItemTemplat e>
<ItemTemplate >
<asp:Label ID="Label1" runat="server" Text='<
%# Bind("CategoryI D") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateFie ld>
<asp:TemplateFi eld HeaderText="Cat egoryName"
SortExpression= "CategoryNa me">
<EditItemTempla te>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("CategoryN ame") %>'></asp:TextBox>
</EditItemTemplat e>
<ItemTemplate >
<asp:Label ID="Label2" runat="server" Text='<
%# Bind("CategoryN ame") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate >
<asp:TextBox ID="CategoryNam eTextBox"
Runat="server"> </asp:TextBox>
</FooterTemplate>
</asp:TemplateFie ld>
<asp:TemplateFi eld HeaderText="Des cription"
SortExpression= "Descriptio n">
<EditItemTempla te>
<asp:TextBox ID="TextBox2" runat="server"
Text='<%# Bind("Descripti on") %>'></asp:TextBox>
</EditItemTemplat e>
<ItemTemplate >
<asp:Label ID="Label3" runat="server" Text='<
%# Bind("Descripti on") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate >
<asp:TextBox ID="Description TextBox"
Runat="server"> </asp:TextBox>
</FooterTemplate>
</asp:TemplateFie ld>
<asp:templatefi eld>
<footertemplate >
<asp:linkbutt on id="btnNew"
runat="server" commandname="Ne w" text="New" />
</footertemplate>
</asp:templatefie ld>
</Columns>
</asp:GridView>
<asp:SqlDataSou rce ID="SqlDataSour ce1" runat="server"
ConnectionStrin g="Data Source=(local); Initial
Catalog=Northwi nd;Integrated Security=True"
DeleteCommand=" DELETE FROM [Categories] WHERE [CategoryID]
= @CategoryID" InsertCommand=" INSERT INTO [Categories]
([CategoryName], [Description]) VALUES (@CategoryName, @Description)"
ProviderName="S ystem.Data.SqlC lient" SelectCommand=" SELECT
[CategoryID], [CategoryName], [Description] FROM [Categories]"
UpdateCommand=" UPDATE [Categories] SET [CategoryName] =
@CategoryName, [Description] = @Description WHERE [CategoryID] =
@CategoryID">
<DeleteParamete rs>
<asp:Paramete r Name="CategoryI D" Type="Int32" />
</DeleteParameter s>
<UpdateParamete rs>
<asp:Paramete r Name="CategoryN ame" Type="String" />
<asp:Paramete r Name="Descripti on" Type="String" />
<asp:Paramete r Name="CategoryI D" Type="Int32" />
</UpdateParameter s>
<InsertParamete rs>
<asp:Paramete r Name="CategoryN ame" Type="String" />
<asp:Paramete r Name="Descripti on" Type="String" />
</InsertParameter s>
</asp:SqlDataSour ce>
</div>
</form>
</body>
</html>
--------------------- .aspx.cs page -----------------------------
using System;
using System.Collecti ons;
using System.Configur ation;
using System.Data;
using System.Web;
using System.Web.Secu rity;
using System.Web.UI;
using System.Web.UI.H tmlControls;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
using System.Data.Sql Client;
using System.Collecti ons.Generic;
public partial class testgrid : System.Web.UI.P age
{
protected void Page_Load(objec t sender, EventArgs e)
{
SetDefault();
}
protected void Page_LoadComple te(object sender, EventArgs e)
{
SetDefault();
}
protected void SetDefault()
{
DropDownList DDLB = GridView1.Foote rRow.FindContro l("DDLB") as
DropDownList;
DDLB.ClearSelec tion();
DDLB.SelectedVa lue = "Top Menu";
DDLB.DataBind() ;
}
protected void GridView1_RowCo mmand(object sender,
GridViewCommand EventArgs e)
{
SqlConnection conn = new SqlConnection(" Data
Source=(local); Initial Catalog=Northwi nd;Integrated Security=True") ;
try
{
if (e.CommandName. Equals("New"))
{
LinkButton btnNew = e.CommandSource as LinkButton;
GridViewRow row = btnNew.NamingCo ntainer as
GridViewRow;
if (row == null)
{
return;
}
TextBox txtCatName =
row.FindControl ("CategoryNameT extBox") as TextBox;
TextBox txtDescription =
row.FindControl ("DescriptionTe xtBox") as TextBox;
SqlCommand cmd = new SqlCommand(
"INSERT INTO [Categories] ([CategoryName],
[Description]) VALUES (@CategoryName, @Description)",
conn);
cmd.Parameters. AddWithValue("C ategoryName",
txtCatName.Text );
cmd.Parameters. AddWithValue("D escription",
txtDescription. Text);
conn.Open();
if (cmd.ExecuteNon Query() == 1)
{
GridView1.DataB ind();
}
}
}
catch (Exception ex)
{
}
finally
{
conn.Close();
}
}
}