在ASP.NET中将事件鼓励到父级 [英] Bubbling Events to Parent in ASP.NET
问题描述
页面
用户控件1
用户控制2
控制3
我想要做的就是控制3 (它可以是任何种类的控制,我想这样做一般)有用户做一些事情触发回发,它起泡一些事件到用户控制2,甚至可能甚至用户控制1(虽然我可以有UC 2手动泡泡事件)。
再次,我想通常这样做,以便层次结构可以更改,它仍然可以工作。也许有多个控件(控件4等)或数据绑定控件。这是可能的吗?
感谢。
进入asp.net
查看: http://www.4guysfromrolla.com/articles/051105-1.aspx
基本上,要提高你想要冒泡的事件:
RaiseBubbleEvent(this,args);
然后抓住它:
protected override bool OnBubbleEvent(object source,EventArgs e){
bool processed = false;
如果(e是TemplatedListCommandEventArgs){
TemplatedListCommandEventArgs ce =(TemplatedListCommandEventArgs)e;
OnItemCommand(ce);
processed = true;
}
返回处理;
}
如代码所示,如果此方法返回false,则事件将继续起泡控制层次结构
RaiseBubbleEvent
的实现由Control提供,不能超过
。 RaiseBubbleEvent将层次结构中的
事件数据发送到
控件的父级。要处理或
提高冒泡事件,控件
必须覆盖OnBubbleEvent
方法。
从MSDN: http://msdn.microsoft .com / en-us / library / aa719644(v = vs.71).aspx
I have say this hierarchy in ASP.NET:
page
user control 1
user control 2
control 3
What I want to be able to do is that when control 3 (it could be any kind of control, I want to do this generically) has the user do something with it that triggers a postback, it bubbles up some event to user control 2, or maybe even user control 1 (though I could have UC 2 manually bubble the event too).
Again, I want to do this generically, so that the hierarchy can change and it still works. Maybe there are multiple controls (control 4, etc.) or a data bound control. Is this possible?
Thanks.
Event Bubbling is built into asp.net
Check this out: http://www.4guysfromrolla.com/articles/051105-1.aspx
Basically, to raise the event that you want bubbled up:
RaiseBubbleEvent(this, args);
And then to catch it:
protected override bool OnBubbleEvent(object source, EventArgs e) {
bool handled = false;
if (e is TemplatedListCommandEventArgs) {
TemplatedListCommandEventArgs ce = (TemplatedListCommandEventArgs)e;
OnItemCommand(ce);
handled = true;
}
return handled;
}
As the code implies, if this method returns false, the event will continue to bubble up the control hierarchy
The implementation of RaiseBubbleEvent is provided by Control and cannot be overridden. RaiseBubbleEvent sends the event data up the hierarchy to the control's parent. To handle or to raise the bubbled event, a control must override the OnBubbleEvent method.
From MSDN: http://msdn.microsoft.com/en-us/library/aa719644(v=vs.71).aspx
这篇关于在ASP.NET中将事件鼓励到父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!