adding user control programatically

  • Thread starter Thread starter francois
  • Start date Start date
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
 
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,
 
Back
Top