In this article I will explain how to find and access controls and their values in GridView FooterTemplate or Footer Row.
Database
I’ll make use of Customers Table of Microsoft’s Northwind Database which you can easily download and install using the link provided below
HTML Markup
The HTML Markup consists of an ASP.Net GridView with controls defined in the FooterTemplate or Footer Row. Below the GridView there’s a Button to explain how we can access the controls and their values on Button Click event handler.
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"
runat="server" AutoGenerateColumns="false" ShowFooter = "true" OnDataBound = "OnDataBound">
<Columns>
<asp:TemplateField HeaderText = "Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtName" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText = "Country">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Country") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlCountries" runat="server">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID = "btnAdd" runat = "server" Text = "Add" OnClick = "Add" />
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Binding the GridView
I have made use of DataTable with some dummy values for this article.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Accessing and binding the DropDownList Control in the FooterTemplate (Footer Row)
I am making use of the GridView OnDataBound event to access and bind the DropDownList control inside the GridView FooterTemplate. The DropDownList is populated with Countries from the Customers Table of the Northwind Database.
C#
protected void OnDataBound(object sender, EventArgs e)
{
DropDownList ddlCountries = GridView1.FooterRow.FindControl("ddlCountries") as DropDownList;
ddlCountries.DataSource = GetData("SELECT DISTINCT Country FROM Customers");
ddlCountries.DataTextField = "Country";
ddlCountries.DataValueField = "Country";
ddlCountries.DataBind();
ddlCountries.Items.Insert(0, new ListItem("Select Country", "0"));
}
private DataTable GetData(string query)
{
string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = query;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
}
}
}
VB.Net
Protected Sub OnDataBound(sender As Object, e As EventArgs)
Dim ddlCountries As DropDownList = TryCast(GridView1.FooterRow.FindControl("ddlCountries"), DropDownList)
ddlCountries.DataSource = GetData("SELECT DISTINCT Country FROM Customers")
ddlCountries.DataTextField = "Country"
ddlCountries.DataValueField = "Country"
ddlCountries.DataBind()
ddlCountries.Items.Insert(0, New ListItem("Select Country", "0"))
End Sub
Private Function GetData(query As String) As DataTable
Dim strConnString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(strConnString)
Using cmd As New SqlCommand()
cmd.CommandText = query
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using ds As New DataSet()
Dim dt As New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Using
End Function
Accessing the Values of Controls in FooterTemplate on Button Click
On the click event handler of the Add button I am accessing values of the controls of the FooterTemplate or Footer Row.
C#
protected void Add(object sender, EventArgs e)
{
string name = (GridView1.FooterRow.FindControl("txtName") as TextBox).Text;
string country = (GridView1.FooterRow.FindControl("ddlCountries") as DropDownList).SelectedItem.Value;
}
VB.Net
Protected Sub Add(sender As Object, e As EventArgs)
Dim name As String = TryCast(GridView1.FooterRow.FindControl("txtName"), TextBox).Text
Dim country As String = TryCast(GridView1.FooterRow.FindControl("ddlCountries"), DropDownList).SelectedItem.Value
End Sub
The below screenshot displays the values of the TextBox and the DropDownList at runtime in the button click event handler.
Demo
Downloads