HOW TO: Display Hierarchical Data by Using Nested Repeater Controls and Visual Basic .NET
Article ID | : | 326338 |
Last Review | : | July 30, 2003 |
Revision | : | 4.0 |
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 | ||||||
| |||||||
• | REFERENCES |
On this Page
SUMMARY | ||
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
back to the top
back to the top
back to the top
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:
|
10. | Add the following code in the Repeater tags: After you add the code, the HTML code for the Repeater is as follows:
|
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:
|
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: 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: After this line of code, add the following code to add a second Repeater control to the ItemTemplate of the parent Repeater control:
|
2. | Set the DataSource property for the child Repeater control as follows: 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:
|
3. | Add the following page directive to the top of the page:
|
4. | In the code-behind page, replace the following line in the Page_Load event with the following code, which adds the Titles table to the DataSet, and then adds the relationships between the Authors and Titles tables:
|
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
REFERENCES
For more information, visit the following topics in the Microsoft .NET Framework Software Development Kit (SDK):
Adding a Relationship between Tables
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconaddingrelationshipbetweentwotables.asp
Navigating a Relationship between Tables
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconnavigatingrelationshipbetweentwotables.asp
Repeater Web Server Control
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconrepeaterwebservercontrol.asp
back to the top
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconaddingrelationshipbetweentwotables.asp
Navigating a Relationship between Tables
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconnavigatingrelationshipbetweentwotables.asp
Repeater Web Server Control
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconrepeaterwebservercontrol.asp
APPLIES TO
• | Microsoft ASP.NET (included with the .NET Framework 1.1) |
• | Microsoft ASP.NET (included with the .NET Framework) 1.0 |
• | Microsoft Visual Basic .NET 2003 Standard Edition |
• | Microsoft Visual Basic .NET 2002 Standard Edition |
Keywords: | kbdatabinding kbhowtomaster kbservercontrols KB326338 |
<script type=text/javascript>var g_asAnswers = [];var g_iAnswerPtr = 0;var currentsectionid = 1;var navindex = 1;var currentsection;var navpath = new Array();var g_SCID = 'sw;en;1075';var g_SITE = 'gn';var g_REGIONID = 'zh-cn';var g_URL = '%252fdefault.aspx%252fkb%252f326338%252fEN-US%252f';var g_BROWSERLANGCODE = 'zh-cn';var g_SURVEYLANGCODE = 'en';var g_SURVEYID = '1075';var g_SURVEYNAME = '';var g_asParams = ['kb','en-us','326338','','','','','','',''];var g_showall = 0;var g_surveystyle = 'popup';var bClickCancel = 0;var g_maxKBsInCookie = '10';var g_kbvisited = '';</script>