views:

398

answers:

3

So I have a number of user control dynamically added to a page, and user interaction with these dynamically added user controls need to be handled by the control's parent.

  1. Is this what is referred to as "event bubbling"?

  2. How do I do that in VB?

Thanks in advance.

A: 

First, you have several complex issues here and my initial thought is that you should separate them and attack each on its own until you have it working and then add them into the final solution. For instance, don't try to start off by dynamically creating your user control and getting it to expose and fire an event. The dynamic part of this problem could get in the way and cause your events not to work but you would never know which is the problem. Instead my recommendation is you focus on events in VB.NET and getting them to work from a statically created user control. Then once you have this all working, move to making the user control dynamically created.

As far as understanding events in VB.NET, I highly recommend the MSDN samples. Start with this simple example: How to: Raise an Event (Visual Basic) and then follow through with the other samples that are linked to from that page. Then once you have learned creating your own events in VB.NET, then look into adding a usercontrol dynamically.

Event bubbling is when a parent control handles an event from a child. So, yes, this is what you are trying to do.

Jeff Widmer
Yes, precisely. Normally I am not too fond of the MSDN help library, but this one was spot on. Thanks.
A: 

I am not sure of VB but in c# you can use following

 id.event+=eventhandler yourEvent;
Vinay Pandey
It seems as if the equivavent in VB is: Addhandler myUserControl, AddressOf Me.parentEventHandler
A: 

My first response to this kind of question is always this - why are you dynamically adding the controls? Could they go into a repeater (where this becomes a lot simpler), or must they be programatically handled?

Paddy
Paddy, the answer to your question is: I am going to build a web based interface (GUI) for home automation. At design time I do not know how many zones a user has activated - this is infered from information in a database, so I need to be able to resolve this dynamically - or programatically if you like.
Indeed. In this case you could put your UserControl/set of controls within a repeater and bind this repeater to a list of 'zones'. I find that there are not too many cases when dynamically adding controls is necessary/beneficial.
Paddy