How to: display hierarchical data by using nested review
Article ID: 326338last Review: July 30, 2003Revision: 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
Summaryreferences
Summary
This Article Describes How To Use Nested
Repeater Web Controls to Display Hierarchical Data. You CAN Apply this 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 The Click Project. 3.Click Visual Basic Projects Under Project Types, and the 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 / NestedRepeater5.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 Genereates IS FOLLOWS:
<% # databinder.eval (container.dataitem, "au_id")%> b>
itemtemplate>
After you add the code, the html code for the reteater is ask:
<% # databinder.eval (container.dataitem, "au_id")%> b>
itemtemplate>
ask: 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 .Loaddim 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 the 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: <% # DataBinder.eval (Container.DataItem, "Au_ID")%> b>
.
After this line of code to add a second reperter control to the itemtemplate of the parent review = "childreteater" runat = "limited"> Runat = "server">
<% # Container.dataitem ("Title_ID")%>
itemtemplate>
ask: repeater>
2.Set the DataSource property for the child Repeater control as follows:
<% # Databinder.eval (container.dataitem, "au_id")%>
b>
DataSource = '<% # Container.DataItem.row.getChildrows ("MyRelation")%>>> <% # Container.DataItem ("Title_ID")%> itemtemplate> ask: repeater> itemtemplate> ask: repeater> 3.Add the following page: <% @ Import namespace = "system.data"%> 4.IN The Code-Behind page, Replace The next 'Insert Code in Step 4 of the next section. 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"). Column ("au_id"), _ DS.Tables ("Titles"). Column ("au_id")) ParentrePeater.DataSource = DS.TABLES ("authors") 5.Save all files, and then build the project.6.view the page in the page Works so far. The output shouth as stock: 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"%>
HEAD>