I’m working on an ASP.NET page, using VB.NET and I have this hierarchy:
Page A
– Web User Control 1
– Web User Control A
– Web User Control B
– Web User Control C
I need to raise an event from Web User Control B that Page A will receive (the event flow will be Web User Control B -> Web User Control 1 -> Page A).
My only approach so far has been this: 1) Add a custom event declaration to both Web User Control B and Web User Control 1 and simply RaiseEvent twice until it gets to Page A (this seems ugly and I don’t particularly like it).
My other idea was to create a custom Event class that inhertis from some magical base Event class and create an instance of it in both Web User Control B and Web User Control 1, but that is proving fruitless because I can’t find any event base classes (maybe b/c they’re aren’t any, since it appears to be a keyword, not a class name).
Any help would be appreciated! Thanks and happy coding!
You can use the BubbleEvent concept to do this. A BubbleEvent goes up the control hierarchy until someone handles it. The GridView and Repeater controls do this with their Row/ItemCommand events.
You could implement it into WebUserControl1, turning it into a standard event for the page (like the GridView does):
Or, do it directly in the page. UserControlB (Child) is the same as above, and UserControl1 (Parent) doesn’t need to do anything special – OnBubbleEvent defaults to returning False, so the event bubbles up:
If your initial event is from a server control (like a Button.Click), then it will have been coded to already raise the bubble event – so UserControlB (Child) doesn’t need to do anything to get that to the parent either. You just need to call RaiseBubbleEvent for any of your custom events, or if you want to transform the EventArgs in some way.