This article describes how to use nested Repeater controls to display hierarchical data. Of course, you can also apply this technique to other list-bound controls, such as a DataGrid containing a combination of datagrid,datalist including DataList, and so on.
Bind to Parent table
1. Add a new Web Form to the application project with the name NestedRepeater.aspx.
2. Repeater a control from the toolbox to this page, set its id attribute to parent.
3. Switch to HTML view.
4. Select the following <itemtemplate> code to copy to the Repeater control's corresponding location. Note that you use the Paste as HTML feature when pasting. These statements contain data binding syntax, which is simple.
5. Open Nestedrepeater.aspx.cs This code to detach the file. The following code is added to the Page_Load event to create a demo database to Pubs (this database is SQL Server). This database is also installed when the. NET Framework SDK is installed, and binds authors tables to repeater controls
public void Page_Load ()
{
SqlConnection cnn = new SqlConnection ("server= (local);d atabase=pubs;uid=sa;pwd=;");
SqlDataAdapter cmd1 = new SqlDataAdapter ("select * from Authors", CNN);
DataSet ds = new DataSet ();
Cmd1. Fill (ds, "authors");
The data binding where the child table will be inserted
Parent. DataSource = ds. tables["Authors"];
Page.DataBind ();
Cnn. Close ();
}
6. Add the following namespace to the head of the file
Using System.Data.SqlClient;
7. Modify the connection string according to your own situation
8. Save and compile the application
9. Open this page in the browser, the output is similar to the following format
1. In the HTML view of the page, add the following code. The goal is to add a repeater control to the project template of the parent repeater to form a nested set.
3. Add the following instructions at the top of the page (note that you are in the. aspx file):
<%@ Import namespace= "System.Data"%>
In the. cs file, replace the annotation section in the Page_Load with the data binding that will be inserted into the child table in the following code:
SqlDataAdapter cmd2 = new SqlDataAdapter ("select * from titleauthor", CNN);
Cmd2. Fill (ds, "titles");
Ds. Relations.Add ("Myrelation",
Ds. tables["Authors"]. columns["au_id"],
Ds. tables["Titles"]. columns["au_id"]);
4. Save and compile the application.
View the modified page in the browser. The display format is similar to the following format:
</form>
</body>
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.Repeater Parent;
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);d atabase=pubs;uid=sa;pwd=;");
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 Titles table.
SqlDataAdapter cmd2 = new SqlDataAdapter ("select * from titleauthor", CNN);
Cmd2. Fill (ds, "titles");
Create the relation bewtween the Authors and Titles tables.
Ds. Relations.Add ("Myrelation",
Ds. tables["Authors"]. columns["au_id"],
Ds. tables["Titles"]. columns["au_id"]);
Bind the Authors table to the parent Repeater control, and call DataBind.
Parent. DataSource = ds. tables["Authors"];
Page.DataBind ();
Close the connection.
Cnn. Close ();
}
private void Page_Init (object sender, EventArgs e)
{
InitializeComponent ();
}
private void InitializeComponent ()
{
This. Load + = new System.EventHandler (this. Page_Load);
}
}
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.