In this tutorial, we will demostrate how to use nested Repeater control to display hierarchical data using ASP.NET 2.0 and C#. You can apply this usage to the other data binding controls as well. For instance, to let DataGrid nest DataGrid, DataList nest DataList etc.
The Repeater Web server control is a container control that allows you to create custom lists out of any data that is available to the page. The Repeater control does not have a built-in rendering of its own, which means that you must provide the layout for the Repeater control by creating templates. When the page runs, the Repeater control loops through the records in the data source and renders an item for each record. First, import the System.Data.SqlClient namespace. The System.Data.SqlClient namespace is the.NET Framework Data Provider for SQL Server. The.NET Framework Data Provider for SQL Server describes a collection of classes used to access a SQL Server database in the managed space.
| using System.Data.SqlClient; |
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!
Then, add a new web form to the solution, name it Nestedrepeater.aspx, and create a connection to the sample database pubs. Binding the table of Authors to Repeater Control.
protected void Page_Load(object sender, EventArgs e) {
//Create the connection and DataAdapter for the Authors table. SqlConnection cnn = new SqlConnection("server=(local);database=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");
//Bind the Authors table to the parent Repeater control, and call DataBind. parent.DataSource = ds.Tables["authors"]; Page.DataBind();
//Close the connection. cnn.Close(); } |
I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.
In the Page_Load, add the child table data binding, and create relationship between the tables of author and title.
//Create a second DataAdapter for the Titles table. SqlDataAdapter cmd2 = new SqlDataAdapter("select * from titles,titleauthor,authors where titles.title_id=titleauthor.title_id and authors.au_id=titleauthor.au_id", 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"]); |
Try Server Intellect for Windows Server Hosting. Quality and Quantity!
The front Nestedrepeater .aspx page looks something like this:
<body> <form id="form1" runat="server"> <fieldset> <legend>Nestedrepeater</legend> <table> <tr> <td><b>Show the author and his works from the databases of pubs</b></td></tr> <tr> <td align="center"> <asp:repeater id="parent" runat="server"> <itemtemplate> <b><%# DataBinder.Eval(Container.DataItem,"au_lname") %></b><br> <asp:repeater id="child" datasource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("myrelation") %>' runat="server"> <itemtemplate> <%# DataBinder.Eval(Container.DataItem, "[\"title\"]")%><br> </itemtemplate> </asp:repeater> </itemtemplate> </asp:repeater></td></tr> </table></fieldset> </form> </body> |
Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!
The flow for the code behind page is as follows.
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient;
public partial class Nestedrepeater : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
//Create the connection and DataAdapter for the Authors table. SqlConnection cnn = new SqlConnection("server=(local);database=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 titles,titleauthor,authors where titles.title_id=titleauthor.title_id and authors.au_id=titleauthor.au_id", 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(); } } |
Looking for the VB.NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!