Event Bubbling in CompositeDataBoundControl
I was looking with Reflector at the GridView control the other day, realizing how it received click events for the buttons defined in the GridView. Whenever a button is clicked, it has a command associated with it. This command event argument is bubbled up to the GridView, which is handled by a private method. This method determines which event needs to be raised and which code needs processed, such as the editing, updating, or cancelling events. This occurs with each control handling bubbling, as such:
protected override bool OnBubbleEvent(object source, EventArgs args)
{
if (args is CommandEventArgs)
{
base.RaseBubbleEvent(source, args);
return true;
}
return false;
}
The returned value determines whether to keep raising the bubble event. Each time it is called, it is handled by the protected OnBubbleEvent method, which you can keep the bubbling process going, call another appropriate event, or cancel it.
In the parent composite data-bound control, you return false as to not continue raising the event; however, based on the command value, you can perform a specific action. For example, when the Edit button is clicked in the GridView, a command event argument for the linkbutton clicked (with the command name set to "Edit") is bubbled up the chain until it gets to the GridView, which handles the Edit event. This goes the same for all events.