ASP.NET: Accessing Nested Repeater Control

The ASP.NET Repeater control is a handy data-bound list control that allows you to create a custom layout by repeating a specified template for each item displayed in the list. You can nest a repeater control inside the other to create advanced layouts.

A frequently asked question about the Repeater control is accessing nested repeater controls from the parent control and providing a DataSource dynamically. It’s quite easy as shown here.

Declare the Nested Repeater controls as shown below. Observe the ItemDataBound event on the parent Nested Repeater

ASP.NET Nested Repeater

In the code-behind, handle the rptParent_ItemDataBound event and provide a DataSource to the Child repeater.

protected void rptParent_ItemDataBound(object Sender, 
RepeaterItemEventArgs e) 
  if ((e.Item.ItemType == ListItemType.Item) ||
    (e.Item.ItemType == ListItemType.AlternatingItem))
     // inner repeater:
     Repeater child = (Repeater)e.Item.FindControl("rptChild");

     // Programmatically providing a datasource
     child.DataSource = dsSomeDataSource;

That’s it. For each item, the ItemDataBound event gets fired and you will get the desired results.

About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.


Leon said...

You could also put a datasource inside the parent repeater which uses a control as selectparameter.
this control is inside the first repeater and can be invisible.
then, the inner repeater will use this datasourceid...

ijazsh7 said...

Hi Suprotim,

I am unable to bind the datasource for the inner repeater:


It says Datasource is an invalid property for childrepeater.

Please help me out, what's the issue in this case.

I appreciate your help. thanks