Click here to Skip to main content
15,881,089 members
Articles / Web Development / ASP.NET
Article

Convert the web user controls into custom controls in MS VS 2005

Rate me:
Please Sign up or sign in to vote.
3.74/5 (23 votes)
8 Oct 2007CPOL5 min read 90.5K   669   33   29
Convert the web user controls into custom controls in MS VS 2005
Title:       Convert the web user controls into custom controls in MS VS 2005
Author:      Dimitar Nikolaev Madjarov
Email:       madjarov_d_n@yahoo.com

Introduction

All Dot.Net developers whose works over Web user interface soon or later they have to develop web user controls or custom
web controls to satisfy the complexity of modern web based systems. Everyone single developer know how difficult is to be done this.
Unlike the student book in the real world of software development process the controls which we develop become more sophisticated
and more complex with any new requirements and end-user functionality.
In case that we choose to develop a Web user control we are choose to use the designer of MS visual studio. By this way when we use
the designer of MS VS 2005 we definitely increase the developer's productivity and quality of the final user control.
Also when we use the designer ability of MS VS 2005 we improve and quality issuance because it is very easy for everyone
developer to test and see the result and control behavior in real time. This possibility arms the developers with better
opportunity to self-test his own code and fix the potential bugs in beginning. The Web User Controls has one big disadvantage.
They are not easy for distributing through big projects or implemented in other separate project. They are very hard to be capsulated.
This article has the single purpose to provide a possibility to everyone developer who use MS VS 2005 and Dot.Net v.2 to develop
Web user controls and convert them to custom web controls which are easy to be distributed and capsulated.

Let we begin

I will try to explain step by step the whole process of developing of two simple web user controls, testing them as user controls
converting them to custom custom controls and using them in separate web project.

1. In current article download file you may find next file "WebDeploymentSetup.msi". You have to install it as plug-in for MS VS 2005.
This file also may to by found in Microsoft Web site and download from there. This file gives the ability of MS VS 2005 to create
more complex deployment web projects.
2. After installation from step one open visual studio 2005 and create a simple web project. Set the Default.aspx page as start page.
3. Now is the right time to create two simple user controls. I demo downloads I create testUC1 and testUC2.
By default the MS VS 2005 will create an ASCX file with next declaration inside it:

Control Language="C#" AutoEventWireup="true" CodeBehind="testUC1.ascx.cs" Inherits="testUserControlsWebProject.testUC1"

and the same declaration about second web user control:
Control Language="C#" AutoEventWireup="true" CodeBehind="testUC2.ascx.cs" Inherits="testUserControlsWebProject.testUC2"

Please notice the sections in red color. For our goal we have to delete them and replace them with ClassName section as it shown below.
Control Language="C#" AutoEventWireup="true" CodeBehind="testUC1.ascx.cs" ClassName="DNM.testUC1"

Control Language="C#" AutoEventWireup="true" CodeBehind="testUC1.ascx.cs" ClassName="DNM.testUC2"

Here I set the "DNM" in front of the name of web controls. To modify the class name in Dot.Net 2 is a new feature
and I am doing this because the class name has to stay within a namespace of ours controls with name which is suitable for us.
In our example I choose "DNM" but you are free to set everyone name which you like to see in your control.

4. Now is time to put a some content of your controls and test in an ASPX page /Default.aspx/ to see are they work correctly.

5. After testing of our web user controls and we are sure that they works as they have to work is time to convert them into custom controls.
Firstly we have to add a web deployment project.
Please notice the fig No.1 and No.2.

Fig No.1 fig No.1

Fig No.2 fig No.2

6. Next step in our converting process is to setting-up our deployment project with correct settings.
Into item "Compilation" we have to uncheck the option: "Allow this precompiled site to be updated".
I am doing this because in update mode only files of code behind will be compiled. ASCX will be left.
Because our goal is to redistribute our DLLS/assemblies/ and for this purpose our DLLS has to be self-contained.
Into item "Output assemblies" we have to check the option: "Create a separate assembly for each page and control output"
And as last option into item "Deployment" we have to check option: "Remove App_Data folder from output location"
All of this actions are shown on figures No.3, 4 and 5.

Fig No.3 fig No.3

Fig No.4 fig No.4

Fig No.5 fig No.5

7. Now in this step we are ready to build the entire solution and generate our assemblies.
In bin folder of web deployment project you may found ours assemblies "App_Web_testuc1.ascx.cdcab7d2.dll" and "App_Web_testuc2.ascx.cdcab7d2.dll".
Exactly these assemblies are our redistributive DLLS. Our custom controls.
8. We are almost on the end of this article. We have to create now one separate Web Application Project which has to consume and use our converted
user controls. Create a usual web project and references to DLLS above.
9. Now the last step is manually to register the added assemblies in ASPX page where we like to use our converted controls. This is very easy step
and the code for this action is shown below:
Register Assembly="App_Web_testuc1.ascx.cdcab7d2" TagPrefix="T1" Namespace="DNM"


Register Assembly="App_Web_testuc2.ascx.cdcab7d2" TagPrefix="T2" Namespace="DNM"

