Author: wincheer from: Asp.Net Chinese Professional Network
Introduction This article describes how to use nested REPEATER controls to display hierarchical data. Of course, you can also apply this technology to other list binding controls, such as the DataGrid contains a combination of DataGrid, DataList, including DataList.
Bind to the parent table
1. Add a new web form to the application project, named NESTEDREPEATER.ASPX. 2. Trust from the toolbox to this page, set its ID attribute to Parent. 3. Switch to the HTML view. 4. Select the following
itemtemplate>
5. Open NestedRepeater.aspx.cs This code is separated. Add the column code to the Page_Load event, its function is to create a Pubs (this database is a demo database for SQL Server. In addition, this database will also install this database) Database connection, and bind the Authors table To the REPEATER control
public void Page_Load () {SqlConnection cnn = new SqlConnection ( "server = (local); database = pubs; uid = sa; pwd =;"); SqlDataAdapter cmd1 = new SqlDataAdapter ( "select * from authors", cnn); DataSet DS = new dataset (); cmd1.fill (DS, "authors"); // This will be inserted into the data binding parent.dataSource = DS.TABLES ["Authors"]; cnn. CLOSE ();
6. Add the following namespace Using System.Data.SqlclClient; 7. Change the connection string according to your own situation 8. Save and compile the application 9. Open this page in the browser, output the result Similar to the format below
172-32-1176 213-46-8915 238-95-7766 267-41-2394 ...
Bind to child table
1. In the HTML view of the page, add the following code. Its purpose is to increase the child Repeater control to the project template of the parent Repeater to form nested.
itemtemplate> asp: REPEATER>
2. Set the DataSource attribute of the sub-Repeater control:
itemtemplate> ask: repeater> itemtemplate > Asp: repeater> form> body> html> 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 yourprojectname {public class nestedrepeater: System.Web.UI.Page {protected System.Web.UI.WebControls .Repea Ter Parent; Public NestedRepeater () {page.init = new system.eventhandler (Page_init);