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["ChildItems"].Columns["CatID"], true);
                relation.Nested = true;

                parentRepeater.DataSource = myDS;


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

<asp:Repeater id="parentRepeater" runat="server">
        <tr class="categoryInTable">
        <td colspan="2">
        <asp:Repeater ID="childRepeater" runat="server" datasource='<%

Ant: Thank you very much! I'm new to 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'

Posted: 28 August 2007

comments powered by Disqus