0

I want my header and footer HTML not to bem written in the case that the datasource has no items.

How do I do that?

Fabio Milheiro
  • 8,100
  • 17
  • 57
  • 96
  • How do you bind your data? Code-behind or via DataSourceID? – citronas Feb 21 '11 at 16:42
  • 1
    Found this after I posted my answer - this question has been asked and answered already: http://stackoverflow.com/questions/327151/how-can-i-hide-a-repeater-in-asp-net-c-if-the-datasource-contains-no-items – David Feb 21 '11 at 16:44
  • @David Stratton, that question is not exactly the same. Stop with that try-to-be-smarter behavior by forcely trying to find similar answers. It's anoying and it's probably the only problem with site. Otherwise, perfect. – Fabio Milheiro Aug 24 '11 at 11:57
  • 1
    @Fabio Miheiro - I'm TRULY sorry that my comment sounded offensive. I meant to be helpful. I've been down-voted previously for answering questions that had a previous answer, so I'm hesitant to post an answer if I find another that works. In that situation, I usually leave a comment with a link to the previous question so that the asker can more easily find what I thought was a helpful previous answer more easily. Perhaps another problem with the site is that it does not allow for visual cues or body language to help determine who is trying to help and who is being a smart-aleck. – David Aug 24 '11 at 13:07
  • @David Stratton, body language! Thanks! – Fabio Milheiro Aug 24 '11 at 14:52

4 Answers4

6

I would recommend setting the Repeater visibility to false if the datasource has no items.

Cyberdrew
  • 1,832
  • 1
  • 19
  • 39
1

utilizing Cyberdrew's idea about making it invisible, the following code worked for me where i was able to get the item count correctly.

void Repeater1_PreRender(object sender, EventArgs e)
{
Repeater rpt = (Repeater)sender;
if (rpt.Items.Count == 0)
{
    rpt.Visible = false;
}
}
civilator
  • 119
  • 4
1

if you want to use jQuery see below

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <script type="text/javascript">
        $(document).ready(function () { if ($("#divItemDetails").text().length > 0) { $('#RepeaterDiv').show(); } });
    </script>
    <div style="overflow: hidden; display: none" id="RepeaterDiv">
        <asp:Repeater runat="server" ID="RepeaterID" DataSourceID="RepeaterDataSource">
            <HeaderTemplate>
                All Names </br>
            </HeaderTemplate>
            <ItemTemplate>
                </br>
                <div id="divItemDetails">
                    <%# Container.DataItem%>
                </div>
                </br>
            </ItemTemplate>
        </asp:Repeater>
        <asp:ObjectDataSource runat="server" ID="RepeaterDataSource" SelectMethod="GetAllEmployees"
            TypeName="MyCustomBAL" />
    </div>
    </form>
</body>
</html>
mydevexperience
  • 375
  • 3
  • 13
0

This is a nested repeater sample child repeater accessing parent repeater datasource
I recommend the following, it seems to work fine for me. Notice I am using header and footer and populating the ul tag conditionally based on what the parent sitemapnode's children nodes existence.

If the node has children, we know to write out the ul tag.

Note the double cast we must use in order to obtain the parent datasource SiteMapNode.

<div class="menu mainNav">
<asp:Repeater ID="rptrMainMenu" runat="server" DataSourceID="sdsMain">
<ItemTemplate>
<%--<li>--%>
<%--<asp:HyperLink runat="server" NavigateUrl='<%#Eval("Url")%>'><%#Eval("Title") %></asp:HyperLink>--%>
<asp:Repeater ID="rptrsub1" runat="server" DataSource='<%#CType(Container.DataItem,SiteMapNode).ChildNodes %>'>
<HeaderTemplate>
<ul class='mainmenu'>
</HeaderTemplate>
<ItemTemplate>
<li class='submenu'>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#Eval("Url")%>'><%#Eval("Title") %></asp:HyperLink>
<asp:Repeater ID="rptrsub1" runat="server" DataSource='<%#CType(Container.DataItem,SiteMapNode).ChildNodes%>'>
<HeaderTemplate>
<%-- <%# If(CStr(Eval("Title") & "") = "", "", "<ul class='submenu'>")%>--%>
 <%-- <% System.Diagnostics.Debugger.Break()%>--%>
<%# If(CType(CType(Container.Parent.Parent, RepeaterItem).DataItem, SiteMapNode).HasChildNodes, "<u class='submenu'>", "")%>
</HeaderTemplate>
<ItemTemplate>
<li class='submenu'>
<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%#Eval("Url") %>'><%#Eval("Title")%></asp:HyperLink>
</li>
</ItemTemplate>
<FooterTemplate>
<%# If(CType(CType(Container.Parent.Parent, RepeaterItem).DataItem, SiteMapNode).HasChildNodes, "</u>", "")%>
</FooterTemplate>
</asp:Repeater>
</li>
</ItemTemplate>
<FooterTemplate>
</ul></FooterTemplate>
</asp:Repeater>
<%-- </li>--%>
</ItemTemplate>
    </asp:Repeater>
</div>
RickIsWright
  • 531
  • 5
  • 6