ASPAlliance.com : The #1 ASP.NET Developer Community : How To Share User Controls Across Applications
ASPAlliance.com : The #1 Active Server Pages .NET Community The #1 ASP.NET Community
Search   Search

Subscribe   Subscribe

Powered by ORCSWeb Hosting


Site Stats


Powered By ASP.NET
 
Featured Sponsor

Featured Columnist


Featured Book
XML and Web Services Unleashed
XML and Web Services Unleashed

Find Prices
Sample Chapter
Sample Chapter
Sample Chapter


New! asp.netPRO

We publish our articles in the standard RSS format.

Powerful .NET Email Component

Code Sharing Software
<- Old Dog, New Tricks Printer Friendly Version 

How to Share User Controls Across Applications

By Jim Ross

User Controls are a great convenience, and one of the first uses many of us found for them was in creating standard heading and footer layouts for our web pages. For those of us who work in a corporate environment, or who contract to companies that want multiple web sites created, the logical next step is to create a central location from which all web sites can share a common set of controls. Such an arrangement would allow, for example, a rapid change in the appearance of all websites that share the common set of controls. And of course, as soon as you have an easy way to share User Controls, all kinds of other "standard" functionality can be easily shared as well.

Unfortunately, for various reasons, first attempts at sharing User Controls across websites did not work. But now a way has been found to do such sharing. This article will show you how. As it has turned out, the method is fairly simple,  but does have some limitations, and places some constraints on its use.

The steps to share User Controls across multiple applications are:

  1. Create a directory on the web server to hold the shared controls.

  2. For each website that will use the shared controls, use Internet Services Manager to create a virtual directory that points to the shared control directory. Unfortunately, neither Visual Studio.NET nor WebMatrix seem to have a way to make this directory show up in your web project's folders. At least, this writer hasn't found a way yet.

  3. Before closing Internet Services Manager, open the Properties dialog for the virtual directory you just created and make sure that the "Application Settings" for the virtual directory have  been cleared so that there is no application associated with it.


Figure 1 - Make sure Application Settings have been cleared.
Do not "Create" an application for the Virtual Directory

  1. Now that you have the virtual directory configured for the website you will need to do some preparation work on your User Controls before they can be successfully shared. There are two things you must do in order to successfully share them from this virtual directory:

    • First, do not use code-behind. This is because you do not want these controls to have to be compiled into a project, they need to be able to stand alone. If you have code associated with the control, it must be included within the ASCX file, NOT in code behind. Also, make sure that the control's declaration does not reference any code behind. The reference should look like this:

      <%@ Control Language="vb" AutoEventWireup="false"
          ClassName="NCG_PageHead" %>
  • Second, make sure that the language specified on your User Control is the same as the language of the pages where it will be used. This is the same as it has always been with User Controls. Because of this, if your controls must support more than one language, then you will need a version of each control for each supported language.

That is it. Now any website that is provided with a Virtual Directory mapped to the common User Control directory can use those controls.

Limitations

We have already noted the limitations of this technique, but it won't hurt to summarize them here for the record

  • No code-behind. All code needed by the control must be inline within the ASCX file.

  • There must be separate versions of each control for each supported language.

  • Neither VB.NET nor WebMatrix will display the virtual directory within the project tree, so you won't be able to drag and drop the controls onto your pages, but will have to code the references by hand. The reference must point to the virtual directory as the location of the control. For example:

<%@ Register TagPrefix="NCG" TagName="Head"
    Src="NCGTemplates/NCG_PageHead.ascx" %>

One Caveat

Be careful that the only files you copy into the shared virtual directory are the ASCX files for the controls and any supporting files such as images or style sheets. ESPECIALLY, do NOT copy a web.config into that directory, as it will cause attempts to load the controls to crash.

We feel that these limitations are very easy to live with, considering the advantages that come from being able to share User Controls.

<- Old Dog, New Tricks Printer Friendly Version 
 Copyright © 2000-2003 ASPAlliance.com  Page Rendered at 7/23/2008 7:46:30 PM