Finding a dynamically added control in a third nested repeater

519 views Asked by At

I have three repeaters. Lets call them R1, R2 and R3.

In R3, I create a bunch of controls in Page_Init. For example, one of those controls has the name WMC_image.

Im using this code to get R2:

Page.FindControl("R1").Controls[1].FindControl("R2")

This works fine. But when I add the code to get R3:

Page.FindControl("R1").Controls[1].FindControl("R2").Controls[1].FindControl("R3")

I get the error:

"System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: index".

How come I get this error? And how do I fetch the HtmlGenericControl inside of R3?

EDIT: Sorry but I forgot to add that only R1 get filled DataBind() in the Page_Init. The other two Repeaters gets filled in the OnItemDataBound event.

When I loop out all controls in R1, I get a bunch of hits. But I get nothing from R2 even tho' I can see them on my site.

1

There are 1 answers

0
msarchet On

Instead of doing it in one line you need to do this in multiple lines:

You'll probably want to add some error checking as well

Control R1;
Control R2;
Control R3;    

if (Page.HasControls()) {
  R1 = Page.FindControl("R1").Controls[1]
  if ( R1.HasControls()) {
    R2 = R1.FindControl("R2").Controls[1]
    if (R2.HasControls()) {
      R3 = R2.FindControl("R3")
    }
  }
}