I have a LINQ grouping that I'd like to use to populate parent and child repeater controls.
<asp:Repeater ID="Parent" runat="server" OnItemDataBound="Parent_ItemDataBound">
<ItemTemplate>
<%# Eval("Key") %>
<asp:Repeater ID="Child" runat="server">
<ItemTemplate>
<%# Eval("Id") %>
<%# Eval("Name") %>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
public class Dog
{
public int Id { get; set; }
public string Name { get; set; }
public string Breed { get; set; }
}
private IEnumerable<IGrouping<string, Dog>> GetDogs()
{
var dogs = new List<Dog>
{
new Dog
{
Id = 1,
Name = "Rex",
Breed = "Poodle",
},
new Dog
{
Id = 2,
Name = "Fido",
Breed = "Terrier",
},
new Dog
{
Id = 3,
Name = "Killer",
Breed = "Pit Bull",
}
};
return dogs.GroupBy(_ => _.Breed);
}
protected void Page_Load(object sender, EventArgs e)
{
Parent.DataSource = GetDogs();
Parent.DataBind();
}
protected void Parent_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
var item = e.Item;
if ((item.ItemType == ListItemType.Item) || (item.ItemType == ListItemType.AlternatingItem))
{
var repeater = (Repeater)item.FindControl("Child");
// I'm stuck on this code...
//repeater.DataSource = what to do?
//repeater.DataBind();
}
}
I'm trying to set the child repeater's data source in the Parent_ItemDataBound event. How do I do this? item.DataItem is type object and I can't figure out how to obtain the list of dogs that's inside the data item.