Simple nested Repeater controls

Just found a piece of functionality which I’ve never noticed before, for populating nested Repeaters, without doing so in the code behind with a “OnDataBound” Event.

It’s as simple as just setting the DataSource on the child repeater, to a property of the parent repeaters DataItem.  For example;

<asp:Repeater ID="BookRepeater" runat="server"  onitemdatabound="bibRepeater_ItemDataBound">
<HeaderTemplate><ul></HeaderTemplate>
<ItemTemplate>
<li>
<h1><%# DataBinder.Eval(Container.DataItem,  "BookTitle")%></h1>
<asp:Repeater ID="AuthorsRepeater" runat="server"  DataSource='<%# ((Book)Container.DataItem).Authors %>'>
<HeaderTemplate><h5>Author: </HeaderTemplate>
<ItemTemplate>
<a href='/AuthorDetails.aspx?id=<%#  DataBinder.Eval(Container.DataItem, "Id")%>' title="<%#  DataBinder.Eval(Container.DataItem, "FirstName")%> <%#  DataBinder.Eval(Container.DataItem, "LastName")%>"><%#  DataBinder.Eval(Container.DataItem, "FirstName")%> <%#  DataBinder.Eval(Container.DataItem, "LastName")%></a>
</ItemTemplate>
<SeparatorTemplate>, </SeparatorTemplate>
<FooterTemplate></h5></FooterTemplate>
</asp:Repeater>
</li>
</ItemTemplate>
<FooterTemplate></ul></FooterTemplate>
</asp:Repeater>

You may need to add a declaration to the namespace in which your entities reside, at the top of your aspx file, like this;

<%@ Import Namespace="Hachette.Entities.Orchard" %>

full details on MSDN; How To Display Hierarchical Data by Using Nested Repeater Controls and Visual C# .NET.

  1. #1 by Chris on July 23, 2012 - 12:50

    I use this same approach on a project I am currently working on. Do you have any idea how to persist this data back to the server, it seems that the data is not being stored in the viewstate.

    Thanks
    Chris

(will not be published)