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
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; child.DataBind(); } }
That’s it. For each item, the ItemDataBound event gets fired and you will get the desired results.
Tweet
2 comments:
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...
Hi Suprotim,
I am unable to bind the datasource for the inner repeater:
childRepeater.Datasource=...
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
ijaz
Post a Comment