Create new account I forgot my password    

Handling Empty Data in an ASP.NET Repeater control
Rating: 14 user(s) have rated this article Average rating: 4.1
Posted by: Suprotim Agarwal, on 2/12/2009, in category "ASP.NET 2.0 & 3.5"
Views: this article has been read 25429 times
Abstract: The GridView has an EmptyDataText property or the that lets us handle EmptyData. However the Repeater has no such property or template. In this 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.

Handling Empty Data in an ASP.NET Repeater control
 
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:
Empty Data
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.
Empty Data Message
I hope this article was useful and I thank you for viewing it. If you liked the article,  Subscribe to the RSS Feed or Subscribe Via Email 









Page copy protected against web site content infringement by Copyscape


How would you rate this article?

User Feedback
Comment posted by CaMeL on Thursday, February 12, 2009 8:44 PM
Thanks for great code ! I needed something like that :)
Comment posted by Yogesh on Friday, February 13, 2009 1:11 AM
Thanks,
It's very good idea.
I like this coading...
Comment posted by Lod Lawson on Thursday, March 05, 2009 12:53 PM
thanks a lot for this one.really helped
spent an hour trying to figure out how to
display a simple feed back message when no data is found
thanks very much
Comment posted by Matt on Friday, May 29, 2009 3:48 PM
I think it would be better to show/hide the whole [tr] row, otherwise when there is data, you have an empty row just sitting there.  That can be acheived by [tr id="myNoDataRow" runat="server"] then finding that control instead of the Label in the code-behind.
Comment posted by Thanigainathan on Sunday, May 31, 2009 2:41 AM
Hi,

Very nice post. Thanks for noting down the footer template .

Thanks,
Thani
Comment posted by kumar on Sunday, May 31, 2009 4:46 AM
Some times we don't have data in our table but what to do with this emptyness so it useful to show some message like this
Comment posted by Shail on Sunday, May 31, 2009 1:06 PM
Good one
Comment posted by Jit on Tuesday, September 22, 2009 7:48 AM
Nice tutorial,
i like the idea of footer template.
i got one more tutorial at below URL which is also working well.

http://shivasoft.in/view_tutorial.php?id=100



Comment posted by Ian on Thursday, November 26, 2009 11:50 AM
I use something like:

    protected void rptTerritories_PreRender(object sender, EventArgs e)
    {
        Repeater rpt = (Repeater)sender;
        if (rpt.Items.Count == 0)
            rpt.Controls.Add(new LiteralControl("No territories selected."));
    }
Comment posted by Ian on Thursday, November 26, 2009 11:55 AM
(edit: obviously above wont work if you have header or footer templates)
Comment posted by Ian on Thursday, November 26, 2009 12:20 PM
Try somthing like this if you dont want the table header shown:

.aspx:

...
<asp:Repeater ID="rptDateWindows" runat="server" DataSource='<%# Eval("NonNullDateWindows") %>'
OnPreRender="rptDateWindows_PreRender">
<HeaderTemplate>
  <table>
    <tr id="DateWindowHeader" runat="server">
...
(etc)


.aspx.cs:

    protected void rptDateWindows_PreRender(object sender, EventArgs e)
    {
        Repeater rpt = (Repeater)sender;
        if (rpt.Items.Count == 0)
        {
            Control c = (Control)this.FindControlRecursive("DateWindowHeader", rpt);
            c.Visible = false;

            rpt.Controls.Add(new LiteralControl("No date windows selected."));
        }
    }


where the 'FindControlRecursive' method penetrates NamingContainer boundaries (I cant remember where I got it - google for it, its quite useful). You need a recursive find because the Header template is a naming container.

cheers
Comment posted by Brian Maddox on Saturday, December 05, 2009 7:42 PM
On a similar note, you can hide items within the repeater if they have no data through the method described at http://blog.evonet.com.au/post/2009/12/04/Hiding-table-rows-in-a-repeater.aspx
Comment posted by Brian Maddox on Saturday, December 05, 2009 7:43 PM
On a similar note, you can hide items within the repeater if they have no data through the method described at

<a href="http://blog.evonet.com.au/post/2009/12/04/Hiding-table-rows-in-a-repeater.aspx">http://blog.evonet.com.au/post/2009/12/04/Hiding-table-rows-in-a-repeater.aspx</a>

Post your comment
Name:
E-mail: (Will not be displayed)
Comment:
Insert Cancel

NEWSLETTER