Reputation: 1341
I have a question regarding repeaters in ASP.net
I have 2 repeaters nested.
I would like to hide both the parent and the child repeater whenever the child repeater holds no items.
Each parent with their child items are giving unique classes like 'class="childlist_1"'.
ascx file:
<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>
What is the best solution for this?
Thanks in advance!
Upvotes: 1
Views: 1361
Reputation: 800
If like me you like to use a method to bind the child repeater (i.e. DataSource='<%# GetChildDatasource(Eval("parentID").ToString()) %>'
), this won't work as the datasource is binded after the parent's itemdatabound method is triggered.
The workaround is to use the PreRender method on the child repeater :
protected void ChildRpt_PreRender(object sender, EventArgs e)
{
//hide if empty
Repeater rpt = (Repeater)sender;
rpt.Visible = rpt.Items.Count > 0;
}
Upvotes: 0
Reputation: 14614
You can do this in ItemDataBound event
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;
}
}
}
Upvotes: 2