Click here to Skip to main content
15,890,690 members
Articles / Web Development / IIS
Article

Sharing User Controls Between Projects

Rate me:
Please Sign up or sign in to vote.
1.26/5 (20 votes)
29 Nov 20043 min read 36.3K   11   4
How to share user controls between projects

Introduction

Recently I have stumbled on an interesting problem : I was working on a project and developed some user controls to use in this project.After a while i have started another project and wanted to use the same user controls for the new project.The problem was that i couldn't find a way to share the controls between the projects i a way that will separate the controls from the projects that use them.

Motivation

The reason that I needed this solution is because in a point of time I had 10 different projects using the same user controls.Each time I wanted to change something in one of the user controls I had to change it in all 10 projects and then recompile and redeploy them.This way I have one project containing the user controls , and every change in it applies to all the sites.
the reason that I prefer using user controls rather then converting them to server control is that sometime there are controls that are heavy and complex in presentation and event handling (several buttons, textboxes etc.) and transforming it to a server side control could be a real headache .

How to Do It

The solution to the problem was pretty simple :

first of all , i have separated the user controls to a different project.The new project contained only the User controls and files related to them.

The second step was connecting the user controls project to the projects that will use them.to do this, first of all we need to define a reference in each project ot the user controls project.the next step will be (and the most important step) to go to the iis and create a virtual directory under the project's web directory that will point to the directory containing the user controls (it shouldn't be defined as an application in IIS so make shure you remove the application after creating the virtual directory) . the final step is using the user controls in your code. this should be done by refering to them as if they are part of the project and placed in a subdirectory (according to the name we gave to the virtual directory in IIS).

that is all .

regarding the encapsulation of resources : the use of a virtual directory for mapping the user controls to the project folder solves this issue because when you try to use a resource , the request is handled according to the path from the request to the resource in the containing site. Which means : as long as you use relative paths to resources you'll be safe regarding resources inside the user controls project and event resources that are in the containing project (which is not recommended because it creates a dependency between the projects)

once we have done all of this , we are sharing the user controls among the projects.

One Minor Problem

There is one minor problem with this method (which is important and I forgot to mention in the article): every time you make a change in the code behind of the user controls , you should redeploy it's DLL among all the projects that use these controls. BUT : if you don't use code behind : you're safe(I do use - a lot), if you do use : it's a lot simpler to copy some DLLs then recompile and redeploy 10 projects !!!

enjoy.

 

 


 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Israel Israel
I work as a developer for the last 5 years. I am working with .NET since the early Beta versions.
Currently i work as a developer at The Ben-Gurion University in the Negev, At the Computation Center.
I work with C#,VB.NET,ASP.NET,JAVA and different DB systems (Oracle,SQL,CACHE,etc.)

My main interests are OOP,TDD and other general related stuff.

Comments and Discussions

 
QuestionThis approach with web deployment projects in VS 2005 Pin
Laith M. Zraikat27-Mar-06 1:07
Laith M. Zraikat27-Mar-06 1:07 
GeneralAdditional Questions... Pin
mariusosh29-Nov-04 4:20
mariusosh29-Nov-04 4:20 
GeneralRe: Additional Questions... Pin
mayafit29-Nov-04 4:59
mayafit29-Nov-04 4:59 
GeneralRe: Additional Questions... Pin
Dan Colasanti29-Nov-04 19:06
professionalDan Colasanti29-Nov-04 19:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.