adding user control programatically

F

francois

Hi,

I would like to add a USER control (not custom control) programmatically to
my aspx page.
I am using something like the following in my page_load code :

*
Bos.UserControls.HeaderFinance header = new HeaderFinance();
this.Controls.Add(header);
*

The problem is that asp.net controls existing in my user control are not
instanciated and are still 'null', then of course it does not work.

Then is there a way to add a user control to a page programatically? Or at
least dynamically. I want to add different user controls on my page
depending of some runtime parameters.

Best regards,

Francois
 
S

Scott Allen

Hi Francois:

You'll want to use Page.LoadControl instead of calling the constructor
of the user control. For example:

Control c = LoadControls("HeaderFinance.ascx");
Controls.Add(c);

HTH,
 

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,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top