asp.net nesting repeater

Source: Internet
Author: User
Tags bind eval insert visual studio
asp.net

Originally not how to use repeater, but recently to display a message board the same thing, with grid implementation is not good to see, grid is more suitable for the formal web system, for the type of Web site, or with repeater, encounter the need to nest repeater situation, the collection, It may be used in the future.
Original from ms:http://support.microsoft.com/default.aspx?scid=kb;en-us;306154

Iew products, this is article applies to.
Article id:306154
Last Review:july 15, 2004
revision:4.1

This is article was previously published under Q306154
On this Page
SUMMARY
Bind to the Parent Table
Bind to the child Table
Complete Code List
NestedRepeater.aspx
Nestedrepeater.aspx.cs
REFERENCES
Applies To

SUMMARY
This article describes you to use the nested Repeater controls to display hierarchical data. Can apply this concept to the other List-bound controls.

Back to the top

Bind to the Parent Table
1. Start Microsoft Visual Studio. NET.
2. On the File menu, point to New, and then click Project.
3. Click Visual C # Projects under Project Types, and then click ASP.net Web application under Templates.
4. In the Location box, delete the webapplication#, and then type NestedRepeater. If you use the local server, leave the server name as http://localhost. The following path appears in the Location box:
Http://localhost/NestedRepeater
Click OK.
5. In Solution Explorer, right-click the NestedRepeater Project name node, point to Add, and then click Add Web Form.
6. To name the Web Form, type NestedRepeater, and click Open.
7. The new Web Form is created. It opens in the Integrated Development Environment (IDE) of Microsoft Visual Studio. NET. From the Toolbox, select the Repeater control, and then drag it to the Web Form page.
8. Change the ID of this Repeater control to Parentrepeater.
9. Switch to the HTML view of this Web Form. To does so, click the HTML tab in the Lower-left corner of the Designer. The Repeater control generates the following HTML code:
<asp:repeater id= "Parentrepeater" runat= "Server" ></asp:Repeater>


ADD the following code in the Repeater tags:
<itemtemplate>
<b><%# DataBinder.Eval (Container.DataItem, "au_id")%></b><br>
</itemtemplate>

The HTML code for the Repeater is as follows:
<asp:repeater id= "Parentrepeater" runat= "Server" >
<itemtemplate>
<b><%# DataBinder.Eval (Container.DataItem, "au_id")%></b><br>
</itemtemplate>
</asp:Repeater>


Solution Explorer, right-click NestedRepeater.aspx, and then click View Code to switch to the NESTEDREPEATER.ASPX.C s Code-behind file.
Add the following namespace declaration to the top of the file:
Using System.Data;
Using System.Data.SqlClient;


ADD the following code to the Page_Load event to the "Create a connection to the" Pubs database, and then to bind the Author s 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);d atabase=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, the next section.
Bind the Authors table to the parent Repeater control, and call DataBind.
Parentrepeater.datasource = ds. tables["Authors"];
Page.DataBind ();

Close the connection.
Cnn. Close ();
}

Note:you may have to modify the database connection string as appropriate for your.


Save all of the files.
In Solution Explorer, right-click the NestedRepeater.aspx, and then click Set as Start Page.
On the "Build" menu click Build Solution to compile the project.
View the. aspx page in the browser, and then verify this page works thus far.

The output should appear as Follows: 172-32-1176
213-46-8915
238-95-7766
267-41-2394
• ...


Back to the top

Bind to the child Table
1. In the HTML view of the NestedRepeater.aspx page, locate the following line of code:
<b><%# DataBinder.Eval (C Ontainer. DataItem, "au_id")%></b><br>
      
Add the following code after This code:

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

This new code adds a second Repeater the ItemTemplate property of the parent Repeater control.
2. Set the DataSource property for the child Repeater control as follows:
<asp:repeater. Datasource= ' <%# ((DataRowView) Container.DataItem)
. Row.getchildrows ("myrelation")%> ' >

After your set the DataSource property for the "child" Repeater control, the HTML code for the two Repeater controls (parent and child) appears as follows:

<asp:repeater id= "Parentrepeater" runat= "Server" >
<itemtemplate>
<b>
<%# DataBinder.Eval (Container.DataItem, "au_id")%>
</b>
<br>
<asp:repeater id= "childrepeater" runat= "Server"
Datasource= ' <%# ((DataRowView) Container.DataItem)
. Row.getchildrows ("myrelation")%> ' >
<itemtemplate>
<%# DataBinder.Eval (Container.DataItem, "[\" title_id\ "]")%><br>
</itemtemplate>
</asp:Repeater>
</itemtemplate>
</asp:Repeater>


3. ADD The following page directive to the top of the page:
<%@ Import namespace= "System.Data"%>


4. In the Code-behind page, replace the following line in the Page_Load event
Insert code in step 4, the next section.

With the following code:
Create a second DataAdapter for the Titles table.
SqlDataAdapter cmd2 = new SqlDataAdapter ("select * from titleauthor", CNN);
Cmd2. Fill (ds, "titles");

Create the relation between the Authors and Titles tables.
Ds. Relations.Add ("Myrelation",
Ds. tables["Authors"]. columns["au_id"],
Ds. tables["Titles"]. columns["au_id"]);


This is adds the titles table to the DataSet, and then adds the relationships between the Authors and titles tables.

5. Save and compile the application.
6. View the page in the browser, and then verify this page works so far. The output should appear as follows:
172-32-1176
PS3333
213-46-8915
BU1032
BU2075
238-95-7766
PC1035
267-41-2394
BU1111
TC7777
...


Back to the top

Complete Code List
NestedRepeater.aspx
<%@ Page language= "C #" codebehind= "NestedRepeater.aspx.cs" autoeventwireup= "false" inherits= " Nestedrepeater.nestedrepeater "%>
<%@ Import namespace= "System.Data"%>

<body>
<form runat=server>

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

<!--start child repeater-->
<asp:repeater id= "childrepeater" Datasource= ' <%# ((DataRowView) Container.DataItem)
. Row.getchildrows ("myrelation")%> ' runat= ' server >

<itemtemplate>
<%# DataBinder.Eval (Container.DataItem, "[\" title_id\ "]")%><br>
</itemtemplate>
</asp:repeater>
<!--end 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 NestedRepeater
{
   public class NestedRepeater:System.Web.UI.Page
   {
      protected System.Web.UI.WebControls.Repeater parentrepeater;
      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; 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 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 Data Bind.
         Parentrepeater.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);
}
}
}



Related Article

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.