gridview defaults in footer row

S

sean_walsh

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="testgrid.aspx.cs" Inherits="testgrid" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<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>
&nbsp;&nbsp;&nbsp;&nbsp;
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="CategoryID"
DataSourceID="SqlDataSource1" ShowFooter="true"
AllowPaging="True" AllowSorting="True"
OnRowCommand="GridView1_RowCommand">
<Columns>

<asp:CommandField ShowDeleteButton="True"
ShowEditButton="True"/>
<asp:TemplateField HeaderText="DDLB"
InsertVisible="True">
<EditItemTemplate>

</EditItemTemplate>
<ItemTemplate>

</ItemTemplate>
<FooterTemplate>
<asp:DropDownList 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:DropDownList>

</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CategoryID"
InsertVisible="False" SortExpression="CategoryID">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<
%# Eval("CategoryID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<
%# Bind("CategoryID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CategoryName"
SortExpression="CategoryName">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("CategoryName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<
%# Bind("CategoryName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="CategoryNameTextBox"
Runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description"
SortExpression="Description">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"
Text='<%# Bind("Description") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<
%# Bind("Description") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="DescriptionTextBox"
Runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:templatefield>
<footertemplate>
<asp:linkbutton id="btnNew"
runat="server" commandname="New" text="New" />
</footertemplate>
</asp:templatefield>

</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Data Source=(local);Initial
Catalog=Northwind;Integrated Security=True"
DeleteCommand="DELETE FROM [Categories] WHERE [CategoryID]
= @CategoryID" InsertCommand="INSERT INTO [Categories]
([CategoryName], [Description]) VALUES (@CategoryName, @Description)"
ProviderName="System.Data.SqlClient" SelectCommand="SELECT
[CategoryID], [CategoryName], [Description] FROM [Categories]"
UpdateCommand="UPDATE [Categories] SET [CategoryName] =
@CategoryName, [Description] = @Description WHERE [CategoryID] =
@CategoryID">
<DeleteParameters>
<asp:parameter Name="CategoryID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:parameter Name="CategoryName" Type="String" />
<asp:parameter Name="Description" Type="String" />
<asp:parameter Name="CategoryID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:parameter Name="CategoryName" Type="String" />
<asp:parameter Name="Description" Type="String" />
</InsertParameters>
</asp:SqlDataSource>

</div>
</form>
</body>
</html>


--------------------- .aspx.cs page -----------------------------
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Collections.Generic;

public partial class testgrid : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SetDefault();
}
protected void Page_LoadComplete(object sender, EventArgs e)
{
SetDefault();
}

protected void SetDefault()
{
DropDownList DDLB = GridView1.FooterRow.FindControl("DDLB") as
DropDownList;
DDLB.ClearSelection();
DDLB.SelectedValue = "Top Menu";
DDLB.DataBind();
}

protected void GridView1_RowCommand(object sender,
GridViewCommandEventArgs e)
{
SqlConnection conn = new SqlConnection("Data
Source=(local);Initial Catalog=Northwind;Integrated Security=True");
try
{
if (e.CommandName.Equals("New"))
{
LinkButton btnNew = e.CommandSource as LinkButton;
GridViewRow row = btnNew.NamingContainer as
GridViewRow;
if (row == null)
{
return;
}
TextBox txtCatName =
row.FindControl("CategoryNameTextBox") as TextBox;
TextBox txtDescription =
row.FindControl("DescriptionTextBox") as TextBox;
SqlCommand cmd = new SqlCommand(
"INSERT INTO [Categories] ([CategoryName],
[Description]) VALUES (@CategoryName, @Description)",
conn);
cmd.Parameters.AddWithValue("CategoryName",
txtCatName.Text);
cmd.Parameters.AddWithValue("Description",
txtDescription.Text);
conn.Open();
if (cmd.ExecuteNonQuery() == 1)
{
GridView1.DataBind();
}
}
}
catch (Exception ex)
{

}
finally
{
conn.Close();
}
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top