The GridView
has an EmptyDataText property or the <EmptyDataTemplate> that lets us
handle EmptyData. However the Repeater has no such property or template. In
this short article, we will see how to adopt a simple technique to handle empty
data in an ASP.NET Repeater control without creating a custom control.
Drag and
drop a Repeater and a SQLDataSource control to the page. Bind the Repeater to
the SQLDataSource as you usually do
<div>
<asp:Repeater ID="Repeater1"
runat="server"
DataSourceID="SqlDataSource1"
onitemdatabound="Repeater1_ItemDataBound">
<HeaderTemplate>
<table border="1" cellpadding="3"
cellspacing="3">
<tr bgcolor="blue">
<td><b>CustomerID</b>
</td>
<td><b>CompanyName</b>
</td>
<td><b>ContactName</b>
</td>
<td><b>ContactTitle</b></td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%#DataBinder.Eval(Container.DataItem, "CustomerID")%>
</td>
<td>
<%#DataBinder.Eval(Container.DataItem, "CompanyName")%>
</td>
<td>
<%#DataBinder.Eval(Container.DataItem, "ContactName")%>
</td>
<td>
<%#DataBinder.Eval(Container.DataItem, "ContactTitle")%>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1"
runat="server"
ConnectionString="<%$
ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT
[CustomerID], [CompanyName], [ContactName],
[ContactTitle], [Address] FROM [Customers] " >
</asp:SqlDataSource>
</div>
In your web.config, add a connection string as
shown below:
<connectionStrings>
<add name="NorthwindConnectionString" connectionString="Data Source =(local);Integrated Security = SSPI; Initial
Catalog=Northwind;"/>
</connectionStrings>
If you run
the application, the repeater would display the data from the database. Now
change the query so that it returns empty data.
SELECT [CustomerID],
[CompanyName], [ContactName], [ContactTitle], [Address] FROM [Customers] WHERE
CUSTOMERID=’XYZ’
On running
the application, you would see the following:
Now this
display is not elegant enough to inform the user that there is no data, right?
So let us see how to display a message in the Repeater control when there is no
data present.
In the
<FooterTemplate>, add a Label with some empty data text and set its
visible property to false.
<FooterTemplate>
<tr>
<td>
<asp:Label ID="lblEmptyData"
Text="No Data To
Display" runat="server" Visible="false">
</asp:Label>
</td>
</tr>
</table>
</FooterTemplate>
Now add an
ItemDataBound event to the Repeater
<asp:Repeater ID="Repeater1"
runat="server"
DataSourceID="SqlDataSource1"
onitemdatabound="Repeater1_ItemDataBound">
In the code
behind, write the following code:
C#
protected void
Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (Repeater1.Items.Count < 1)
{
if (e.Item.ItemType == ListItemType.Footer)
{
Label lblFooter = (Label)e.Item.FindControl("lblEmptyData");
lblFooter.Visible = true;
}
}
}
VB.NET
Protected Sub
Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As
RepeaterItemEventArgs)
If Repeater1.Items.Count < 1 Then
If e.Item.ItemType = ListItemType.Footer Then
Dim lblFooter As
Label = CType(e.Item.FindControl("lblEmptyData"),
Label)
lblFooter.Visible = True
End If
End If
End Sub
The code
checks if the Repeater has items in it. If the items count is less than 1, the
code uses the FindControl() to locate the label and set it to visible.
On running
the application again, you get to see the message 'No Data To Display' as shown
in the screenshot below. This is certainly more user friendly than letting the
user wonder why the data was not displayed at the first place.
No comments:
Post a Comment