views:

534

answers:

4

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

A: 

Set e.Handled = true (if e is the name of your event).

JamesL
James,I can't seem to find the Handled property you mention. Currently I am trying to do this inside a user control's button click event. The method has the following signature: Protected Sub btnAddToCart_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddToCart.Clicke in this case is just EventArgs and it does not have a handled property. Is there somewhere else I should be trying to access this property?
Mike C
+1  A: 

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.

Josh Einstein
There is no mention in the question of a Click event, but of an ItemCommand. Which *does* bubble. Morgan's answer shows how to handle this.
Ruben
It was mentioned in a follow up to JamesL's answer. Yes Morgan's answer is better hence why it is voted up. StackOverflow is working as designed.
Josh Einstein
+7  A: 

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.

Morgan
A: 

Hi,

I am also facing the same issue. I have a click event in the master page and in the user control also has the same click event. When í click the user control's click its filring the parent click evenet and validating the entire form . Could you please suggest how to prevent this.

Vaishnavi

Vaishnavi