Problems with event bubbling

G

Guest

1. I've got a default user control class with an event handler 'OnBubbleEvent':

public class DefaultUserControl : System.Web.UI.UserControl
{
public event EventHandler DoBubbling;
...
protected void OnBubbleEvent(EventArgs e)
{
DoBubbling(this, e);
}
...
}

2. Every user control class deriving from this default class may call that
event handler:

public class DerivedUserControl : DefaultUserControl
{
...
private void MyList_SelectedIndexChanged(object sender, System.EventArgs e)
{
OnBubbleEvent(e);
}
...
}

3. In my main page all user controls derived from the default class are
loaded dynamically:

public class MainPage : System.Web.UI.Page
{
...
private void LoadControl()
{
DefaultUserControl myControl;
if (this.Session[nameOfControl] == null)
{
myControl = (DefaultUserControl) this.LoadControl(nameOfControl);
this.Session[nameOfControl] = myControl;
myControl.ID = myControl.ToString();
}
else
{
myControl = (DefaultUserControl) this.Session[nameOfControl];
}
myControl.DoBubbling += new EventHandler(this.FetchBubble);
this.MyPanel.Controls.Add(myControl);
}
...
private void FetchBubble(object sender, EventArgs e)
{
this.ActivateControl();
}
}

The 'MyList_SelectedIndexChanged' event handler calls the 'OnBubble' event
handler which in turn calls the 'FetchBubble' event handler in the main page.
BUT then the 'FetchBubble' event handler is called again... Why does this
happen?

If I load and display my user controls dynamically: when should this happen?
Do I have to add the event handler ('myControl.DoBubbling += new
EventHandler(this.FetchBubble);') each time?

Many thanks in advance,
P.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top