Microsoft KB Archive/306154

= How To Display Hierarchical Data by Using Nested Repeater Controls and Visual C# .NET =

Article ID: 306154

Article Last Modified on 3/29/2007

-

APPLIES TO


 * Microsoft ASP.NET 1.0
 * Microsoft ASP.NET 1.1
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition

-



This article was previously published under Q306154



For a Microsoft Visual Basic .NET version of this article, see 326338.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System.Data
 * System.Data.SqlClient

IN THIS TASK
SUMMARY
 * Bind to the Parent Table
 * Bind to the Child Table
 * Complete Code List

REFERENCES



SUMMARY
This article describes how to use nested Repeater controls to display hierarchical data. You can apply this concept to other list-bound controls.

back to the top

Bind to the Parent Table
 Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. Click Visual C# Projects under Project Types, and then click ASP.NET Web Application under Templates. In the Location box, delete the WebApplication, and then type NestedRepeater. If you use the local server, leave the server name as http://localhost. The following path appears in the Location box:

http://localhost/ NestedRepeater

Click OK. In Solution Explorer, right-click the NestedRepeater project name node, point to Add, and then click Add Web Form. To name the Web Form, type NestedRepeater, and click Open. The new Web Form is created. It opens in Design View in the Integrated Development Environment (IDE) of Microsoft Visual Studio .NET. From the Toolbox, select the Repeater control, and then drag it to the Web Form page.</li> Change the ID property of this Repeater control to parentRepeater.</li>  Switch to the HTML view for this Web Form. To do so, click the HTML tab in the lower-left corner of the Designer. The Repeater control generates the following HTML code: <asp:Repeater id=&quot;parentRepeater&quot; runat=&quot;server&quot;></asp:Repeater> </li>  Add the following code in the Repeater tags: <%# DataBinder.Eval(Container.DataItem, &quot;au_id&quot;) %> After you do that, the HTML code for the Repeater is as follows: <asp:Repeater id=&quot;parentRepeater&quot; runat=&quot;server&quot;> <%# DataBinder.Eval(Container.DataItem, &quot;au_id&quot;) %> </asp:Repeater> </li> In Solution Explorer, right-click NestedRepeater.aspx, and then click View Code to switch to the NestedRepeater.aspx.cs code-behind file.</li>  Add the following namespace declaration to the top of the file: using System.Data; using System.Data.SqlClient; </li>  Add the following code to the Page_Load event to create a connection to the Pubs database, and then to bind the Authors table to the Repeater control: public void Page_Load(object sender, EventArgs e)     { //Create the connection and DataAdapter for the Authors table. SqlConnection cnn = new SqlConnection(&quot;server=(local);database=pubs; Integrated Security=SSPI&quot;); SqlDataAdapter cmd1 = new SqlDataAdapter(&quot;select * from authors&quot;,cnn);

//Create and fill the DataSet. DataSet ds = new DataSet; cmd1.Fill(ds,&quot;authors&quot;); //Insert code in step 4 of the next section here. //Bind the Authors table to the parent Repeater control, and call DataBind. parentRepeater.DataSource = ds.Tables[&quot;authors&quot;]; Page.DataBind;

//Close the connection. cnn.Close; } NOTE: You may have to modify the database connection string as appropriate for your environment.

</li> Save all of the files.</li> In Solution Explorer, right-click the NestedRepeater.aspx, and then click Set As Start Page.</li> On the Build menu click Build Solution to compile the project.</li> View the .aspx page in the browser, and then verify that the page works thus far.

The output should appear as follows: <ul> 172-32-1176</li> 213-46-8915</li> 238-95-7766</li> 267-41-2394</li> ...</li></ul> </li></ol>

back to the top

Bind to the Child Table
<ol> <li> In the HTML view of the NestedRepeater.aspx page, locate the following line of code: <%# DataBinder.Eval(Container.DataItem, &quot;au_id&quot;) %> Add the following code after this code:

<asp:repeater id=&quot;childRepeater&quot; runat=&quot;server&quot;> <%# DataBinder.Eval(Container.DataItem, &quot;[\&quot;title_id\&quot;]&quot;)%> </asp:repeater> This new code adds a second Repeater control to the ItemTemplate property of the parent Repeater control. </li> <li> Set the DataSource property for the child Repeater control as follows: <asp:repeater ... datasource='<%# ((DataRowView)Container.DataItem) .Row.GetChildRows(&quot;myrelation&quot;) %>' > After you set the DataSource property for the child Repeater control, the HTML code for the two Repeater controls (parent and child) appears as follows:

