Dirk Bergmann

Developer, panographer. Does consultancy work from own company. Lives in Chiang Mai, Thailand.

Twitter

Chiang Mai

Hua Hin

Thailand Hotels

ListVote - List. Vote. Learn.

Partnerseek - Find Partners

Working with Nested Repeaters in ASP.NET

Sometimes I miss the option that you have in classic ASP, PHP or Ruby on Rails to just put a nested loop into the html page (view, presentation layer) to display nested data. Of course this is, strictly speaking, against ASP.NET best practices where you're supposed to use web controls for everything. Then again one could make the argument that the loop actually belongs in the view. This would be .aspx page, and not in the controller or the model (code behind file in ASP.NET).

The task at hand is to iterate through parent categories and child items. The technique suggested by Microsoft is outlined here, and this article uses a nested repeater with a self-join. Starting point is the following DataSet with two related tables and a repeater in the page:

                 DataSet myDS = new DataSet("NestedRepeaterDemo");
                ... //fill the dataset                

                myDS.Tables[0].TableName = "ParentCategories";
                myDS.Tables[1].TableName = "ChildItems";
                DataRelation relation = new DataRelation("myRelation",
                 myDS.Tables["ParentCategories"].Columns["CatID"],
                 myDS.Tables["ChildItems"].Columns["CatID"], true);
                relation.Nested = true;

                myDS.Relations.Add(relation);
                parentRepeater.DataSource = myDS;

                parentRepeater.DataBind();

So why not bind the child repeater in the page, just for the heck of it:

<table>
<asp:Repeater id="parentRepeater" runat="server">
    <itemtemplate>
        <tr class="categoryInTable">
        <td colspan="2">
         <%
         </td>
        </tr>

        <asp:Repeater ID="childRepeater" runat="server" datasource='<%
        <ItemTemplate>

#

Ant: Thank you very much! I'm new to ASP.net and I have been tearing my hair out over this for the last 3 hours.

Imir: Hi Dirk, you article was a a crack man. Thanks a lot. I had been looking for a solution to this but none was helping. Thanks again.

Clay: After spending hours investigating the "DataBinding: ‘System.Data.DataRow’ does not contain a property with the name x error." error, I finally found this. Thanks!

surendra tarai: Thanks for this article, and i am so much helpful with article.

surendra tarai: but when i tried <asp:Repeater ID=”childRepeater” runat=”server” datasource=’’ > then i got the error 'System.Data.DataRow' does not contain a definition for 'CreateChildView'