Event of a dynamically added user control doesn't fire

N

niberhate

I have MyUserControlClass.cs as follows. You will see that

(1) LoadMyControl loads MyUserControl1, its CancelButtonEventHandler
is delegated to myControl1_CancelButtonClicked.

(2) In myControl1_CancelButtonClicked, MyUserControl2 is loaded, and
its NewButtonClickEventHandler is delegated to
myControl2_NewButtonClicked.

CancelButtonClicked event fires with no problem and MyUserControl2 is
successfully loaded upon firing.

But, the New button's click event isn't fired, in other words,
myControl2_NewButtonClicked doesn't get executed.

Note that MyEventArgs has been correctly implemented. Any idea to
share? Thanks.

//////////////////////////////
public partial class MyUserControlClass : System.Web.UI.UserControl
{

protected void Page_Load(object sender, EventArgs e)
{
LoadMyControl();
}

protected void LoadMyControl()
{
placeHolder1.Controls.Clear();
var myControl1 = LoadControl("~/UserControls/
MyUserControl1.ascx") as UserControls_MyUserControl1;
myControl1.ID = "myControl1Id";
myControl1.CancelButtonClicked += new
CancelButtonClickEventHandler(myControl1_CancelButtonClicked);
placeHolder1.Controls.Add(myControl1);
}

// myControl1_CancelButtonClicked gets executed as expected.
protected void myControl1_CancelButtonClicked(object sender,
MyEventArgs e)
{
var myControl2 = LoadControl("~/UserControls/
MyUserControl2.ascx") as UserControls_MyUserControl2;
myControl2.ID = "myControl2Id";
myControl2.NewButtonClicked += new
NewButtonClickEventHandler(myControl2_NewButtonClicked);
placeHolder2.Controls.Clear();
placeHolder2.Controls.Add(myControl2);
}

// New button click event either doesn't fire or doesn't get
captured, and the following
// method doesn't get executed.
protected void myControl2_NewButtonClicked(object sender,
MyEventArgs e)
{
// Do something here.
}

}
 

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,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top