Add a WebUserControl Control At Runtime

F

Fao, Sean

I would like to add a WebUserControl control at runtime in the
CreateChildControls() method, but it's not working like I had hoped.
This technique works fine for WebControls:

protected override void CreateChildControls()
{
Header hdr = new Header();
Footer ftr = new Footer();

Controls.Add(hdr);
Controls.Add(ftr);
}

Header and Footer are both WebUserControl's. How would I go about
adding these controls at runtime?

Thank you in advance,
 
F

Fao, Sean

Nevermind, found out that I needed to call the LoadControl() method on
WebUserControls rather than instantiating them:

protected override void CreateChildControls()
{
Header hdr = (Header) this.LoadControl("Controls/Header.ascx");
Footer ftr = (Footer) this.LoadControl("Controls/Footer.ascx");

Controls.AddAt(1, hdr); // Add header
Controls.AddAt(4, ftr); // Add footer
}
 

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