On the File menu, point to New, and then click Project.
Click Visual C# Projects under Project Types, and then click ASP.NET Web Application under Templates.
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.
In Solution Explorer, right-click the NestedRepeater project name node, point to Add, and then click Add Web Form.
To name the Web Form, type
NestedRepeater, and click Open.
The new Web Form is created. It opens in Design View 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.
Change the ID property of this Repeater control to parentRepeater.
Switch to the HTML view for this Web Form. To do so, click
the HTML tab in the lower-left corner of the Designer. The Repeater control generates the following HTML code:
In Solution Explorer, right-click NestedRepeater.aspx, and then click View Code to switch to the NestedRepeater.aspx.cs 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 create a connection to the Pubs database, and then to 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 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 environment.
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 that
the page works thus far.
After you set the DataSource property for the child Repeater control, the HTML code for the two Repeater controls (parent and child) appears as follows:
Add the following page directive to the top of the page:
<%@ Import Namespace="System.Data" %>
In the code-behind page, replace the following line in the Page_Load event
//Insert code in step 4 of the next section here.
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 adds the Titles table to the DataSet, and then adds the relationships between the Authors and Titles
tables.
Save and compile the application.
View the page in the browser, and then verify that the page
works so far. The output should appear as follows:
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);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");
//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.
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);
}
}
}
Need More Help? Contact a Support professional by E-mail, Online or Phone.
Customer Service For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more.
Newsgroups Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.