Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I cancel event bubbling in ASP.net?

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

like image 660
mclark1129 Avatar asked Nov 30 '09 21:11

mclark1129


People also ask

What is event bubbling in asp net?

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.

What method do you implement to allow a server control to participate in event bubbling?

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.


2 Answers

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.

like image 92
Morgan Avatar answered Oct 13 '22 18:10

Morgan


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.

like image 33
Josh Avatar answered Oct 13 '22 18:10

Josh