

Navigator : Home > Tutorials > Controls Tutorials > ...
To display hierarchical data using ASP.NET 2.0 and C#
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; |
Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.
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(); } |
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"]); |
The front Nestedrepeater .aspx page looks something like this:
Try Server Intellect for Windows Server Hosting. Quality and Quantity!
<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> |
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!
If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.