Stock Photos, Royalty Free Stock Photography, Photo Search
Royalty Free Images

Handling empty data in a Repeater

As powerful and elegant as the Repeater control is, there is no built mechanism for handling how an empty data condition will be displayed. Other controls, for example the GridView, provide properties and templates that will be rendered when the DataSource is void of data. Unfortunately no such behavior is built into the Repeater control. Fortunately, due to the inherent flexibility of the Repeater, it is fairly easy to add this functionality. Within this article, we'll explore using an existing Repeater to display a message when the data collection bound to the repeater is empty.

In the following code example, a Repeater is used to generate a table based on a databound Collection.

<asp:Repeater runat="server" ID="Repeater1">
<HeaderTemplate>
<table width="80%">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:Label runat="server" Text='<%# Eval("Model") %>' /></td>
</tr>
</ItemTemplate>
<
FooterTemplate>
</table>
</FooterTemplate>
</
asp:Repeater>
 
if (!IsPostBack)
{
List<Car> cars = new List<Car>();
Repeater1.DataSource = cars;
Repeater1.DataBind();
}

Since there haven't been any items added to the "cars" collection, the ItemTemplate will not be used and the Repeater will generate an empty table. Note that the behavior is to render the HeaderTemplate and FooterTemplate even though an empty collection has been bound to the Repeater. The following shows the resulting HTML.

<table width="80%">
</
table>

Since the Header will render regardless of if there is are any items in the collection, this is a good place to put the HTML (or ASP.NET controls) that will display when there is no data to display. We will also set the Visible property based on the data count of the Repeater's DataSource.

Building on the previous example, the HeaderTemplate will now contain:

<table width="80%">
<tr runat="server"
visible='<%# ((ICollection)Repeater1.DataSource).Count == 0 ? true : false %>'>
<td>
There is no data to display
</td>
</tr>

There are a couple of items that should be noted in the above example.

  • The Repeater has been referenced by it's ID.
  • The DataSource property of the Repeater has been cast to an ICollection object. DataSource is defined as type object, but when data is bound to it, it must be of type ICollection. We are using the Count property from this interface to determine if the "Empty Data" message should display.

When there are several controls where the Visible property needs to be set, the logic can be moved to the code-behind for the page. This is shown in the following IsDataEmpty property.

protected bool IsDataEmpty 
{
get
{
ICollection list = Repeater1.DataSource as ICollection;
return list.Count == 0 ? true : false;
}
}

The ASPX will change to reference this property as follows:

<table width="80%">

<tr runat="server"
visible='<%# IsDataEmpty %>'>
<td>
There is no data to display
</td>
</tr>

Regardless of which approach you use, the resulting HTML will now look like:

<table width="80%">
<tr>
<td>
There is no data to display
</td>
</tr>
</
table>

As shown in the above examples, it is fairly simple to add HTML that will conditionally render based on the presence (or lack) of data bound to a Repeater.

Vince wrote on 2/21/2008 6:47:27 AM:

Well done... This is exactly what I was looking for. A lot of other places I looked involved extending the Repeater class into a custom control, etc. Much more elegant way of handling this problem. Thank You!

Dean wrote on 7/8/2008 8:04:03 AM:

Very clever, really like it

Yoann. B wrote on 12/13/2008 3:40:18 AM:

Hi, Check out my approach using a CustomRepeater WebControl with Templatable Empty Data : http://blog.sb2.fr/post/2008/12/12/How-To-Handle-Empty-Data-With-ASPNET-Repeater.aspx

Gordon Bergling wrote on 12/23/2008 2:44:25 AM:

Thanks your post. Saved me a lot of time...

Christine wrote on 1/12/2009 12:27:17 PM:

This solution is elegantly fabulous. Love it. Thank you very much!

Pino wrote on 1/17/2009 8:51:14 AM:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: Line 97: { Line 98: ICollection list = Contacts.DataSource as ICollection; Line 99: return list.Count == 0 ? true : false; Line 100: } Line 101:}

Housy wrote on 2/5/2009 2:07:31 PM:

How can this be done when the DataSource is set to the result of a method that returns IEnumerable<type> ?

Housy wrote on 2/5/2009 2:08:27 PM:

How can this be done when the DataSource is set to the result of a method that returns IEnumerable<> ?

Eric wrote on 3/30/2009 10:39:23 AM:

Alternately, you can check if Repeater1.Items.Count is 0 in Page_PreRenderComplete.

Toby wrote on 6/8/2009 9:22:40 AM:

There it is again.. Why code return list.Count == 0 ? true : false; when you can just write: return list.Count == 0; ?? Makes no sense to turn simple expression into an IF statement.

Ian wrote on 11/26/2009 9:49:08 AM:

Personally I just use: 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.")); }

sunny wrote on 3/4/2010 11:12:08 AM:

This is awesome! thanks a lot

Parth Trivedi wrote on 8/23/2010 2:36:15 AM:

Thanks its really helpfull

max lengh on field - thefilthylist.com wrote on 6/9/2011 11:59:11 AM:

no results row for a repeater dynamic toggle visibility markup / declarative based example.. thank you, wonderful..

textsanitation.com wrote on 6/9/2011 12:00:44 PM:

this example is fucking awesome!!! Are you filtering your text!!?? www.thefilthylist.com

Mike wrote on 6/9/2011 12:09:01 PM:

http://www.thefilthylist.com sounds interesting , seems like a real cool service, most of user interactive / software intensive systems need it

Leave a Comment

Name  
Comment  
Enter CodeEnter security code