Click here to Skip to main content
12,254,879 members (63,705 online)
Click here to Skip to main content
Add your own
alternative version


4 bookmarked

Nested Repeater Controls in ASP.NET

, 19 Mar 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
How to use nested Repeater controls to display hierarchical data


This article describes how to use nested Repeater controls to display hierarchical data. You can apply this concept to other list-bound controls. This article only shows you how to implement nested Repeaters and will not attempt to explain Repeaters in general.


I am going to demonstrate a simple ASP.NET web application that will display a questionnaire using nested Repeaters. In this example the first Repeater will display the sections of the questionnaire and the second Repeater will display the subsections and the third one will display questions. We have set two templates for questions: Descriptive and Yes/No questions, the template will change dynamically according to the question type.

Using the code 

<asp:Repeater ID="rptrParent" runat="server" OnItemDataBound="rptrParent_ItemDataBound">
<div style="padding: 10px 0px 10px 0px;">
<asp:Label ID="Label5" CssClass="label" Font-Bold="true" Font-Size="13pt" runat="server"
Text='<%#Eval("SectionName") %>'></asp:Label>
<asp:HiddenField ID="hdnSecID" runat="server" Value='<%#Eval("SectionID") %>' />


In the BindQuestionnaire method the sections of the questionnaire will bind to the first Repeater and in the ItemDataBound event of the first Repeater, the sub sections and questions will bind to the second and third Repeaters. On the questions ItemDataBound event the templates of the questions will change dynamically according to the question type.  

private void BindQuestionnaire()
    CQuestionnaire objQst = new CQuestionnaire();
    List<AQuestionnaire> objList = new List<AQuestionnaire>();
    objQst.QID = 14;
    objList = objQst.SelectQSections();
    rptrParent.DataSource = objList;

protected void rptrParent_ItemDataBound(object sender, RepeaterItemEventArgs e)
    CQuestionnaire objQst = new CQuestionnaire();
    HiddenField hdnSec = e.Item.FindControl("hdnSecID") as HiddenField;
    Repeater rpChild = e.Item.FindControl("rptrQuestionnaire") as Repeater;
    rpChild.DataSource = objQst.SelectQSubSections(Convert.ToInt32(hdnSec.Value));

    foreach (RepeaterItem repeaterItem in rpChild.Items)
        CQuestionnaire objQst1 = new CQuestionnaire();
        // Databinding with Nested Repeater Control  
        string sSubID = ((HiddenField)(repeaterItem.FindControl("hdnSubID"))).Value;
        ((Repeater)(repeaterItem.FindControl("rptrQuestionS"))).DataSource = 

protected void rptrQuestionS_ItemDataBound(object sender, RepeaterItemEventArgs e)
    CQuestionnaire objQst = new CQuestionnaire();
    HiddenField hdnQstnTyp = e.Item.FindControl("hdnQstnType") as HiddenField;
    Label lblQstNo = e.Item.FindControl("lblQstNo") as Label;
    Label lblQstNo1 = e.Item.FindControl("lblQstNo1") as Label;            
    HtmlGenericControl dvDS = (HtmlGenericControl)e.Item.FindControl("dvDesc");
    HtmlGenericControl dvYN = (HtmlGenericControl)e.Item.FindControl("dvYN");
    if (hdnQstnTyp.Value.ToString() == "1")
        dvYN.Visible = true;
        dvDS.Visible = false;
        lblQstNo1.Text = (e.Item.ItemIndex + 1).ToString();
        dvYN.Visible = false;
        dvDS.Visible = true;
        lblQstNo.Text = (e.Item.ItemIndex + 1).ToString();


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Nidhin Das K
Software Developer BEO SOFTWARES
India India
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionThanks by your code Pin
Member 115098719-Mar-15 2:30
memberMember 115098719-Mar-15 2:30 
GeneralMy vote of 1 Pin
Member 1042935610-Dec-13 1:03
memberMember 1042935610-Dec-13 1:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 19 Mar 2013
Article Copyright 2013 by Nidhin Das K
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid