ASP.NET - 如何使用嵌套用户控件传播事件
我在页面上嵌套了控件。例如:
Page
-ChildControl1 (of type AllOfMyItems)
-ChildControl2 (of type ListOfItems)
-ChildControl3 (of type MyItem <- this one fires event)
当子控件引发事件时,我希望页面处理该事件。有什么好的方法可以做到这一点?
事件和委托似乎是在这里使用的好主意。
因此,我尝试在页面的 Page_Init 上执行以下操作:
MyItem.SomethingHappened += doStuff();
执行此操作的好方法是什么?
I have nested controls on a page. Ex:
Page
-ChildControl1 (of type AllOfMyItems)
-ChildControl2 (of type ListOfItems)
-ChildControl3 (of type MyItem <- this one fires event)
When child control raises an event, I want page to handle that event. What is a good way to do this?
Events and delegates seem like a good idea to use here.
So I'm trying to do the following on Page_Init of my Page:
MyItem.SomethingHappened += doStuff();
what is a good way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您无法从页面直接访问 ChildControl2,这就是您的问题。
一种选择是将事件从一个 UserControl 冒泡到其父 UserControl,依此类推,直到您的页面处理 ParentControl.SomethingHappened 事件。如果用户控件并不总是像本示例中那样嵌套,则建议使用这种方法。
I assume that you don't have direct access to ChildControl2 from your page and thats your problem.
One option is to bubble the event from one UserControl to it's parent UserControl and so on until your page handles the ParentControl.SomethingHappened event. This way is recommended if the usercontrols aren't always nested like in this example.