To display hierarchical data using nested repeater controls

Source: Internet
Author: User
Tags format bind eval
Controls | data | Show INTRODUCTION

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.


<itemtemplate>
<b> <%# DataBinder.Eval (Container.DataItem, "au_id")%> </b> <br>
</itemtemplate>

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


172-32-1176
213-46-8915
238-95-7766
267-41-2394
...

Binding to child tables

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.

<asp:repeater id= "Child" runat= "Server"
<itemtemplate>
<%# DataBinder.Eval (Container.DataItem, "[\" title_id\ "]")%> <br>
</itemtemplate>
</asp:repeater>

2. Set the DataSource property of the child repeater control:

<asp:repeater. Datasource= ' <%# ((DataRowView) Container.DataItem)
. Row.getchildrows ("myrelation")%> ' >

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:

172-32-1176
PS3333
213-46-8915
BU1032
BU2075
238-95-7766
PC1035
267-41-2394
BU1111
TC7777
...

The complete code

NestedRepeater.aspx
<%@ Page language=c# inherits= "Yourprojectname.nestedrepeater"%>
<%@ Import namespace= "System.Data"%>

<body>
<form runat=server>

!--Start Parent Repeater-->
<asp:repeater id= "Parent" runat= "Server"
<itemtemplate>
<b> <%# DataBinder.Eval (Container.DataItem, "au_id")%> </b> <br>

!--Start Child repeater-->
<asp:repeater id= "Child" Datasource= ' <%# ((DataRowView) Container.DataItem)
. Row.getchildrows ("myrelation")%> ' runat= ' server ' >
<itemtemplate>
<%# DataBinder.Eval (Container.DataItem, "[\" title_id\ "]")%> <br>
</itemtemplate>
</asp:repeater>
!--End Child Repeater-->

</itemtemplate>
</asp:repeater>
!--End Parent Repeater-->

</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);
}
}
}

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.