Please notice this fragment Namespace="DNM". Here I use the exact namespace which I set in our web controls.
10. The last step is to register in our ASPX page these two controls and starts use them. For this goal I use next code:
T1:testUC1  id="mitkoTestControl1" runat="server" />
T2:testUC2  id="mitkoTestControl2" runat="server" />

We may run our project and start used our converted controls as regular custom controls.

NB: Attention

Because we develop self-contained control/standalone redistributed DLL/ it has not been depended from application global things as App_Code or global.asax.
Also we have to use a static images and resources. Our control is permitted to be dependant from other assemblies but they
have to be also available in bin folder of the web project which uses our converted controls.

Thanks

I would like to say a "big thank you guys" for all staff of Tenrox Inc. You are one of the best professionals with which I have been work ever.
I would like to send my gratitude to Tenrox Inc company for their efforts to provide a very interesting and challenging web projects which help me
to become better developer
.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) http://www.uplandsoftware.com/
Canada Canada
Canada, Quebec, Laval,
Currnet position: Senior ASP.NET Developer
Company: Uplandsoftware Inc
Certificates: MCITP, MCP, MCTS

Comments and Discussions

 
GeneralConverting and using the web user controls into custom controls in MS VS 2005 [modified] Pin
Deo From Baghi27-Aug-09 21:25
Deo From Baghi27-Aug-09 21:25 
Generalproblem with VS 2008 Pin
meysam navaei12-Aug-09 21:51
meysam navaei12-Aug-09 21:51 
Questiontoolbox support ? Pin
devherooo22-May-09 10:57
devherooo22-May-09 10:57 
QuestionRegarding Custom Control Pin
archananaresh17-Mar-09 1:09
archananaresh17-Mar-09 1:09 
GeneralGood Technique Pin
Member 309327910-Aug-08 8:06
Member 309327910-Aug-08 8:06 
QuestionI try to do it in VS 2008! but i failed Pin
alashram11-Jun-08 8:22
alashram11-Jun-08 8:22 
AnswerRe: I try to do it in VS 2008! but i failed Pin
jtv4k20-Jan-10 12:41
jtv4k20-Jan-10 12:41 
QuestionUnable to see "Add Web Deployment project" in VS 2005 menu for Webproject despite installing WebDeploymentSetup.msi Pin
mkdave31-May-08 12:00
mkdave31-May-08 12:00 
QuestionWhy can't it be more simple ? Pin
stixoffire5-May-08 6:51
stixoffire5-May-08 6:51 
AnswerRe: Why can't it be more simple ? Pin
Dimitar Madjarov5-May-08 11:34
Dimitar Madjarov5-May-08 11:34 
QuestionRe: Why can't it be more simple ? Pin
stixoffire5-May-08 17:56
stixoffire5-May-08 17:56 
GeneralUser control in user control Pin
kuzmanovskidarko1-May-08 22:36
kuzmanovskidarko1-May-08 22:36 
Generaldoes not seem to work with VS.NET 2008 Pin
Mark Kamoski30-Apr-08 7:50
Mark Kamoski30-Apr-08 7:50 
GeneralRe: does not seem to work with VS.NET 2008 Pin
Dimitar Madjarov1-May-08 2:53
Dimitar Madjarov1-May-08 2:53 
GeneralRe: does not seem to work with VS.NET 2008 Pin
Mark Kamoski1-May-08 13:04
Mark Kamoski1-May-08 13:04 
You are quite right. I was wrong. My code had a namespace error. Thank you.
Questionplease help me in this issue Pin
srirammanoj24-Apr-08 17:55
srirammanoj24-Apr-08 17:55 
QuestionError: Object reference not set to an instance of an object. Pin
kumartadika23-Apr-08 14:06
kumartadika23-Apr-08 14:06 
GeneralRe: Error: Object reference not set to an instance of an object. Pin
Dimitar Madjarov24-Apr-08 0:51
Dimitar Madjarov24-Apr-08 0:51 
GeneralRe: Error: Object reference not set to an instance of an object. Pin
Dimitar Madjarov24-Apr-08 2:47
Dimitar Madjarov24-Apr-08 2:47 
GeneralRe: Error: Object reference not set to an instance of an object. Pin
Dimitar Madjarov24-Apr-08 2:56
Dimitar Madjarov24-Apr-08 2:56 
GeneralRe: Error: Object reference not set to an instance of an object. Pin
kumartadika24-Apr-08 10:57
kumartadika24-Apr-08 10:57 
GeneralRe: Error: Object reference not set to an instance of an object. Pin
kumartadika24-Apr-08 12:32
kumartadika24-Apr-08 12:32 
GeneralRe: Error: Object reference not set to an instance of an object. Pin
Dimitar Madjarov24-Apr-08 17:34
Dimitar Madjarov24-Apr-08 17:34 
GeneralRe: Error: Object reference not set to an instance of an object. Pin
kumartadika25-Apr-08 7:56
kumartadika25-Apr-08 7:56 
QuestionCan you help me in this issue... Pin
srirammanoj23-Apr-08 5:46
srirammanoj23-Apr-08 5:46 

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.