<asp:Repeater id=&quot;parentRepeater&quot; runat=&quot;server&quot;> <b> <%# DataBinder.Eval(Container.DataItem, &quot;au_id&quot;) %> </b> <asp:repeater id=&quot;childRepeater&quot; runat=&quot;server&quot; datasource='<%# ((DataRowView)Container.DataItem) .Row.GetChildRows(&quot;myrelation&quot;) %>' > <%# DataBinder.Eval(Container.DataItem, &quot;[\&quot;title_id\&quot;]&quot;)%> </asp:Repeater> </asp:Repeater> </li> <li> Add the following page directive to the top of the page: <%@ Import Namespace=&quot;System.Data&quot; %> </li> <li> In the code-behind page, replace the following line in the Page_Load event //Insert code in step 4 of the next section here. with the following code: //Create a second DataAdapter for the Titles table. SqlDataAdapter cmd2 = new SqlDataAdapter(&quot;select * from titleauthor&quot;,cnn); cmd2.Fill(ds,&quot;titles&quot;);

//Create the relation between the Authors and Titles tables. ds.Relations.Add(&quot;myrelation&quot;,        ds.Tables[&quot;authors&quot;].Columns[&quot;au_id&quot;],         ds.Tables[&quot;titles&quot;].Columns[&quot;au_id&quot;]);

This adds the Titles table to the DataSet, and then adds the relationships between the Authors and Titles tables.

</li> <li>Save and compile the application.</li> <li>View the page in the browser, and then verify that the page works so far. The output should appear as follows:

172-32-1176

PS3333

213-46-8915

BU1032

BU2075

238-95-7766

PC1035

267-41-2394

BU1111

TC7777

...

</li></ol>

back to the top

Nestedrepeater.aspx
<%@ Page language=&quot;c#&quot; Codebehind=&quot;NestedRepeater.aspx.cs&quot; AutoEventWireup=&quot;false&quot; Inherits=&quot;NestedRepeater.NestedRepeater&quot; %> <%@ Import Namespace=&quot;System.Data&quot; %>

<asp:repeater id=&quot;parentRepeater&quot; runat=&quot;server&quot;> <%# DataBinder.Eval(Container.DataItem,&quot;au_id&quot;) %>

<asp:repeater id=&quot;childRepeater&quot; datasource='<%# ((DataRowView)Container.DataItem) .Row.GetChildRows(&quot;myrelation&quot;) %>' runat=&quot;server&quot;>

<%# DataBinder.Eval(Container.DataItem, &quot;[\&quot;title_id\&quot;]&quot;)%> </asp:repeater>

</asp:repeater>

Nestedrepeater.aspx.cs
using System; using System.Data; using System.Data.SqlClient; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls;

namespace NestedRepeater {  public class NestedRepeater : System.Web.UI.Page {     protected System.Web.UI.WebControls.Repeater parentRepeater; public NestedRepeater {        Page.Init += new System.EventHandler(Page_Init); }     public void Page_Load(object sender, EventArgs e)      { //Create the connection and DataAdapter for the Authors table. SqlConnection cnn = new SqlConnection(&quot;server=(local);database=pubs; Integrated Security=SSPI ;&quot;); SqlDataAdapter cmd1 = new SqlDataAdapter(&quot;select * from authors&quot;,cnn);

//Create and fill the DataSet. DataSet ds = new DataSet; cmd1.Fill(ds,&quot;authors&quot;);

//Create a second DataAdapter for the Titles table. SqlDataAdapter cmd2 = new SqlDataAdapter(&quot;select * from titleauthor&quot;,cnn); cmd2.Fill(ds,&quot;titles&quot;);

//Create the relation bewtween the Authors and Titles tables. ds.Relations.Add(&quot;myrelation&quot;,        ds.Tables[&quot;authors&quot;].Columns[&quot;au_id&quot;],         ds.Tables[&quot;titles&quot;].Columns[&quot;au_id&quot;]);

//Bind the Authors table to the parent Repeater control, and call DataBind. parentRepeater.DataSource = ds.Tables[&quot;authors&quot;]; Page.DataBind;

//Close the connection. cnn.Close; }     private void Page_Init(object sender, EventArgs e)      { InitializeComponent; }     private void InitializeComponent {            this.Load += new System.EventHandler(this.Page_Load); }  } } back to the top

<div class="references_section">