Is it possible to stop an ItemCommand or control event from bubbling up the control hierarchy? I have a control that contains child user controls that may or may not already have code that handles their ItemCommand events. What I would like to do is to allow the child control to decide to not pass the ItemCommand along to the parent control in the event it already processes the event in its code-behind.
Thanks,
Mike
What is a bubbled event? Handling child controls events through parent control in data bind controls known as event bubbling. That means, when events occurred in child controls , the event is bubbled to the parent control.
Simply calling a parent control's event from a child control is called Event bubbling. Handling child controls events through parent control in data bind controls known as event bubbling. Server controls like Datagrid, DataList, and Repeater can have other child controls inside them.
In your user control class, override OnBubbleEvent(). If you return true, you will stop the "bubble up" to the parent controls.
protected override bool OnBubbleEvent(object source, EventArgs args)
{
//handled
return true;
//uncomment line below to bubble up (unhandled)
//return base.OnBubbleEvent(source, args);
}
Another somewhat neat thing to think about that I found while tinkering on this, which might be useful in some instances... you can change the command name that 'bubbles up' in the control heirachy as well. In your child user control, use OnCommand, rather than Onclick.
So, say you have a button in your user control, change the code from this:
<asp:button id="mySpecialButton"
onClick="mySpecialButton_OnClick" runat="server">
to this:
<asp:Button id="mySpecialButton"
CommandName="mySpecialCommand"
CommandArgument="myArgument"
OnCommand="mySpecialButton_Command"
runat="server"/>
then in the codebehind,
protected void mySpecialButton_Command(object sender, CommandEventArgs e)
{
RaiseBubbleEvent(this, new CommandEventArgs("Handled", e));
}
Thus, in your parent control's ItemCommand handler you will then get this new command name rather than the original command name from the child control, which you can do with as you see fit.
The Click event technically is not bubbling. That event is raised and handled by your code but the list control is also watching the Click event and raising an ItemCommand event. There's no way to prevent that because you can't even guarantee which event handler will be called first.
Do you have a CommandName associated with the button that you don't want raising that event? You should probably get rid of your Button_Click event entirely and do your command handling in the ItemCommand event, checking the event args for the CommandName and reacting appropriately.
In other words, use a CommandName that identifies what you want to happen, then in the ItemCommand event, only take action when you see a CommandName that you are responsible for handling.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With