使用转发器捕获嵌套用户控件中的按钮单击事件

时间:2012-01-03 15:56:16

标签: asp.net user-controls repeater nested-repeater

以下是我的页面&控制层次。

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>

我需要能够在按钮点击时刷新 Repeater2

任何指针都会非常有用。

1 个答案:

答案 0 :(得分:0)

将事件附加到单击按钮。在该事件中,您应该能够遍历控制层次结构以进入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;
}