Click here to Skip to main content
Click here to Skip to main content

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

, 8 Oct 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Dimitar Madjarov
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] PinmemberGyan Singh27-Aug-09 22:25 
Generalproblem with VS 2008 Pinmembermeysam navaei12-Aug-09 22:51 
Questiontoolbox support ? Pinmembernano1922-May-09 11:57 
QuestionRegarding Custom Control Pinmemberarchananaresh17-Mar-09 2:09 
QuestionI try to do it in VS 2008! but i failed Pinmemberalashram11-Jun-08 9:22 
AnswerRe: I try to do it in VS 2008! but i failed Pinmemberjtv4k20-Jan-10 13:41 
QuestionUnable to see "Add Web Deployment project" in VS 2005 menu for Webproject despite installing WebDeploymentSetup.msi Pinmembermkdave31-May-08 13:00 
QuestionWhy can't it be more simple ? Pinmemberstixoffire5-May-08 7:51 
AnswerRe: Why can't it be more simple ? PinmemberDimitar Madjarov5-May-08 12:34 
QuestionRe: Why can't it be more simple ? Pinmemberstixoffire5-May-08 18:56 
GeneralUser control in user control Pinmemberkuzmanovskidarko1-May-08 23:36 
Generaldoes not seem to work with VS.NET 2008 PinmemberMark Kamoski30-Apr-08 8:50 
GeneralRe: does not seem to work with VS.NET 2008 PinmemberDimitar Madjarov1-May-08 3:53 
GeneralRe: does not seem to work with VS.NET 2008 PinmemberMark Kamoski1-May-08 14:04 
Questionplease help me in this issue Pinmembersrirammanoj24-Apr-08 18:55 
QuestionError: Object reference not set to an instance of an object. Pinmemberkumartadika23-Apr-08 15:06 
GeneralRe: Error: Object reference not set to an instance of an object. PinmemberDimitar Madjarov24-Apr-08 1:51 
GeneralRe: Error: Object reference not set to an instance of an object. PinmemberDimitar Madjarov24-Apr-08 3:47 
GeneralRe: Error: Object reference not set to an instance of an object. PinmemberDimitar Madjarov24-Apr-08 3:56 
GeneralRe: Error: Object reference not set to an instance of an object. Pinmemberkumartadika24-Apr-08 11:57 
GeneralRe: Error: Object reference not set to an instance of an object. Pinmemberkumartadika24-Apr-08 13:32 
GeneralRe: Error: Object reference not set to an instance of an object. PinmemberDimitar Madjarov24-Apr-08 18:34 
GeneralRe: Error: Object reference not set to an instance of an object. Pinmemberkumartadika25-Apr-08 8:56 
QuestionCan you help me in this issue... Pinmembersrirammanoj23-Apr-08 6:46 
QuestionResolve my issue is urgent...Plzzzz Pinmemberahsan829619-Sep-12 21:05 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150301.1 | Last Updated 8 Oct 2007
Article Copyright 2007 by Dimitar Madjarov
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid