Microsoft KB Archive/326338

= HOW TO: Display Hierarchical Data by Using Nested Repeater Controls and Visual Basic .NET =

Article ID: 326338

Article Last Modified on 7/30/2003

-

APPLIES TO


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

-



This article was previously published under Q326338



For a Microsoft Visual C# .NET version of this article, see 306154.

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 Web 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 Basic Projects under Project Types, and then click ASP.NET Web Application under Templates. In the Location box, delete WebApplication, and then type

NestedRepeater

. If you use the local server, you can leave the server name as http://localhost. The path appears as follows in the Location box: http://localhost/ NestedRepeater In Solution Explorer, right-click the project-name node (NestedRepeater), click Add, and then click Add Web Form. In the Name box, type NestedRepeater, and click Open. The new Web Form is created. It opens in Design View in the 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 HTML that the Repeater control generates is as follows: <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 add the code, 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 the NestedRepeater.aspx, and then click View Code to switch to the code-behind page (NestedRepeater.aspx.vb).</li>  Add the following namespace declaration to the top of the file: Imports System.Data Imports System.Data.SqlClient </li>  Add the following code to the Page_Load event to create a connection to the Pubs database, and to bind the Authors table to the Repeater control: Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim cnn As SqlConnection = New SqlConnection(&quot;server=(local);database=pubs;Integrated Security=SSPI&quot;) Dim cmd1 As SqlDataAdapter = New SqlDataAdapter(&quot;select * from authors&quot;, cnn) Dim ds As DataSet = New DataSet cmd1.Fill(ds, &quot;authors&quot;)

'Insert code in step 4 of the next section here.

parentRepeater.DataSource = ds.Tables(&quot;authors&quot;) Page.DataBind cnn.Close End Sub NOTE: You may have to modify the database connection string as appropriate for your environment.

</li> Save all of the files.</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 so far. The output should appear as follows:

172-32-1176

213-46-8915

238-95-7766

267-41-2394

...

</li></ol>

back to the top

Bind to the Child Table
<ol>  In the HTML view of the NestedRepeater.aspx page, locate the following code: <%# DataBinder.Eval(Container.DataItem, &quot;au_id&quot;) %> .<BR/> After this line of code, add the following code to add a second Repeater control to the ItemTemplate of the parent Repeater control: <asp:repeater id=&quot;childRepeater&quot; runat=&quot;server&quot;> <%# Container.DataItem(&quot;title_id&quot;) %> </asp:repeater> </li>  Set the DataSource property for the child Repeater control as follows: <asp:repeater ... datasource='<%# 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='<%# Container.DataItem.Row.GetChildRows(&quot;myrelation&quot;)%>'> <%# Container.DataItem(&quot;title_id&quot;) %> </asp:Repeater> </asp:Repeater> </li>  Add the following page directive to the top of the page: <%@ Import Namespace=&quot;System.Data&quot; %> </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, which adds the Titles table to the DataSet, and then adds the relationships between the Authors and Titles tables: Dim cmd2 As SqlDataAdapter = New SqlDataAdapter(&quot;select * from titleauthor&quot;, cnn) cmd2.Fill(ds, &quot;titles&quot;) 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;)) parentRepeater.DataSource = ds.Tables(&quot;authors&quot;) </li> Save all files, and then build the project.</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
<%@ Import Namespace=&quot;System.Data&quot; %> <%@ Page Language=&quot;vb&quot; AutoEventWireup=&quot;false&quot; Codebehind=&quot;NestedRepeater.aspx.vb&quot; Inherits=&quot;yourprojectnamespace.NestedRepeater&quot;%>

<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;> <HTML> <HEAD> NestedRepeater <meta name=&quot;GENERATOR&quot; content=&quot;Microsoft Visual Studio.NET 7.0&quot;> <meta name=&quot;CODE_LANGUAGE&quot; content=&quot;Visual Basic 7.0&quot;> <meta name=&quot;vs_defaultClientScript&quot; content=&quot;JavaScript&quot;> <meta name=&quot;vs_targetSchema&quot; content=&quot;http://schemas.microsoft.com/intellisense/ie5&quot;> </HEAD> <body MS_POSITIONING=&quot;GridLayout&quot;> <form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;> <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='<%# Container.DataItem.Row.GetChildRows(&quot;myrelation&quot;) %>'>

<%# Container.DataItem(&quot;title_id&quot;) %> </asp:Repeater> </asp:Repeater> </HTML>

Nestedrepeater.aspx.vb
Imports System.Data Imports System.Data.SqlClient

Public Class NestedRepeater Inherits System.Web.UI.Page Protected WithEvents childRepeater As System.Web.UI.WebControls.Repeater Protected WithEvents parentRepeater As System.Web.UI.WebControls.Repeater


 * 1) Region &quot; Web Form Designer Generated Code &quot;

'This call is required by the Web Form Designer. <System.Diagnostics.DebuggerStepThrough> Private Sub InitializeComponent

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent End Sub


 * 1) End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim cnn As SqlConnection = New SqlConnection(&quot;server=(local);database=pubs;Integrated Security=SSPI&quot;) Dim cmd1 As SqlDataAdapter = New SqlDataAdapter(&quot;select * from authors&quot;, cnn) Dim ds As DataSet = New DataSet cmd1.Fill(ds, &quot;authors&quot;)

Dim cmd2 As SqlDataAdapter = New SqlDataAdapter(&quot;select * from titleauthor&quot;, cnn) cmd2.Fill(ds, &quot;titles&quot;) 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;)) parentRepeater.DataSource = ds.Tables(&quot;authors&quot;)

parentRepeater.DataSource = ds.Tables(&quot;authors&quot;) Page.DataBind cnn.Close

End Sub

End Class

back to the top

<div class="references_section">