Asp.net Webforms Nested Repeater With Linq Group Data Source
I have a LINQ grouping that I'd like to use to populate parent and child repeater controls.
Solution 1:
Expanding on Joshua's correct answer. If the 4.5 runtime is available to you, you can declare the types on the repeater itself so you will not have to bind the ItemDataBound
event.
<ul><asp:Repeaterrunat="server"ID="Parent"ItemType="IGrouping<String, Dog>"><ItemTemplate><li><%# Item.Key %><ul><asp:Repeaterrunat="server"ID="Child"ItemType="Dog"DataSource="<%#Item%>"><ItemTemplate><li><%# Item.Id %></li><li><%# Item.Name %></li></ItemTemplate></asp:Repeater></ul></li></ItemTemplate></asp:Repeater></ul>
Solution 2:
You can get the data from e.Item.DataItem. You should be able to cast this to IGrouping. Then you can get the inner repeater by looking for the control in the current repeaters repeateritem. Then bind the data from DataItem to the nested repeater.
This code below should work.
All this assumes you don't wrap the nested repeater in other controls.
protectedvoidParent_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
var item = e.Item.DataItem as IGrouping<string, Dog>;
if (item == null)
return;
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
var repeater = e.Item.FindControl("Child") as Repeater;
if (repeater != null)
{
repeater.DataSource = item;
repeater.DataBind();
}
}
}
Post a Comment for "Asp.net Webforms Nested Repeater With Linq Group Data Source"