【问题标题】:ASP.NET - How to propagate events with nested user controlsASP.NET - 如何使用嵌套用户控件传播事件
【发布时间】:2011-08-18 22:23:33
【问题描述】:

我在页面上有嵌套控件。例如:

Page 
  -ChildControl1 (of type AllOfMyItems)
      -ChildControl2 (of type ListOfItems)
         -ChildControl3 (of type MyItem <- this one fires event)

当子控件引发事件时,我希望页面处理该事件。有什么好方法可以做到这一点?

Events and delegates 在这里使用似乎是个好主意。

所以我试图在我的页面的 Page_Init 上执行以下操作:

 MyItem.SomethingHappened += doStuff();

有什么好的方法可以做到这一点?

【问题讨论】:

    标签: asp.net events delegates delegation


    【解决方案1】:

    我假设您无法从您的页面直接访问 ChildControl2,这就是您的问题。

    一种选择是将事件从一个 UserControl 冒泡到它的父 UserControl,依此类推,直到您的页面处理 ParentControl.SomethingHappened 事件。如果用户控件并不总是像本例中那样嵌套,则建议使用这种方式。

    【讨论】:

      猜你喜欢
      • 2012-12-31
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 2011-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多