How to Display Hierarchical Data by Usted Repeater Controls and Visual C # .NET

xiaoxiao2021-03-06  21

This Article Was previously Published Under Q306154

For a Microsoft Visual Basic .NET VERSION OF This Article, See

326338.

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 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 C # Projects Under Project Types, Andre Click ASP.NET Web Application Under Templates.4.in The Location Box, Delete The WebApplication #, and then Typeater, Leave The Server Name As http: // localhost. The Following Path Appears in the location box:

http:// localhost / nestedrepeater

Click OK. 5.In Solution Explorer, right-click the NestedRepeater project name node, point to Add, and then click Add Web Form.6.To name the Web Form, type NestedRepeater, and click Open.7.The new Web Form is created. It opens in Design View in the Integrated Development Environment (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 Repeater control generates the following HTML code:.. 10.Add The Following Code in The Repeater Tags:

<% # databinder.eval (container.dataitem, "au_id")%>

After you do what, The HTML Code for the Repeater IS FOLLOWS:

<% # databinder.eval (container.dataitem, "au_id")%>

11.In Solution Explorer, right-click NestedRepeater.aspx, and then click View Code to switch to the NestedRepeater.aspx.cs code-behind file.12.Add the following namespace declaration to the top of the file: using System.Data ;

Using system.data.sqlclient;

13.Add the following code to the Page_Load event to create a connection to the Pubs database, and then to bind the Authors table to the Repeater control: public void Page_Load (object sender, EventArgs e)

{

// Create the connection and dataadapter for the authors table.sqlconnection CNN = New SqlConnection ("Server = (local); Database = PUBS; Integrated Security = SSPI");

Sqldataadapter cmd1 = new sqldataadapter ("Select * from authors", CNN);

// CREATE AND FILL THE DATASET.

DataSet DS = New DataSet ();

Cmd1.fill (DS, "Authors");

// INSERT CODE IN Step 4 of the next section here.

// bind the authors table to the parent reteater control, and call data.

PARENTREPEATER.DataSource = DS.TABLES ["authors"];

Page.DATABIND ();

// close the connection.

CNN.Close ();

}

NOTE: You may have to modify the database connection string as appropriate for your environment.14.Save all of the files.15.In Solution Explorer, right-click the NestedRepeater.aspx, and then click Set As Start Page 16.On. THE BUILD MENU CLICK Build Solution To Compile The Project.17.view The .aspx Page In The Browser, and The Verify That The page Works thould 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 Line of Code: <% # DataBinder.eval (Container.DataItem, "AU_ID")%>

Add The Following Code After this Code:

<% # DataBinder.eval (Container.DataItem, "[/" Title_ID / "]")%>

This new code adds a second Repeater control to the ItemTemplate property of the parent Repeater control 2.Set the DataSource property for the child Repeater control as follows:.

.Row.getChildRows ("MyRelation")%>>>

<% # DataBinder.eval (Container.DataItem, "[/" Title_ID / "]")%>

3.Add the following 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.

.............. ..

Sqldataadapter cmd2 = new SqldataAdapter ("Select * from titleauthor", cnn);

Cmd2.fill (DS, "Titles");

// Create The Relation Between The Authors and Titles Tables.

DS.RELATIONS.ADD ("MyRelation",

DS.Tables ["authors"]. Column ["au_id"],

DS.Tables ["Titles"]. Columns ["au_id"]);

This adds the Titles table to the DataSet, and then adds the relationships between the Authors and Titles tables.5.Save and compile the application.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

<% @ Page language = "c #" codebehind = "nestedrepeater.aspx.cs" autoeventwireup = "false" inherits = "nestedrepeater.nestedrepeater"%>%>%>%>%>%>%>%>%>%>%>%>%>%>%>%>%>%>%>%>%>%>%>%>%>%>%>%>%>%>%>%>%>%>%>%>%>%>%>

<% @ Import namespace = "system.data"%>

<% # databinder.eval (container.dataitem, "au_id")%>

<% # DataBinder.eval (Container.DataItem, "[/" Title_ID / "]")%>

NestedRepeater.aspx.cs

Using system;

Using system.data;

Using system.data.sqlclient;

Using system.Web;

Using system.Web.SessionState;

Using system.Web.ui;

Using system.Web.ui.webcontrols;

Namespace NestedRepeater

{

Public class nestedrepeater: system.Web.ui.page {

Protected system.Web.ui.WebControls.repeater PARENTREPEATER

Public NestedRepeater ()

{

Page.init = New System.EventHandler (Page_init);

}

Public void Page_Load (Object Sender, Eventargs E)

{

// Create The Connection and DataAdapter for the authors Table.

SqlConnection CNN = New SQLCONNECTION ("Server = (local); database = pubs; integrated security = sspi;");

Sqldataadapter cmd1 = new sqldataadapter ("Select * from authors", CNN);

// CREATE AND FILL THE DATASET.

DataSet DS = New DataSet ();

Cmd1.fill (DS, "Authors");

// CREATE A Second DataAdapter for the title.

Sqldataadapter cmd2 = new SqldataAdapter ("Select * from titleauthor", cnn);

Cmd2.fill (DS, "Titles");

// Create The Relation BEWTWEEN TABLES.

DS.RELATIONS.ADD ("MyRelation",

DS.Tables ["authors"]. Column ["au_id"],

DS.Tables ["Titles"]. Columns ["au_id"]);

// bind the authors table to the parent reteater control, and call data.

PARENTREPEATER.DataSource = DS.TABLES ["authors"];

Page.DATABIND ();

// close the connection.

CNN.Close ();

}

Private void Page_init (Object Sender, Eventargs E)

{

InitializationComponent ();

}

Private vidinitiRizeComponent ()

{

This.Load = New System.EventHandler (this.page_load);

}

}

}

Back to the top

References

For More Information, Refer to the Following Topics in The Microsoft .NET Framework Software Development Kit (SDK):

Add a rellationship Between Tables

Http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpConaddingReranceShipBetwentWotables.asp

Navigating a ratetp: //msdn.microsoft.com/library/default.asp? URL = / library / en-us / cpguide / html / cpConnavigatingReranceShipShiPbetWeentWotables.asp

REPEATER Web Server Control

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconrepeaterwebserverControl.asp

Back to the top

转载请注明原文地址:https://www.9cbs.com/read-43844.html

New Post(0)