Dynamically Include (@reference) another Control or aspx file

  • Thread starter Thread starter Aamir Mahmood
  • Start date Start date
A

Aamir Mahmood

Hello,

Can I include a control or another aspx file into a container aspx file on
the basis of some parameters?\
Like I want ControlA to be inserted in page index.aspx if the submit form
has some specific value, and ControlB otherwise.

-
AM
 
yeap you can certainly add controls at run time (even the ones written as
user controls in .ascx)
you can use place holder or any other container to insert the control.
something like this.

if( a == 1)
{
MyUserControl1 mUC1 = new MyUserControl1();
myPlaceHolder1.Controls.Add(mUC1);
}
else
{
MyUserControl2 mUC2 = new MyUserControl2();
myPlaceHolder1.Controls.Add(mUC2);
}
--

Regards,

Hermit Dave (D'way)
http://hdave.blogspot.com
 
Hello Aamir,
Hello,

Can I include a control or another aspx file into a container aspx
file on
the basis of some parameters?\
Like I want ControlA to be inserted in page index.aspx if the submit
form
has some specific value, and ControlB otherwise.

You can use the Page.LoadControl method to dynamically loading a control, and then place it somewhere in yout Page.Controls collection. But be aware that there can be issues with dynamically loading controls due to the way viewstate is handled.
 
Back
Top