Welcome to DotNetManiac

Repeater Control: Nested Repeaters

Date Revised: August 13 2009

Visual Studio 2008 | C#

This shows how to use nested Repeater controls in asp.net to display hierarchical data. You can apply this concept to other list-bound controls.

Step 1. I have created a simple data source for this demo using an Access 2000 database. The data is a one-to-many relationship that I want to display in a hierarchical order. For this demo I have added the database to the App_Data directory.

relationships

Step 2. Add the following markup code to your asp.net page. This consists of two repeater controls that are nested. The trick is to add the DataSource in the nested or child repeater. This is bound to a method that returns a DataReader containing child links when you pass it the primary key from the first or parent repeater, in this case the SectionID.


<asp:Repeater ID="repeaterParent" runat="server">
            <ItemTemplate>
                <strong>
                    <%# Eval("Section").ToString() %></strong><br />
                <asp:Repeater ID="repeaterChild" runat="server" DataSource='<%# LinksDataReader(Eval("SectionID").ToString()) %>'>
                    <ItemTemplate>
                        <a href="#">
                            <%# Eval("LinkText").ToString() %></a><br />
                    </ItemTemplate>
                </asp:Repeater>
            </ItemTemplate>
        </asp:Repeater>

Step 3: Add the following code to you code beside page.

using System;
using System.Data;
using System.Data.OleDb;
using System.Web.UI;
 
public partial class _Default : System.Web.UI.Page
{
    private const string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\Database1.mdb;";
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string sqlSelectSections = "SELECT [SectionID], [Section] FROM SECTIONS ORDER BY [Section];";
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                OleDbCommand command = new OleDbCommand(sqlSelectSections, connection);
                command.CommandType = CommandType.Text;
 
                connection.Open();
                repeaterParent.DataSource = command.ExecuteReader(CommandBehavior.CloseConnection);
                repeaterParent.DataBind();
            }
        }
    }
 
    public OleDbDataReader LinksDataReader(string sectionID)
    {
        string sqlSelectLinks = "SELECT [LinkText] FROM LINKS WHERE [SectionID]=" + sectionID + " ORDER BY LinkText;";
 
        OleDbConnection connection = new OleDbConnection(connectionString);
        OleDbCommand command = new OleDbCommand(sqlSelectLinks, connection);
        command.CommandType = CommandType.Text;
        connection.Open();
        return command.ExecuteReader(CommandBehavior.CloseConnection);
    }
}

Live Demo

(these are not real links)

ASP
ASP.net 4.0
Classic ASP
C#
Best Practices
VB.net
101 Applications
Programmers Reference
vs C#
Welcome to DotNetManiac