In this article I will explain how to use EmptyDataTemplate in ASP.Net Repeater i.e. Show (display) empty data message when no records are present in the ASP.Net Repeater control using C# and VB.Net.
Like GridView, Repeater control does not have any EmptyDataTemplate and hence we need to make use of FooterTemplate and programmatically show (display) empty data message from code behind when no records are present.
HTML Markup
The HTML Markup consists of an ASP.Net Repeater control and a Button. The Repeater control contains an HTML Table Row (trEmpty) which will be shown programmatically from code behind when no records are present.
<asp:Repeater ID="rptCustomers" runat="server">
<HeaderTemplate>
<table cellspacing="0" rules="all" border="1">
<tr>
<th scope="col" style="width: 80px">
Customer Id
</th>
<th scope="col" style="width: 120px">
Customer Name
</th>
<th scope="col" style="width: 100px">
Country
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# Eval("Id") %>
</td>
<td>
<%# Eval("Name") %>
</td>
<td>
<%# Eval("Country") %>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
<tr id="trEmpty" runat="server" visible="false">
<td colspan = "3" align = "center">
No records found.
</td>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
<br />
<asp:Button Text="Make Empty" runat="server" OnClick = "MakeEmpty" />
Namespaces
You will need to import the following namespace.
C#
VB.Net
Show (Display) Empty data message in Repeater control in ASP.Net
The Repeater control is populated with a DataTable with some dummy records using the BindRepeater function in the Page Load event.
The BindRepeater functions binds the DataTable to the Repeater and the checks if the DataTable has no record then it finds the HTML Table Row (trEmpty) and makes it Visible True.
For illustration purposes, I have added a Button which binds an empty DataTable to the Repeater control.
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");
this.BindRepeater(dt);
}
}
private void BindRepeater(DataTable dt)
{
rptCustomers.DataSource = dt;
rptCustomers.DataBind();
if (dt.Rows.Count == 0)
{
Control FooterTemplate = rptCustomers.Controls[rptCustomers.Controls.Count - 1].Controls[0];
FooterTemplate.FindControl("trEmpty").Visible = true;
}
}
protected void MakeEmpty(object sender, EventArgs e)
{
DataTable dt = new DataTable();
this.BindRepeater(dt);
}
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")
Me.BindRepeater(dt)
End If
End Sub
Private Sub BindRepeater(dt As DataTable)
rptCustomers.DataSource = dt
rptCustomers.DataBind()
If dt.Rows.Count = 0 Then
Dim FooterTemplate As Control = rptCustomers.Controls(rptCustomers.Controls.Count - 1).Controls(0)
FooterTemplate.FindControl("trEmpty").Visible = True
End If
End Sub
Protected Sub MakeEmpty(sender As Object, e As EventArgs)
Dim dt As New DataTable()
Me.BindRepeater(dt)
End Sub
Screenshots
Repeater control displaying data
Repeater control displaying empty data message
Demo
Downloads