Repeater control templates with empty datasource
Just a quick note and quick fix. I have a Repeater control with both Header, Item and Footer templates in one of my ASP.NET 2.0 projects. This control is rendering contextual site navitagion to ul/li structure. Sometimes this navigation is simply empty and i needed some easy way (preferably declarative) to hide Header and Footer templates when the datasource is empty because they were still beeing rendered.
To my knowledge, there is no property on Repeater control that i could for such behaviour so i solved my problem with this little trick. I'm using the Visible property of Repeater control and hiding it when there are no data in the datasource available.
1: <asp:Repeater
2: runat="server"
3: DataSourceID="ContextualSitemapDataSource"
4: Visible="<%# Repeater1.Items.Count > 0 %>"
5: EnableViewState="false">
6: <HeaderTemplate>
7: <h2>Contextual Links</h2>
8: <ul class="menublock">
9: </HeaderTemplate>
10: <ItemTemplate>
11: <li><a
12: runat="server"
13: href='<%# Eval("[navigateurl]") %>'><%# Eval("Title") %></a>
14: </li>
15: </ItemTemplate>
16: <FooterTemplate>
17: </ul>
18: </FooterTemplate>
19: </asp:Repeater>
Is there some straight way how to do this declaratively in ASP.NET 2.0? I tried google search but didn't find anything usefull.
How about this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
repeater.DataBind()
Dim record As String = repeater.Items.Count.ToString
If record = 0 Then
repeater.Visible = False
Else
repeater.Visible = True
End If
End Sub