开发者

ASP.NET Repeater control: How not to show the header template if the datasoure has zero elements?

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?


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


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>


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;
}
}


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