Navigator: Home - Advanced - Using Nested Repeaters to Display Hierarchical Data C#

Using Nested Repeaters to Display Hierarchical Data C#

This tutorial will show how we can create a relational hierarchical structure using nested repeaters. This is useful for such an implementation as a structured menu system with different levels (categories and sub-categories). C# version.

Download the Full Working Version of this Project written with Visual Studio.NET C# 2005 Here!

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

Repeaters are very powerful tools for displaying data, and while they are not the most feature-packed tool in the ASP.NET toolbox, they are arguably the most versatile.
One of the things we can do with Repeaters is nest them to show hierarchical data. In this example, we will display a menu system with sub-menu items, using two Repeaters. These menu items will be pulled from a Sample database.

First, we add the Connection String in Web.config:

<appSettings>
<add key="ConnectionString" value="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True" /> </appSettings>
<connectionStrings/>

We also need the following assembly references:

using System.Data;
using System.Data.SqlClient;

We add the code for the two nested Repeater Controls:

<form id="form1" runat="server">

<asp:Repeater ID="repMenu1" runat="server" OnItemDataBound="repMenu1_ItemDataBound">
<ItemTemplate>
<a href="#"><%#DataBinder.Eval(Container.DataItem, "menuItem")%></a>
<asp:Repeater ID="repMenu2" runat="server">
<HeaderTemplate><br /></HeaderTemplate>
<ItemTemplate><div style="margin-left:15px;"><a href="#"><%#DataBinder.Eval(Container.DataItem, "subMenuItem")%></a><br /></div></ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>

</form>

Notice the nested Repeater is in the <ItemTemplate> tags of the first.
Next, we add the following to the code-behind.

protected void ShowMenu()
{
SqlDataAdapter cmd = new SqlDataAdapter("SELECT * FROM [Table1];SELECT * FROM [Table2]", new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]));
DataSet ds = new DataSet();
cmd.Fill(ds);
ds.Relations.Add(new DataRelation("nestThem", ds.Tables[0].Columns["ID"], ds.Tables[1].Columns["parentMenuID"]));

repMenu1.DataSource = ds;
repMenu1.DataBind();
}

protected void repMenu1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
DataRowView dv = e.Item.DataItem as DataRowView;
if (dv != null)
{
Repeater repSubMenu = e.Item.FindControl("repMenu2") as Repeater;
if (repSubMenu != null)
{
repSubMenu.DataSource = dv.CreateChildView("nestThem");
repSubMenu.DataBind();
}
}
}

The above method should be called on Page_Load so that it can bind the data to the first repeater, which will retrieve the top-menu items from the database and populate the repeater. When menu1 is databound, it will find the matching submenu items in the database and bind them to the second (nested) repeater, displaying the sub-menu items below the parent.

The entire code-behind should look something like this:

using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ShowMenu();
}

protected void ShowMenu()
{
SqlDataAdapter cmd = new SqlDataAdapter("SELECT * FROM [Table1];SELECT * FROM [Table2]", new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]));
DataSet ds = new DataSet();
cmd.Fill(ds);
ds.Relations.Add(new DataRelation("nestThem", ds.Tables[0].Columns["ID"], ds.Tables[1].Columns["parentMenuID"]));

repMenu1.DataSource = ds;
repMenu1.DataBind();
}

protected void repMenu1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
DataRowView dv = e.Item.DataItem as DataRowView;
if (dv != null)
{
Repeater repSubMenu = e.Item.FindControl("repMenu2") as Repeater;
if (repSubMenu != null)
{
repSubMenu.DataSource = dv.CreateChildView("nestThem");
repSubMenu.DataBind();
}
}
}
}

Download the Full Working Version of this Project written with Visual Studio.NET C# 2005 Here!

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

411asp.net123aspxDotNetFreaksServer Intellect