Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeater in Repeater

I have a repeater inside a repeater. Where the parent repeater is bound to a Datatble which has a column with a Datatable in it.

I would like to bind the child repeater to the datatable column in the parent repeater's datarow

Is this possible? i was thinking i could do this directly in the aspx file like:

DataSource="<%# DataBinder.Eval(Container.DataItem, "Products")%>" but it doesn't seem to work.

like image 842
bill Avatar asked May 27 '10 16:05

bill


People also ask

How do I use a repeater as a repeater?

In the parent repeater, attach a method to the OnItemDataBound event and in the method, find the nested repeater and data bind it. Always find it odd when ItemDataBound is used for this.

What is a repeater example?

There are several different types of repeaters; a telephone repeater is an amplifier in a telephone line, an optical repeater is an optoelectronic circuit that amplifies the light beam in an optical fiber cable; and a radio repeater is a radio receiver and transmitter that retransmits a radio signal.

What is repeater in ASP net with example?

The Repeater control is used to display a repeated list of items that are bound to the control. The Repeater control may be bound to a database table, an XML file, or another list of items. Repeater is a Data Bind Control. Data Bind Controls are container controls.


2 Answers

In the parent repeater, attach a method to the OnItemDataBound event and in the method, find the nested repeater and data bind it.

Example (.aspx):

<asp:Repeater ID="ParentRepeater" runat="server" OnItemDataBound="ItemBound">     <ItemTemplate>         <!-- Repeated data -->         <asp:Repeater ID="ChildRepeater" runat="server">             <ItemTemplate>                 <!-- Nested repeated data -->             </ItemTemplate>         </asp:Repeater>     </ItemTemplate> </asp:Repeater> 

Example (.cs):

protected void Page_Load(object sender, EventArgs e) {     if (!IsPostBack)     {         ParentRepeater.DataSource = ...;         ParentRepeater.DataBind();     } }  protected void ItemBound(object sender, RepeaterItemEventArgs args) {     if (args.Item.ItemType == ListItemType.Item || args.Item.ItemType == ListItemType.AlternatingItem)     {         Repeater childRepeater = (Repeater)args.Item.FindControl("ChildRepeater");         childRepeater.DataSource = ...;         childRepeater.DataBind();     } } 
like image 132
Anton Avatar answered Sep 22 '22 18:09

Anton


I would add a DataBinding event to the child repeater itself:

<asp:Repeater ID="parentRepeater" runat="server">     <asp:Repeater ID="childRepeater" runat="server"         OnDataBinding="childRepeater_DataBinding" /> </asp:Repeater> 

Then just implement it:

protected void childRepeater_DataBinding(object sender, System.EventArgs e) {     Repeater rep = (Repeater)(sender);      int someIdFromParentDataSource = (int)(Eval("ParentID"));      // Assuming you have a function call `GetSomeData` that will return     // the data you want to bind to your child repeater.     rep.DataSource = GetSomeData(int);     rep.DataBind(); } 

I prefer to do it at the control level instead of the ItemDataBound level so that if you ever have to remove controls or items within your templates you don't have to worry about looking for code in the parent controls that use it. It get's all localize witht he control itself. Plus you never have to do a FindControl.

If you want to replace a control in the future you can just delete it and your code will still work since it is all self contained. Using the ItemDataBound would cause your code to still compile but crash or act unexpectedly at runtime because of it's reliance on child controls.

like image 30
Kelsey Avatar answered Sep 26 '22 18:09

Kelsey