mcl*_*129 8 asp.net events user-controls
是否可以阻止ItemCommand或控件事件冒泡控制层次结构?我有一个控件,其中包含子用户控件,可能已经或可能没有处理其ItemCommand事件的代码.我想要做的是允许子控件决定在它已经在其代码隐藏中处理事件的情况下不将ItemCommand传递给父控件.
谢谢,
麦克风
小智 11
在用户控件类中,重写OnBubbleEvent().如果返回true,则会停止向父控件"冒泡".
protected override bool OnBubbleEvent(object source, EventArgs args)
{
//handled
return true;
//uncomment line below to bubble up (unhandled)
//return base.OnBubbleEvent(source, args);
}
Run Code Online (Sandbox Code Playgroud)
另一个有点整洁的事情是我在修补这个时发现的,这在某些情况下可能很有用......你可以在控件heirachy中更改"冒泡"的命令名称.在您的子用户控件中,使用OnCommand而不是Onclick.
因此,假设您的用户控件中有一个按钮,请更改以下代码:
<asp:button id="mySpecialButton"
onClick="mySpecialButton_OnClick" runat="server">
Run Code Online (Sandbox Code Playgroud)
对此:
<asp:Button id="mySpecialButton"
CommandName="mySpecialCommand"
CommandArgument="myArgument"
OnCommand="mySpecialButton_Command"
runat="server"/>
Run Code Online (Sandbox Code Playgroud)
那么在代码隐藏中,
protected void mySpecialButton_Command(object sender, CommandEventArgs e)
{
RaiseBubbleEvent(this, new CommandEventArgs("Handled", e));
}
Run Code Online (Sandbox Code Playgroud)
因此,在父控件的ItemCommand处理程序中,您将获得此新命令名称而不是子控件中的原始命令名称,您可以根据需要使用该名称.
| 归档时间: |
|
| 查看次数: |
5672 次 |
| 最近记录: |