Events not firing on dynamically added UserControls

J

James Gerdes

I am trying to dynamically add a user control to a Custom Control that
inherits a Panel. The control renders fine on the web form. The
problem is that none of the user control's events are firing (button
clicks, etc.) on the server. The page is posting back, but none of
the user control's event delegates are being hit.

I am trying to load the User Control in the PreRender of the Custom
Control (I have tried putting this in different places with no
success).

Any help would be hugely appreciated.

Thanks,

James

Here's the code for the web form on which I'm trying to instantiate
the custom control:

public class WebForm3 : System.Web.UI.Page
{
private WebCustomControl1 cc;

private void Page_Load(object sender, System.EventArgs e)
{
//instantiate the custom panel
cc = new
WebCustomControl1("~/WebUserControl1.ascx");
//add the panel to this page's form
this.Controls[1].Controls.Add(cc);
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web
Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new
System.EventHandler(this.Page_Load);
}
#endregion
}

And here's the entire code for my custom control:

public class WebCustomControl1 : Panel
{
public WebCustomControl1(string url)
{
m_PlaceHolder = new PlaceHolder();
m_Url = url;
}

protected override void OnPreRender(EventArgs e)
{
this.m_PlaceHolder.Controls.Add(Page.LoadControl(m_Url));
this.Controls.Add(m_PlaceHolder);
base.OnPreRender (e);
}
}
 
V

Victor Garcia Aprea [MVP]

Hi James,
That's the problem. PreRender is too late as event firing logic has already
passed by and your usercontrol didn't existed at that time. You need to add
them earlier, ie: Init.

This[1] may give you some tips.

[1] http://weblogs.asp.net/vga/posts/23498.aspx

--
Victor Garcia Aprea
Microsoft MVP | ASP.NET
Looking for insights on ASP.NET? Read my blog:
http://obies.com/vga/blog.aspx
To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
and not by private mail.

James Gerdes said:
I am trying to dynamically add a user control to a Custom Control that
inherits a Panel. The control renders fine on the web form. The
problem is that none of the user control's events are firing (button
clicks, etc.) on the server. The page is posting back, but none of
the user control's event delegates are being hit.

I am trying to load the User Control in the PreRender of the Custom
Control (I have tried putting this in different places with no
success).

Any help would be hugely appreciated.

Thanks,

James

Here's the code for the web form on which I'm trying to instantiate
the custom control:

public class WebForm3 : System.Web.UI.Page
{
private WebCustomControl1 cc;

private void Page_Load(object sender, System.EventArgs e)
{
//instantiate the custom panel
cc = new
WebCustomControl1("~/WebUserControl1.ascx");
//add the panel to this page's form
this.Controls[1].Controls.Add(cc);
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web
Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new
System.EventHandler(this.Page_Load);
}
#endregion
}

And here's the entire code for my custom control:

public class WebCustomControl1 : Panel
{
public WebCustomControl1(string url)
{
m_PlaceHolder = new PlaceHolder();
m_Url = url;
}

protected override void OnPreRender(EventArgs e)
{
this.m_PlaceHolder.Controls.Add(Page.LoadControl(m_Url));
this.Controls.Add(m_PlaceHolder);
base.OnPreRender (e);
}
}
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top