views:

318

answers:

4

How do i stop the header template of a repeater from displaying when there are no items in the datasource

            <asp:Repeater ID="TabsRepeater" runat="server" DataSource='<%#Eval("OrderedChildNodes") %>'>
                <HeaderTemplate>
                    <ul class="child">
                </HeaderTemplate>
                <ItemTemplate>

the repeater is nested in another repeater control so i can't check before databinding.

+1  A: 

Put an empty literal control in there and set it's value in the OnItemDataBound function (same for footer).


Hmm... or even a little simpler give it the correct text initially but start with the literal's .Visible property set to false so it won't render. Then just set it to True in OnItemDatabound.

Joel Coehoorn
+1  A: 

Right after you DataBind()...


TabsRepeater.Visible = TabsRepeater.Items.Count > 0;

This makes the whole repeater invisible, because I'm assuming that theres a </ul> in your footer template that you wouldn't want to show either.

mgroves
+1  A: 

You could also make the markup conditional, see example below (untested).

<asp:Repeater ID="TabsRepeater" runat="server" DataSource='<%#Eval("OrderedChildNodes") %>'>
                <HeaderTemplate>
          <% if (  ((yourType)Eval("OrderedChildNodes")).Count > 0 ) %>
                    <ul class="child">
                </HeaderTemplate>
                <ItemTemplate>
James
that was my first thought, but it doesn't seem to like gives the error:Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Stephen Binns
Are you by chance using the .Net 3.5 framework or greater? If so I'd highly recommend replacing the repeater control with the more flexible ListView control which has the utility to deal with empty data.
James
A: 

How about this :

<HeaderTemplate>
   <ul class="child" visible='<%= (TabsRepeater.Items.Count > 0).ToString() %>'>
</HeaderTemplate>
Canavar