Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
8 Oct 2007 1  
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 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