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"> <FooterTemplate> <asp:Label Text="No Items Found" runat="server" Visible='<%# ((Repeater)Container.Parent).Items.Count == 0 %>' /> </FooterTemplate> <ItemTemplate></ItemTemplate> </asp:Repeater>
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.