3

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.

Dan B
  • 936
  • 2
  • 13
  • 26
  • I would recommend that you look into the Portal Framework that comes built into ASP.Net. It allows you to create modular web sites, which is kind of similar to what you are attempting to do. It might be worth looking at it to see if it fits your needs, so that you don't have to reinvent the wheel. It also allows for personalization, so that different users can have customized views of the web site. – Waleed Al-Balooshi Dec 28 '10 at 10:21
  • Your feedback is not really addressing the question. I have actually built in jquery what MS does with their portal with full fancy drag and drop with dynamically loading HTML editors. – Dan B Jan 11 '11 at 22:11

2 Answers2

1

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.
Kashif
  • 14,071
  • 18
  • 66
  • 98
AsifQadri
  • 2,388
  • 1
  • 20
  • 30
  • Do I need to pre-compile my user controls before adding them to a pre-compiled site - the user controls will be outside of the web application directory. – Dan B Dec 28 '10 at 10:04
0
  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.

Phill
  • 18,398
  • 7
  • 62
  • 102
  • Super articles - but I don't think they address the question of dynamically loading user controls into a pre-compiled web application. – Dan B Dec 27 '10 at 22:38
  • I should have mentioned that I have dynamically loaded user controls in the past ok into a "website" so I am comfortable with how to do it. I'm more concerned with how to do it properly in a specific type of deployment. – Dan B Dec 27 '10 at 22:39
  • They help you understand how the controls are being loaded. If you had a user control, that was like a calculator, and you dynamically loaded it, the events wouldn't just be magically raised on those controls. If you read those articles you will understand why and how, without needing to come back to Stack Overflow for the 30 questions you will need to ask when you run into all the issues of dynamically loading controls. – Phill Dec 27 '10 at 22:42
  • You can do what you've been doing in the past then, in a Web Application. – Phill Dec 27 '10 at 22:43
  • I have only been using websites in the past not web applications but now i am switching to web applications and thinking before I jump which is why i am checking the impact of such decisions first. – Dan B Dec 28 '10 at 10:02
  • There's a lot of reasons to use Web App's over Web Site's. The most important one of all is the fact Web App's play nice with Source Control, Web Site's don't because they are based on the file system. So by default referenced assemblies are part of your bin directory. Causes all sorts of problems. Theres HEAPS of reasons why its better to move to Web App tho. – Phill Dec 28 '10 at 10:05
  • So with a web application that is pre-compiled on a server I can just use LoadControl to reference a user control in a folder outside the directory? Doesn't the loaded user control need to be compiled at some point? – Dan B Dec 28 '10 at 10:09
  • If you create a web app, and you have a page, and a user control. In your page you load that user control. That user control is already compiled as part of your app. If you have two apps. App 1 has a page, App 2 has a user control. If the page loads the user control, it will require the assembly from App 2 so that it has the compiled code-behind. – Phill Dec 28 '10 at 10:19
  • I am planning on uploading the user controls to a directory outside of the web application, then reference this folder by means of a virtual directory. So the user control won't be compiled. So I guess, so how does that work with a web application (precompiled) with a user control loading that is not compiled. Is ASP.NET clever enough to dynamically compile that one user control when requested for a web application (pre-compiled)? – Dan B Dec 28 '10 at 11:31
  • Sound's like what you're trying to do is overly complicated. If you uploaded 'web site' files with the codebehind, it would compile itself. If you used Web App files, you could upload the assembly with them and dynamically load it into the app domain if it wasn't loaded already. – Phill Dec 28 '10 at 11:39
  • I'm just trying to do what DNN and sitefinity does as standard only a lot more simple. At the moment, I am looking at pre-compiling user controls or creating a virtual path provider to server user controls from the database (but I think this requires full trust which I am uneasy about). – Dan B Dec 28 '10 at 16:37
  • If it helps, DNN isn't dynamically compiled, it loads the assembly into the app domain. – Phill Dec 28 '10 at 21:26