ASP.Net Repeater Nested Using C#

Updated on 10 Aug 2012,
Published on 14 Jul 2008

Repeater Databound Control in ASP.Net 2.0 can be nested within other Repeater control to display the related database items from the relational SQL Database using C# code. To work with nested Repeater Control in ASP.Net C# code you must be familiar with FindControl function that provides the functionality to get the server control within the ItemTemplate of the Repeater Control. Here we will use FindControl Function to bind the related products table to a nested Repeater by passing the categoryId of outer Repeater control with Data Items of category table. To explain the example in this tutorial of Nested Repeater Control we have used the Northwind database of SQL Server 2000.

Repeater Examples:

You can see the live samples and examples of Repeater Control from the following links:

Repeater FindControl

FindControl function is used to find any control by its server ID reference. Using FindControl function you can find the nested Repeater control from the ItemTemplate cell of any row of ASP.Net Repeater control. Not even nested Repeater Control you can find any server control placed inside the ItemTemplate by passing the ID of the server control.

To access the properties of the server control returned by the FindControl function you have to type cast that control into its WebControls class. For example to type cast the searched Repeater control using FindControl function you can use ((Repeater) object.FindControl("RepeaterID") ) .

HTML Source Code for ASP.Net nested Repeater Control

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <h4><%#DataBinder.Eval(Container.DataItem, "CategoryName") %></h4>
        <asp:HiddenField ID="hiddenCategoryID" 
            runat="server" 
            Value='<%#DataBinder.Eval(Container.DataItem, "CategoryID") %>' />
        <asp:Repeater ID="Repeater2" runat="server">
            <ItemTemplate>
                <%#DataBinder.Eval(Container.DataItem, "productName") %><br />
            </ItemTemplate>
        </asp:Repeater>
    </ItemTemplate>
    <SeparatorTemplate>
        <hr />
    </SeparatorTemplate>
</asp:Repeater>

ASP.Net C# code for Nested Repeater Control DataBinding

// SQL Select Command
SqlCommand mySqlSelect = new SqlCommand("select * from categories", mySQLconnection);

mySqlSelect.CommandType = CommandType.Text;

SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlSelect);

DataSet myDataSet = new DataSet();

mySqlAdapter.Fill(myDataSet);

// Repeater Control Databinding using Datasource
Repeater1.DataSource = myDataSet;

Repeater1.DataBind();

foreach (RepeaterItem repeaterItem in Repeater1.Items)
{
    // SQL Select Command
    mySqlSelect = new SqlCommand("select * from products where categoryId = @categoryId", mySQLconnection);

    mySqlSelect.CommandType = CommandType.Text;

    // Parameter passed as categoryID
    // to get products that belong to the passed categoryId
    // FindControl HiddenField used to retrieve the value of categoryId
    mySqlSelect.Parameters.Add("@categoryId", SqlDbType.Int).Value = ((HiddenField)repeaterItem.FindControl( "hiddenCategoryID" )).Value;

    mySqlAdapter = new SqlDataAdapter(mySqlSelect);

    myDataSet = new DataSet();

    mySqlAdapter.Fill(myDataSet);

    // Databinding with Nested Repeater Control
    ((Repeater)(repeaterItem.FindControl( "Repeater2" ))).DataSource = myDataSet;

    ((Repeater)(repeaterItem.FindControl( "Repeater2" ))).DataBind();

}

Output:

Get the output for above discussed code from the following link:

Repeater Nested

Continue to next tutorial: ASP.Net Repeater Table to learn how to customize the appearance of Repeater control using HTML table layout.

6 Responses to "ASP.Net Repeater Nested Using C#"
Thanks for this post... it really help me lot........
Nicolas
What I need to do if I want to find a control inside the the second repeater?? I have this problem for a long time and I cannot find an answer to this problem. I need to find a Label inside the second repeater. Im using VB.net Thanks for any help
Ezineasp.net
Hi Nicolas

You can try the following code sample to find the Label control placed inside the ItemTemplate region of nested Repeater control:

1. HTML code for nested repeaters:

<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<strong><%#Container.DataItem%></strong><br />
<asp:Repeater ID="Repeater2" runat="server">
<ItemTemplate>
<i><%#Container.DataItem%></i><br />
<asp:Label ID="Label1" runat="server"></asp:Label>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>

2. VB.Net code to find the Label control placed inside the nested repeater control:

Dim parentArray() As String = {"parent item1", "parent item2", "parent item3", "parent item4", "parent item5"}
Dim childArray() As String = {"child item1", "child item2", "child item3", "child item4", "child item5"}

Repeater1.DataSource = parentArray
Repeater1.DataBind()

For Each parentRepeaterItem As RepeaterItem In Repeater1.Items
Dim Repeater2 As Repeater = CType(parentRepeaterItem.FindControl("Repeater2"), Repeater)

Repeater2.DataSource = childArray
Repeater2.DataBind()

For Each childRepeaterItem As RepeaterItem In Repeater2.Items
Dim Label1 As Label = CType(childRepeaterItem.FindControl("Label1"), Label)
Label1.Text = "I found my nested label " + Label1.ClientID + "<br />"
Next

Next

In the above VB.Net code we have used the For Each loop over the RepeaterItem(s) of Repeater1. Inside the loop we have used the Repeater class object to get the reference to the nested Repeater control i.e. Repeater2. Then after binding the data to the nested repeater control we have again used the For Each loop over the RepeaterItems of nested Repeater2.

Likewise we used the Repeater class object to get the reference of Repeater2 in the outer loop, we have used the Label class object to get the reference of Label control placed inside the ItemTemplate of nested Repeater2. You can use this Label object according to your requirement. You can either assign a value to it or retrieve the value from it.

Hope this will help you.

Good Luck.
Thank you very much! this was the only working piece of text on the entire internet that explained how to get Nested repeaters to work. Tyvm! keep up the good work! :)
Shant
Hello its perfect, Thank you, but i would like to know how can i add nested repeater dynamically but using external XML, and how can i make it designed as i want. Thank you
Shannon
Of all the examples I've found, this was the clearest, and worked with stored procedure calls. Thank you!!
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email
  • HIRE EzineASP.Net Developers