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:
<% # databinder.eval (container.dataitem, "au_id")%> b>
itemTemplate> After completion, the REPEATER HTML code will look like this:
<% # databinder.eval (container.dataitem, "au_id")%> b>
itemtemplate>
ask: REPEATER> 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")%> b>
Add the following code after the above code:
<% # DataBinder.eval (Container.DataItem, "[/" Title_ID / "]")%>
itemtemplate>
ask: REPEATER> 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 / "]")%> itemtemplate> ask: repeater> itemtemplate> asp: repeater> 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"%>