asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search





Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

Sharing Controls Across Different Web Sites Without Using DLLs
By Scott D. Smith
Rating: not yet rated
Rate this article


  • email this article to a colleague
  • suggest an article


  • download source code
  • Introduction

    One of the most frequent gripes most programmers have about ASP.NET 2.x and 3.x is that you can't implement include files across several web sites. Remember the good old days in Classic ASP when you could just put a reference to a virtual file in the code and it would automagically work? In ASP.NET, Microsoft recommends that you use DLLs to share code, but this can be hazardous and complex, especially when updating DLLs in a production environment. It would be much easier and intuitive if you could share source code files in ASP.NET. Some people like to gerry-rig the App_Code folder to do this, but that can cause more problems than it solves.

    I recently wanted to share a piece of code that makes a database call with several other web sites. After Googling around for awhile, I didn't find anything that was exactly what I was looking for, so I got creative and came up with the solution described here. Just like in Classic ASP, this can be done easily using virtual directories. (To be fair, there is a Microsoft support page that describes something similar, but they make it way too complicated.)

    Summary

    To share a control across different web projects, you simply:

    1. Create a control in a new web site.
    2. Configure IIS properly to handle the control in ASP.NET.
    3. Create a virtual directory in the web site that wants to use the shared control.
    4. Register the control in any webpage that wants to use it and instantiate the control in the form.
    5. Use the control at will.

    Detailed instructions are provided below.

    Getting Started: Create a Shared Control

    The first thing you need to do is create a new web site. But there's no need to create a solution or anything like that.

    Open up Visual Studio and select New Web Site from the File menu. Create whichever type of web site you want.

    New Web Site

    Next, you need to configure IIS properly. This involves setting creating an application and setting it to ASP.NET 2.x or 3.x.

    Configure IIS

    Configure IIS

    Then you need to create a simple ASCX file (user control) called GetInfo. To avoid compilation errors that can happen when you make changes in a production environment, I strongly recommend that you keep the code in a single file.

    Configure IIS

    Here's the code for my simple user control. You pass it an account and a company and it returns a SQL statement that can be used by any web project on the webserver.

    <%@ Control Language="VB" ClassName="GetInfo" %>

    <script runat="server">
        ' CHANGING THIS CODE WILL CHANGE EVERY WEBPAGE THAT DISPLAYS ALLERGIES
        Function GetAllergies(ByVal Account As String, ByVal Company As String)
            GetAllergies = "exec sp_GetAllergiesNEW '" + Account + "','" + Company + "'"
        End Function

    </script>

    Sharing the Control with an Existing ASP.NET Web Site

    In order to use a shared control in another web site, you first need to configure a virtual directory using IIS.

    1. Open IIS and right click on the folder containing the desired website. (This is presumably a different website on the same webserver.)
    2. Select New->Virtual Directory... and specify the name and location of the directory (e.g. SharedCodeVB in C:\Inetpub\wwwroot\SharedCodeVB).
    3. Modify the properties of the new virtual directory so it is an ASP.NET directory.

    This includes creating an application and setting it to ASP.NET 2.x or 3.x, as explained earlier.

    Using the Shared Control on a Web Page

    1. To use the control in a specific web page, put a register statement similar to this at the top:

      <%@ Register TagPrefix="uc1" TagName="SharedCodeVB" Src="SharedCodeVB/GetInfo.ascx" %>

      NOTE: The Visual Studio compiler may complain that this control does not exist. Since these websites are not compiled, it doesn't matter.

    2. Put an instance of the shared control somewhere in the <form>.

      <form id="form1" runat="server">
      <uc1:SharedCodeVB id="SharedCodeVB1" runat="server" />

    3. Call the function in the shared control like this:

      Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
          Dim strSQL As String = SharedCodeVB1.GetAllergies("012345", "MAIN")
          Label1.Text = strSQL
      End Sub

    Happy Programming!

    Additional Reading

    Attachments

    About the Author

    Scott D. Smith has a degree in operating systems at Chico State University (CSU). He has been a database administrator and .NET applications programmer for 7+ years at Prairie Cardiovascular Consultants. He is a Graduate Assistant for the Dale Carnegie program based out of Quincy, Illinois. His Quality Treatment Plan web pages have been demonstrated around the world by Dr. James T. Dove, M.D., president of the American College of Cardiology (ACA). If you have questions or comments about this article, email Scott at ssmith(at)prairieheart.com.

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Other Articles
    Jul 21, 2005 - N-Tier Web Applications using ASP.NET 2.0 and SQL Server 2005 - Part 1
    While the .NET Framework made building ASP.NET applications easier then it had ever been in the past, .NET 2.0 builds on that foundation in order to take things to the next level. This article shows you to how to construct an N-Tier ASP.NET 2.0 Web application by leveraging the new features of ASP.NET 2.0 and SQL Server 2005.
    [Read This Article]  [Top]
    Apr 28, 2005 - New Files and Folders in ASP.NET 2.0
    With the release of ASP.NET 2.0, Microsoft has greatly increased the power of ASP.NET by introducing a suite of new features and functionalities. As part of this release, ASP.NET 2.0 also comes with a host of new special files and folders that are meant to be used to implement a specific functionality. This article examines these new files and folders in detail and provides examples that demonstrate how to utilize them to create ASP.NET 2.0 applications.
    [Read This Article]  [Top]
    Mar 10, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 2, Cont'd
    Alex Homer continues his detailed look at the major changes to the DataSet class. In this part, he looks at two features that allow developers to work with data in a more structured and efficient way when using the DataSet with a SQL Server 2005 database server.
    [Read This Article]  [Top]
    Mar 9, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 2
    Alex Homer continues his detailed look at the major changes to the DataSet class. In this part, he looks at two features that allow developers to work with data in a more structured and efficient way when using the DataSet with a SQL Server 2005 database server.
    [Read This Article]  [Top]
    Mar 3, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 1, Cont'd
    In this article, Alex Homer looks at the changes between the version 1.x and version 2.0 DataSet and their associated classes, showing you how you can take advantage of the new features to improve your applications' capabilities and performance.
    [Read This Article]  [Top]
    Mar 2, 2005 - The DataSet Grows Up in ADO.NET 2.0 - Part 1
    In this article, Alex Homer looks at the changes between the version 1.x and version 2.0 DataSet and their associated classes, showing you how you can take advantage of the new features to improve your applications' capabilities and performance.
    [Read This Article]  [Top]
    Feb 16, 2005 - Writing a Custom Membership Provider for the Login Control in ASP.NET 2.0
    In ASP.NET 2.0 and Visual Studio 2005, you can quickly program custom authentication pages with the provided Membership Login controls. In this article, Dina Fleet Berry examines the steps involved in using the Login control with a custom SQL Server membership database.
    [Read This Article]  [Top]
    Dec 29, 2004 - ClickOnce Deployment in .NET Framework 2.0
    In this article, Thiru Thangarathinam examines .NET 2.0's new ClickOnce deployment technology that is designed to ease deployment of Windows forms applications. This new technology not only provides an easy application installation mechanism, it also eases deployment of upgrades to existing applications.
    [Read This Article]  [Top]
    Dec 15, 2004 - A Sneak Peek at ASP.NET 2.0's Administrative Tools
    With ASP.NET 2.0, Microsoft has made great strides in increasing developer productivity and has made implementing previously complex solutions relatively easy. Where this version of ASP.NET really shines, however, is in its new administrative tools that allow developers to spend less time managing the configuration of the servers and software and more time developing great code.
    [Read This Article]  [Top]
    Nov 17, 2004 - The ASP.NET 2.0 TreeView Control
    Thiru Thangarathinam introduces ASP.NET 2.0's new TreeView control which provides a seamless way to consume and display information from hierarchical data sources. The article discusses this new control in depth and explains how to use this feature rich control in your ASP.NET applications.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info

    Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers