Many of you might already know about this, but I’ll post it for those of you like me who didn’t. Occasionally I’ll nest a repeater inside of another repeater, and when I do I always attach an ItemDataBound event handler to the parent repeater so I can set the DataSource of the child repeater.
<asp:Repeater ID="rptManufacturers" runat="server" OnItemDataBound="rptManufacturers_ItemDataBound"> <ItemTemplate> <%# Eval("Name") %> <asp:Repeater runat="server" ID="rptModels"> <HeaderTemplate> <ul> </HeaderTemplate> <ItemTemplate> <li><%# Eval("ModelName") %></li> </ItemTemplate> <FooterTemplate> </ul> </FooterTemplate> </asp:Repeater> </ItemTemplate> </asp:Repeater>protected void rptManufacturers_ItemDataBound(object sender, RepeaterItemEventArgs e) { Manufacturer man = (Manufacturer)e.Item.DataItem; Repeater rptModels = (Repeater)e.Item.FindControl("rptModels"); rptModels.DataSource = man.Models; rptModels.DataBind(); }I always found it annoying that I had to create a ItemDataBound function when the only thing I needed to do was bind the child repeater. However, recently I found out you don’t need to bother with all the above code. You can just do this:
<asp:Repeater ID="rptManufacturers" runat="server"> <ItemTemplate> <%# Eval("Name") %> <asp:Repeater runat="server" ID="rptModels" DataSource='<%# Eval("Models") %>'> <HeaderTemplate> <ul> </HeaderTemplate> <ItemTemplate> <li><%# Eval("ModelName") %></li> </ItemTemplate> <FooterTemplate> </ul> </FooterTemplate> </asp:Repeater> </ItemTemplate> </asp:Repeater>This is a really simple solution when the only thing you need to do is bind a child control ( Repeater, DataGrid, GridView, etc ). The solution doesn’t really apply if you need to do more logic on the ItemDataBound event.