关闭

HOW TO:使用嵌套 Repeater 控件和 Visual Basic .NET显示分层数据

2615人阅读 评论(0) 收藏 举报

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
 
Bind to the Parent Table
Bind to the Child Table
Complete Code List
REFERENCES
On this Page
SUMMARY SUMMARY
REFERENCES 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

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
...
back to the top

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
...
back to the top

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


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
Did this content help you?
Yes
No
Maybe
Please select one option based on your first choice:
I'm very satisfied
I think it will help, but I haven't tried it yet
It is helpful, but I need more information
It is helpful, but hard to understand
Seemed relevant in search results, but didn't help me
The information is wrong
The page contains one or more broken links
Suggest new content or let us know how we can improve this content (optional):

©2004 Microsoft Corporation. 版权所有.  保留所有权利 |商标 |隐私权声明
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:200124次
    • 积分:2066
    • 等级:
    • 排名:第19151名
    • 原创:247篇
    • 转载:8篇
    • 译文:0篇
    • 评论:134条