Search code examples
asp.netuser-controlsrepeaternested-repeater

Capturing button click event in nested user controls with repeaters


Following is my page & control hierarchy.

Page
  UserControl 1
    <Repeater1> //Contents of UserControl 1
      UserControl 2
        <Accordion> //Contents of UserControl 2
        <Header/>
        <Content>
           <Repeater2/> //Repeater 2 is within Usercontrol 2
        <Button/>    // Button is within Usercontrol2, not Repeater 2
         </Accordion>
     </Repeater1>

I need to be able to refresh the Repeater2 upon Button click.

Any pointers will be really helpful.


Solution

  • Attach an event to the click of the button. Inside that event, you should be able to traverse the control hierarchy to get to repeater2.

    Button theButton = sender as Button;
    UserControl2 parentControl = theButton.Parent as UserControl2;
    
    Repeater theRepeater = null;
    foreach(Control control in parentControl.Controls){
        theRepeater = control as Repeater;
        if(theRepeater != null) break;
    }