Dynamically loading user controls in C# Web Application rather than website

1.9k Views Asked by At

I am trying to create my own website management framework similar to DNN/SiteFinity (only a mucho cut down version).

One of the big (but common) features is to be able to dynamically add user controls to provide additional functionality.

I would prefer to use a pre-compiled .NET "Web Application" over a dynamically compiled "Website".

What are the implications of using LoadControl method to dynamically add a user control for a pre-compiled web applications with specific regard to:

  1. Session (preume I am using the stateserver)
  2. What happens to all the other users - will they be logged out?
  3. Application pool recycling due to changes to some compilation??

I am not sure I have ever dynamically added a user control to a pre-compiled web applications as I have mainly been using "Websites" so I am making a large presumption that it will work because if the website is pre-compiled, does this mean the dynamically added user control will be dynamically compiled as required even though the site is pre-compiled?

EDIT

I also forgot to mention that the user controls will be outside of the application directory and so won't be published during web deploy - they will be uploaded via another website.

Thanks! Dan.

2

There are 2 best solutions below

1
On

You can dynamically add user control with in your precompiled website.

I give a description about user control life cycle:

Every control on the page has a unique ID called the ClientID. (That's an actual property on each control.) A user control is a "naming container". That means each control it contains also contains the ID of the User control. For example, "TextBox1" in UserControl with the ID "UserControl1" will have the clientID: UserControl1_TextBox1.

Post back retrieves the values from each field by the ClientID. For example, to get the above TextBox's value from the form, use Request.Forms["UserControl1_TextBox1"]. Now what happens when you replace one UserControl with another that has the same ClientID? The second gets the data of the first.

Recommendations:

  1. Always assign a unique ID to the UserControl in its ID property.

  2. You should always recreate the controls of the page you sent to the browser on postback before rearranging them. That allows the original controls to load their data and run their event handlers. It also allows the ViewState to properly distribute its contents without getting a ViewState corrupted error.

  3. So on Post_Back, I recommend calling LoadControl to get the original UserControl. Then before loading the second, set the first's Visible property to false to remove it.
12
On
  1. No issue on session
  2. No
  3. No

My only suggestion is you read:

http://weblogs.asp.net/infinitiesloop/archive/2006/08/25/TRULY-Understanding-Dynamic-Controls-_2800_Part-1_2900_.aspx

http://weblogs.asp.net/infinitiesloop/archive/2006/08/30/TRULY-Understanding-Dynamic-Controls-_2800_Part-2_2900_.aspx

http://weblogs.asp.net/infinitiesloop/archive/2006/08/30/TRULY-Understanding-Dynamic-Controls-_2800_Part-3_2900_.aspx

http://weblogs.asp.net/infinitiesloop/archive/2006/10/16/TRULY-Understanding-Dynamic-Controls-_2800_Part-4_2900_.aspx

The fact you're currently using a 'Web Site' rather than a web Application doesn't change your ability to add controls dynamically. Tho it would be rather beneficial if MS just removed 'Web Site' altogether since it creates nothing but problems. Thats a different argument for another day tho.