Extended asp:Repeater

Published 19 June 04 11:31 AM | alexcampbell

I've always wished for a Repeater with a NoData template.  It is a very common requirement that we display a “No data available” message when a database query returned no rows.  Now I have finally got around to writing this very simple extension:

using System;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

namespace alexcampbell.Controls

{

      /// <summary>

      /// An asp:Repeater control that lets you specify a template to be shown if there is no data in the repeater

      /// </summary>

      public class ExtendedRepeater : Repeater

      {

            private ITemplate _noDataTemplate;

            public ITemplate NoDataTemplate {

                  get {

                        return _noDataTemplate;

                  }

                  set {

                        _noDataTemplate = value;

                  }

            }

 

 

            protected override void OnDataBinding(EventArgs e) {

                  base.OnDataBinding (e);

                  if(this.Items.Count == 0) {

                        NoDataTemplate.InstantiateIn(this);

                  }

            }

      }

}

You can use this control on a web form like this:

<%@ Register TagPrefix=”AlexCampbell” Namespace=”alexcampbell.Controls” Assembly=”alexcampbell” %>
<AlexCampbell:ExtendedRepeater>
    <ItemTemplate>
          <%# Container.DataItem %>
    </ItemTemplate>
    <NoDataTemplate>
         No data returned!
    </NoDataTemplate>
</AlexCampbell:ExtendedRepeater>

Comments

# TrackBack said on June 18, 2004 09:01 AM:
# Jerry Pisk said on June 18, 2004 10:08 PM:

Since I'm way too lazy to try it - will it still render the header and footer, with the NoDataTemplate between or not? My guess is not, but I could be wrong...

# J. Michael Palermo IV said on June 18, 2004 11:57 PM:

What a great idea. Thanks for the post!

# Joe said on June 19, 2004 04:51 AM:

I don't think this will work. If you don't DataBind (e.g. during a postback), nothing will be displayed.

# Jerry Pisk said on June 19, 2004 03:31 PM:

Well, yeah, you probably should override Render, not OnDataBinding... But then, I personally don't use postbacks (whyw ould I want to process input in the same page where I generate the input view? What happened to separating presentation from logic), so this works just fine.

# Jason said on June 21, 2004 03:41 PM:

This is super-cool.

# Eric Newton said on June 24, 2004 03:31 PM:

I actually implemented this on datagrids, and have been beating the drum to the System.Web guys to go ahead and DO THIS in the framework... kudos

# Will Jayroe said on July 8, 2004 12:30 PM:

Hey, I got this to work, but it puts the <NoDataTemplate> *after* the header and footer template tags... is there a way to fix this or just not use the header and footer template tags?

# Granger said on August 7, 2004 04:13 PM:

Here's a way that works (and it goes between the header and footer as it ought). Instead of overriding OnDataBinding, do this:

protected override void OnItemCreated(RepeaterItemEventArgs ea)
{
if (ea.Item.ItemType == ListItemType.Footer && this.Items.Count == 0)
NoDataTemplate.InstantiateIn(this);
base.OnItemCreated(ea);
}

# Granger said on August 7, 2004 04:42 PM:

One more change. The above way won't allow you to put Asp-tags in the template (you'll get that "The Controls collection cannot be modified because the control contains code blocks..." exception).

Change the NoDataTemplate to be of type PlaceHolder, then change the method to look like this:

protected override void OnItemCreated(RepeaterItemEventArgs ea)
{
if (ea.Item.ItemType == ListItemType.Footer && this.Items.Count == 0)
Controls.Add(NoDataTemplate);
base.OnItemCreated(ea);
}

# Bruusi said on August 10, 2004 10:50 AM:

Anyone know if it would be able to hide the Header and Footer templates in case of an empty data source?

# TrackBack said on October 20, 2004 07:58 PM:
# Life Is What You Make It said on May 16, 2007 07:42 AM:

Pingback from  Life Is What You Make It

# Andrew said on June 12, 2007 09:32 PM:

What about vb.net ?

A straight conversion doesn't seem to work.  The NoDataTemplate ends up as a property only (i.e. <cc1:ExtendedRepeater NoItemTemplate="No Data returned"> instead of being able to next it properly - <NoDataTemplate>

Frustrated here.

Not happy about being forced to use vb.net, all the code examples are c# ...