【问题标题】:Hide child and parent repeater when child repeater is empty当子中继器为空时隐藏子中继器和父中继器
【发布时间】:2017-04-26 00:03:54
【问题描述】:

我对 ASP.net 中的中继器有疑问

我嵌套了 2 个中继器。
每当子中继器没有物品时,我想隐藏父中继器和子中继器。 每个带有子项的父母都提供独特的类,例如 'class="childlist_1"'。

ascx 文件:

<asp:Repeater ID="ParentRepeater" runat="server">
<ItemTemplate>
    <ul class="Mainlist">
       <li>
        <h3 class="selected"><a href="#">List 1</a></h3>
        <ul id="DomainList" class="child-items" runat="server">
            <asp:Repeater ID="ChildRepeater" runat="server">
                <ItemTemplate><li><a href="#">Link to child item</a></li></ItemTemplate>
            </asp:Repeater>
        </ul>
        </li>
    </ul>
</ItemTemplate>
</asp:Repeater>

什么是最好的解决方案?

提前致谢!

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    您可以在ItemDataBound 活动中这样做

    protected void ParentRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item)
        {
            // code that binds ChildRepeater
            .....
    
            // check if ChildRepeater has no items
            if (((Repeater)e.Item.FindControl("ChildRepeater")).Items.Count == 0)
            {
                e.Item.Visible = false;
            }
        }
    }
    

    【讨论】:

    • 我是否在 ItemDataBound 事件中使用它?
    • 如果您想在 ItemDataBound 事件中执行此操作,请查看我编辑的答案。
    • 是的,这正是我需要的!非常感谢
    【解决方案2】:

    如果你像我一样喜欢使用绑定子转发器的方法(即DataSource='&lt;%# GetChildDatasource(Eval("parentID").ToString()) %&gt;'),这将不起作用,因为在触发父项的 itemdatabound 方法后绑定了数据源。

    解决方法是在子转发器上使用 PreRender 方法:

    protected void ChildRpt_PreRender(object sender, EventArgs e)
    {
        //hide if empty
        Repeater rpt = (Repeater)sender;
        rpt.Visible = rpt.Items.Count > 0;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-10
      • 1970-01-01
      相关资源
      最近更新 更多