306154 HOW TO: Use nested Repeater controls and Visual C # .NET display hierarchical data (from mkba)

xiaoxiao2021-03-06  76

The release number of this article has been CHS306154

For Microsoft Visual Basic .NET versions of this article, see

326338.

This article references the following Microsoft .NET Framework Class Bank Name Space:

System.data system.data.sqlclient

This task content

summary

Bind to the parent table to bind to the child table complete code list reference

Summary This article describes how to use nested

The REPEATER control displays a hierarchical data. This concept can be applied to another list binding control.

Back to top

Bind to the parent table

Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. Click the Visual C # item under the project type, then click ASP.NET Web Applications under Templates. In the location box, delete WebApplication # and type NESTEDRepeater. If you are using a local server, keep the server name http: // localhost constant. The following paths will be displayed in the location box: http: // localhost / nestedRepeater Click OK. In the Solution Explorer, right-click the NestedRepeater project name node, point to add, and then click Add Web Form. To rename a web form, type NESTEDREPEATER and click Open. Create a new web form. It will open in the Microsoft Visual Studio .NET integration development environment (IDE) design view. From the toolbox, select the REPEC control, then drag it to the web form page. Change the ID attribute of this Repeater control to "ParentrePeater". HTML view to switch to the web form. To do this, click the HTML tab in the lower left corner of the designer. The REATER control generates the following HTML code: controls will generate the following HTML code: Add the following code to the REPEATER tag:

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

After completion, the REPEATER HTML code will look like this:

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

Control The following HTML code: In "Solution Explorer", right-click NestedRepeater.aspx, and then click View Code to switch to the NestedRepeater.aspx.cs code hide file. The control will generate the following HTML code: add the following namespace declaration to the top of the file: use system.data;

Using system.data.sqlclient; control will generate the following HTML code: Add the following code to the Page_Load Event to create a connection to the Pubs database, then 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 so that it is suitable for your environment. The control will generate the following HTML code: Save all files. The control will generate the following HTML code: On the Generative menu, click Generate Solution to compile the project. The control will generate the following HTML code: View the .aspx page in the browser, and verify that the page is still working. The output should be as follows: 172-32-1176 213-46-8915 238-95-7766 267-41-2394 ...

Back to top

Bind to child table

In the HTML view of the NestedRepeater.aspx page, find the following code rows: <% # dataBinder.eval (Container.DataItem, "AU_ID")%>
Add the following code after the above code:

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

This new code adds the second REPEATER control to the ItemTemplate property of the parent Repeater control. Set the DataSource property of the subreate control, as follows:

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

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

Set the top below to add the following page instruction to the top of the page: <% @ import namespace = "system.data"%> Setting the child in the code hidden page, put the following line in the Page_Load event : // INSERT CODE IN Step 4 of the next section. Replace with the following code: // Create a Second DataAdapter for the title table.

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"]);

This will add the Titles table to the DataSet, then add the relationship between the Authors table and the Titles table. Set the child to save and compile the application. Set the child to view the page in the browser and verify that the page is still working. The output should be as follows: 172-32-1176 PS3333 213-46-8915 bu1032 bu 2015 238-95-7766 PC1035 267-41-2394 bu1111 TC7777 ...

Back to 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 DIPEC

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 top

Refer to more information, see the following topics in the Microsoft .NET Framework Software Development Kit (SDK):

Adding a Relationship between Tables (add relationships between tables) http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconaddingrelationshipbetweentwotables.asp Navigating a Relationship between Tables (relationship between the browser table) http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconnavigatingrelationshipbetweentwotables.asp Repeater Web server control (Repeater Web server control) Http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconrepeaterwebcontrol.asp

Back to top

The information in this article applies to:

Microsoft ASP .NET (provided with .NET Framework) Microsoft Visual C # .NET (2002) Recent Update: 2002-9-27 (1.0) Keywords_ik KBDataBinding Kbdsupport KBGRPDSASP KBHOWTO KBHOWTOMASTER KBSERVERCONTROLS KB306154

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

New Post(0)