In this article I will explain how to find and access controls in GridView EmptyDataTemplate (Empty Row) in ASP.Net using C# and VB.Net.
You might also like to read:
HTML Markup
The HTML Markup consists of a GridView, a Button and placed a Label control inside the GridView EmptyDataTemplate.
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
<asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
</Columns>
<EmptyDataTemplate>
<asp:Label ID = "lblEmptyMessage" Text="" runat="server" />
</EmptyDataTemplate>
</asp:GridView>
<br />
<asp:Button ID = "btnClear" Text="Clear GridView" runat="server" OnClick = "Clear" />
Namespaces
You will need to import the following namespaces
C#
VB.Net
Binding the GridView
The GridView is populated using some dummy records using DataTable.
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
Find and Access controls in GridView EmptyDataTemplate
The following event handler is executed when the Button is clicked, here I am setting the GridView DataSource to NULL so that we can see the empty data row.
Then I am searching for the Label control within the GridView EmptyDataTemplate and setting the empty message to be displayed.
C#
protected void Clear(object sender, EventArgs e)
{
GridView1.DataSource = null;
GridView1.DataBind();
Label lblEmptyMessage = GridView1.Controls[0].Controls[0].FindControl("lblEmptyMessage") as Label;
lblEmptyMessage.Text = "Currently there are no records in system.";
}
VB.Net
Protected Sub Clear(sender As Object, e As EventArgs)
GridView1.DataSource = Nothing
GridView1.DataBind()
Dim lblEmptyMessage As Label = TryCast(GridView1.Controls(0).Controls(0).FindControl("lblEmptyMessage"), Label)
lblEmptyMessage.Text = "Currently there are no records in system."
End Sub
Here Controls[0] is the Child Table within the GridView and the next Controls[0] is the GridView EmptyDataTemplate Row.
C#
Label lblEmptyMessage = GridView1.Controls[0].Controls[0].FindControl("lblEmptyMessage") as Label;
VB.Net
Dim lblEmptyMessage As Label = TryCast(GridView1.Controls(0).Controls(0).FindControl("lblEmptyMessage"), Label)
Downloads