HOW TO:使用嵌套 Repeater 控件和 Visual Basic .NET显示分层数据
来源:互联网 发布:python的合法标识符 编辑:程序博客网 时间:2024/05/16 12:08
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.DataImports System.Data.SqlClientPublic 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 SubEnd 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 |
- HOW TO:使用嵌套 Repeater 控件和 Visual Basic .NET显示分层数据
- HOW TO:使用嵌套 Repeater 控件和 Visual C# .Net 显示分层数据
- 使用嵌套 Repeater 控件和 Visual C# .Net 显示分层数据
- 使用嵌套 Repeater 控件和 Visual C# .Net 显示分层数据
- 使用嵌套 Repeater 控件和 Visual C# .Net 显示分层数据
- 使用嵌套的Repeater控件显示分级数据
- 使用嵌套的Repeater控件显示分级数据
- 使用嵌套的Repeater控件显示分级数据
- 使用嵌套的Repeater控件显示分级数据(转)
- 使用嵌套的Repeater控件显示分级数据
- 使用嵌套的Repeater控件显示分级数据
- 使用嵌套的Repeater控件显示分级数据
- 使用嵌套的Repeater控件显示分级数据
- 使用嵌套的Repeater控件显示分级数据
- asp.net Repeater控件嵌套使用
- ASP.NET Repeater控件的嵌套使用
- Repeater控件嵌套使用
- Repeater 控件嵌套使用
- Debian 的硬盘安装
- 线程的基础知识
- 如何撰写优秀的毕业论文
- 使用Erwin进行MYSQL的数据库设计
- CASSINI源代码分析(4)
- HOW TO:使用嵌套 Repeater 控件和 Visual Basic .NET显示分层数据
- 交换机、集线器、路由器区别
- JBoss及相关设置
- {推荐}-----IT职业生涯规划PPT
- 老爸腰伤了
- 杂感
- 软件测试工具的分类和选择
- IBM2004开发者大会归来
- calc