Easy “No Item Template” In Repeater

A “No Item Template” is some content that you want to display if a Repeater is bound with no items. There is no built in tag to handle empty data in the repeater in markup. But, it is easy to add a “No Item Template” inside your markup without having to extend the Repeater class.

Simply set the Visibility property of your content to the number of items bound to the repeater inside the FooterTemplate tag. Instead of relying on the ID of the Repeater in the conditional, I just cast Container.Parent as a Repeater. This is better than the alternative because:

  • There is less repeated markup in case the ID changes.
  • You can use the snippet inside of naming containers such as nested Repeaters, to avoid the error: “The name ‘repItems’ does not exist in the current context”

Here is what it looks like:

<asp:Repeater id="repItems" runat="server">
		<asp:Label Text="No Items Found" runat="server" 
			Visible='<%# ((Repeater)Container.Parent).Items.Count == 0 %>' />

Pretty simple trick, but this feature is not something that I want to have to add extra code for and event handlers for, and the snippet does the job.

One Response to “Easy “No Item Template” In Repeater”

  1. John Pasquet Says:

    Pretty cool, Tim.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: