In this article I will explain how to find and access Value or Text of TextBox control placed in GridView FooterTemplate or Footer Row in ASP.Net
HTML Markup
The HTML Markup consists of an ASP.Net GridView with TextBox controls placed in the FooterTemplate or Footer Row. Below the GridView there’s a Button to describe, how we can access the TextBox 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">
<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:TextBox ID="txtCountry" runat="server" />
</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#
VB.Net
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 the Values of TextBox 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("txtCountry") as TextBox).Text;
}
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("txtCountry"), TextBox).Text
End Sub
The below screenshot displays the values of the TextBox controls at runtime in the button click event handler.
Demo
Downloads