Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

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.

Bind to the Parent Table

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. Click Visual Basic Projects under Project Types, and then click ASP.NET Web Application under Templates.
  4. 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
  5. In Solution Explorer, right-click the project-name node (NestedRepeater), click Add, and then click Add Web Form.
  6. In the Name box, type NestedRepeater, and click Open.
  7. 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.
  8. Change the ID property of this Repeater control to parentRepeater.
  9. 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="parentRepeater" runat="server"></asp:Repeater>
    					
  10. Add the following code in the Repeater tags:
    <itemtemplate>
         <b><%# DataBinder.Eval(Container.DataItem, "au_id") %></b><br>
    </itemtemplate>
    					
    After you add the code, the HTML code for the Repeater is as follows:
    <asp:Repeater id="parentRepeater" runat="server">
    	<itemtemplate>
    	     <b><%# DataBinder.Eval(Container.DataItem, "au_id") %></b><br>		
            </itemtemplate>
    </asp:Repeater>
    					
  11. In Solution Explorer, right-click the NestedRepeater.aspx, and then click View Code to switch to the code-behind page (NestedRepeater.aspx.vb).
  12. Add the following namespace declaration to the top of the file:
    Imports System.Data
    Imports System.Data.SqlClient
    					
  13. 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("server=(local);database=pubs;Integrated Security=SSPI")
        Dim cmd1 As SqlDataAdapter = New SqlDataAdapter("select * from authors", cnn)
        Dim ds As DataSet = New DataSet()
        cmd1.Fill(ds, "authors")
    
        'Insert code in step 4 of the next section here.
    
        parentRepeater.DataSource = ds.Tables("authors")
        Page.DataBind()
        cnn.Close()
    End Sub
    					
    NOTE: You may have to modify the database connection string as appropriate for your environment.

  14. Save all of the files.
  15. On the Build menu, click Build Solution to compile the project.
  16. 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
    ...

Bind to the Child Table

  1. In the HTML view of the NestedRepeater.aspx page, locate the following code:
    <b><%# DataBinder.Eval(Container.DataItem, "au_id") %></b><br>.<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="childRepeater" runat="server">
    		<itemtemplate>
    	            <%# Container.DataItem("title_id") %>
    		    <br>
    		</itemtemplate>
    </asp:repeater>
    					
  2. Set the DataSource property for the child Repeater control as follows:
    <asp:repeater ... datasource='<%# Container.DataItem.Row.GetChildRows("myrelation") %>'>
    					
    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="parentRepeater" runat="server">
    	<itemtemplate>
    		<b>
    		 <%# DataBinder.Eval(Container.DataItem, "au_id") %>
    		</b>
    		<br>
    		<asp:repeater id="childRepeater" runat="server" 
                        datasource='<%#
                        Container.DataItem.Row.GetChildRows("myrelation")%>'>
    			<itemtemplate>
    				<%# Container.DataItem("title_id") %><br>	
    			</itemtemplate>
    		</asp:Repeater> 
    	</itemtemplate>
    </asp:Repeater>
    					
  3. Add the following page directive to the top of the page:
    <%@ Import Namespace="System.Data" %>
    					
  4. 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("select * from titleauthor", cnn)
    cmd2.Fill(ds, "titles")
    ds.Relations.Add("myrelation", _
    ds.Tables("authors").Columns("au_id"), _
    ds.Tables("titles").Columns("au_id"))
    parentRepeater.DataSource = ds.Tables("authors")
    					
  5. Save all files, and then build the project.
  6. 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
    ...

Complete Code List

Nestedrepeater.aspx

<%@ Import Namespace="System.Data" %>
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="NestedRepeater.aspx.vb" Inherits="yourprojectnamespace.NestedRepeater"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
	<HEAD>
		<title>NestedRepeater</title>
		<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
		<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
		<meta name="vs_defaultClientScript" content="JavaScript">
		<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
	</HEAD>
	<body MS_POSITIONING="GridLayout">
		<form id="Form1" method="post" runat="server">
			<asp:Repeater id="parentRepeater" runat="server">
				<itemtemplate>
					<b>
						<%# DataBinder.Eval(Container.DataItem, "au_id") %>
					</b>
					<br>
					<asp:repeater id="childRepeater" 
                                         runat="server" datasource='<%# Container.DataItem.Row.GetChildRows("myrelation") %>'>

						<itemtemplate>
							<%# Container.DataItem("title_id") %><br>	
						</itemtemplate>
					</asp:Repeater> 
				</itemtemplate>
			</asp:Repeater>
		</form>
	</body>
</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

#Region " Web Form Designer Generated Code "

    '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

#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("server=(local);database=pubs;Integrated Security=SSPI")
        Dim cmd1 As SqlDataAdapter = New SqlDataAdapter("select * from authors", cnn)
        Dim ds As DataSet = New DataSet()
        cmd1.Fill(ds, "authors")

        Dim cmd2 As SqlDataAdapter = New SqlDataAdapter("select * from titleauthor", cnn)
        cmd2.Fill(ds, "titles")
        ds.Relations.Add("myrelation", _
        ds.Tables("authors").Columns("au_id"), _
        ds.Tables("titles").Columns("au_id"))
        parentRepeater.DataSource = ds.Tables("authors")

        parentRepeater.DataSource = ds.Tables("authors")
        Page.DataBind()
        cnn.Close()

    End Sub

End Class

				

↑ Back to the top


Keywords: KB326338, kbservercontrols, kbhowtomaster, kbdatabinding

↑ Back to the top

Article Info
Article ID : 326338
Revision : 7
Created on : 7/30/2003
Published on : 7/30/2003
Exists online : False
Views : 363