Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested Repeater not being recognised in code behind

I have a nested repeater set up but the child repeater control is not being recognised in the code behind. It's not even being added to the designer file. I've tried this on an aspx and an ascx page but both gives the same problem.

<asp:Repeater ID="RepeaterParent" runat="server">
    <ItemTemplate>

        <asp:Repeater ID="RepeaterChild" runat="server">
        </asp:Repeater>

    </ItemTemplate>
</asp:Repeater>

with this on the page the code behind only recognises the RepeaterParent but not RepeaterChild.

Can anyone help me out here? Many thanks!

like image 729
Sid Avatar asked Dec 17 '10 13:12

Sid


2 Answers

Like any other control that is used within a repeater (or template) control, you need to retrieve the control using FindControl.

So, in your item data bind event handler for the parent, you would do:

var childRepeater = RepeaterParent.FindControl("RepeaterChild") as Repeater;
like image 84
Oded Avatar answered Oct 15 '22 06:10

Oded


The RepeaterChild will be accessible when you use FindControl("RepeaterChild") on the parent repeater I think. Can't remember the exact syntax.

Also note that the FindControl method will also take the context of the current item of the parent repeater, as the name you specify will repeat. Naming containers do some work in the background to provide unique naming, but it is hard to track sometimes.

like image 39
Adam Houldsworth Avatar answered Oct 15 '22 06:10

Adam Houldsworth