Click here to Skip to main content
Email Password   helpLost your password?
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
.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralConverting and using the web user controls into custom controls in MS VS 2005 [modified]
Gyan Singh
22:25 27 Aug '09  
Hi,
I have created some user controls. I have dynamically loaded one usercontrol say "ChildControl.ascx" in another user control say "ParentControl.ascx" using the following code:

Dim tempControl As New Control
tempControl = LoadControl("~/folder/folder2/ChildControl.ascx")
Dim objChild As ChildControl = TryCast(tempControl, ChildControl)

I have converted the user controls into custom controls. Then I am using all these control as you mentioned in this post.
But I am getting the following error while the parent control is loading the child control dynamically:

The file '/folder/folder2/ChildControl.ascx' does not exist.

How should I solve this problem?

I also want to generate output to single assembly/dll for all the user controls, and I will use this single dll to register any usercontrol. how is it possible?

Regards,

modified on Friday, August 28, 2009 3:47 AM

Generalproblem with VS 2008
meysam navaei
22:51 12 Aug '09  
problem with VS 2008
plaese help me :(
Questiontoolbox support ?
nano19
11:57 22 May '09  
can I add the generated custom controls in the toolbox, so that I can drag them to a web page just like
the standard asp.net controls ?


Thanks
QuestionRegarding Custom Control
archananaresh
2:09 17 Mar '09  
Its an Excellent Article...
I succeeded in doing this.
Iam trying to create login Control, where in custom control i create only 2 textboxes as userid and password and on normal web application i make use of this .dll file where i use button control ...on click my textboxes text should authenticated?
problem is how should i refer individual control id from custom control in my web application like "select * from tblregistration where userid ='" + textbox1.text + "' AND passwd ='" + textbox2.text + "'"; //customcontrolID.------? what
plz help me
tanx
GeneralGood Technique
Member 3093279
9:06 10 Aug '08  
A very good technique...

It solved my problem.





Thanks,

Ahmed Fahad

QuestionI try to do it in VS 2008! but i failed
alashram
9:22 11 Jun '08  
Dear all;
can you help me?
i try to do it but i failed!

when i add new web user Control
the source code is

Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl"

when i try to change it to

Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl.ascx.cs" ClassName="Test.WebUserControl"

first i lost the relation with the WebUserControl.ascx.cs file and i wrote the c# code as


script runat="server"

protected void Button1_Click(object sender, EventArgs e)
{

}
/script


second point when i add the web deployment project and change the output assemblies to "create a separate file for each page and control" and build the project


the result
Validation Complete
------ Build started: Project: createwebuser_deploy, Configuration: Debug Any CPU ------
if exist ".\TempBuildDir\" rd /s /q ".\TempBuildDir\"
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_compiler.exe -v /createwebuser -p "D:\My Documents\createwebuser" -u -f -d -fixednames .\TempBuildDir\
if exist "D:\My Documents\Visual Studio 2008\Projects\createwebuser\createwebuser_deploy\Debug\" rd /s /q "D:\My Documents\Visual Studio 2008\Projects\createwebuser\createwebuser_deploy\Debug\"
if not exist "D:\My Documents\Visual Studio 2008\Projects\createwebuser\createwebuser_deploy\Debug\" md "D:\My Documents\Visual Studio 2008\Projects\createwebuser\createwebuser_deploy\Debug\"
if exist ".\TempBuildDir\" rd /s /q ".\TempBuildDir\"
========== Build: 2 succeeded or up-to-date, 0 failed, 0 skipped ==========

when i open the Debug\bin folder i get only App_Web_default.aspx.cdcab7d2 file for the aspx page and i don't get the dll file for the web user control ???
can i get help ????

regards
AnswerRe: I try to do it in VS 2008! but i failed
jtv4k
13:41 20 Jan '10  
I'm having the same problem! Does anyone know if this is possible on VS 2008?
QuestionUnable to see "Add Web Deployment project" in VS 2005 menu for Webproject despite installing WebDeploymentSetup.msi
mkdave
13:00 31 May '08  
Despite repeatedly installing the WDP in VS 2005 SP1, the option to "Add Web Deployment Project" doesnt display for a web application project. It displays only for a website project. What am I missing??

When I create my controls in a website and use the "Add Web Deployment Project" and build, I am getting issues with "Null object reference" ....

So could you tell me how to get that menu "Add Web Deployment Project" for web app project?
QuestionWhy can't it be more simple ?
stixoffire
7:51 5 May '08  
Why can't we just do this:
Create a custom control project.
Put our user control in the project as an embedded resource.
Read in our embeeded resource as design time html or as create child controls...

And then bam...we have it - sure would be nice don't you think.
AnswerRe: Why can't it be more simple ?
Dimitar Madjarov
12:34 5 May '08  
Please realize your idea in C# and share the results with us. Will be interesting....
Big Grin Big Grin Big Grin

Dimitar Madjarov

QuestionRe: Why can't it be more simple ?
stixoffire
18:56 5 May '08  
I will try to do this - but one thing I would like to know more about and yet have found little information on
The HtmlEmptyControlBuilder - do you know of this component and how to use it - there are no examples on MSDN, and there does not seem to be anything on the web...searching Google Excite etc..
GeneralUser control in user control
kuzmanovskidarko
23:36 1 May '08  
Hi,
I have a user control that is used in several other user controls.
The control has some buttons and i want to set their properties from let's say from one of the user controls. i have this code:

protected Button btnPrevious;

and the property
public bool EnabledPrevious { get { return btnPrevious.Enabled; } set { this.btnPrevious.Enabled = value; } }

from the other user control i have this code in the Page_Load method:

WebControl1 Top = new WebControl1();
Top = (WebControl)Page.LoadControl("path to the control");
Top.EnabledPrevious = false;

but the button Previous remains Enabled.
Can please anyone help me?
Generaldoes not seem to work with VS.NET 2008
Mark Kamoski
8:50 30 Apr '08  
Please help.

This technique worked for me with VS.NET 2005.

Unfortunately, this technique does not seem to work with VS.NET 2008.

The error is that at run-time, the contol is null.

Do you have any suggestions?

Please advise.

Thank you.

-- Mark
GeneralRe: does not seem to work with VS.NET 2008
Dimitar Madjarov
3:53 1 May '08  
Dear Mark,

I am using this in MS VS 2008 without any troubles. Everything is the same.
Please check that you use the WebDeployment project extensions from Microsoft Web Site for MS VS 2008.
It is different than MS VS 2005. In my article I attach only MS VS 2005.
Please give me a e-mail and I will send you this 'msi' file in case that you can not find it.

Dimitar Madjarov

GeneralRe: does not seem to work with VS.NET 2008
Mark Kamoski
14:04 1 May '08  
You are quite right. I was wrong. My code had a namespace error. Thank you.
Questionplease help me in this issue
srirammanoj
18:55 24 Apr '08  
its working fine in Microsoft Internet Explorer.But the functionality in that ASPX page is not working in other browsers like Mozilla. I can't see some controls, which results in not meeting the requirement itself. So, I was just wondering about this issue .

what might be the problem here, can you please help me from this.
QuestionError: Object reference not set to an instance of an object.
kumartadika
15:06 23 Apr '08  
In Page_load of user control if i reference any of the controls i get the error as Object reference not set to an instance of an object. In other blogs i saw similar error when converting user controls to custom controls but never saw any solution regrading this issue. Please let me know if you have any solution for this issue
GeneralRe: Error: Object reference not set to an instance of an object.
Dimitar Madjarov
1:51 24 Apr '08  
Hi.
Please give me 1 hour to reach my work and I will send you an example code.

Faithfully yours,
Madjarov.D.N
Smile

Dimitar Madjarov

GeneralRe: Error: Object reference not set to an instance of an object.
Dimitar Madjarov
3:47 24 Apr '08  
Hi,

This is the first option:

In your control in the 'ascx' file you have to use exactly this file as 'server' side code to manage your events and pages.
Like the example below:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WUCGridToGrid.ascx.cs" ClassName="TGtoG.WUCGridToGrid"%>
<%@ Register Assembly="DevExpress.Web.ASPxGridView.v8.1" Namespace="DevExpress.Web.ASPxGridView" TagPrefix="dxwgv" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
//Put your code here................
}
</script>

Faithfully yours,
Madjarov.D.N

Dimitar Madjarov

GeneralRe: Error: Object reference not set to an instance of an object.
Dimitar Madjarov
3:56 24 Apr '08  
Hi,

This is the second options:
When you add the generate from WebDeploy project assembly in your new aspx page where you like to use the control in the C# code you may use and manage all kind of controls placed in your converted user control as the next example:

protected override void OnPreRender(EventArgs e)
{
if (!this.Page.IsPostBack)
{
//Make a reference with left grid control
if (myConvertedUserControl.FindControl("grdLeftGrid") != null)
{
ASPxGridView gridLeft = (ASPxGridView)myConvertedUserControl.FindControl("grdLeftGrid");
gridLeft.Settings.ShowVerticalScrollBar = true;
gridLeft.Settings.ShowColumnHeaders = true;
}

base.OnPreRender(e);
}
}

Here 'myConvertedUserControl' is the server control which we convert from user control.
And 'grdLeftGrid' is a DevExpress grid control which I place inside my web user control. By this way you may access all controls, public propertirs and override publick events inside the user control.

Faithfully yours,
Madjarov.D.N
Smile

Dimitar Madjarov

GeneralRe: Error: Object reference not set to an instance of an object.
kumartadika
11:57 24 Apr '08  
Thank you so much Madjarov.

Your first option works fine. I did not understand why the same thing does not work when Page_Load is in Code Behind file. That is a million dollar question for me now.
GeneralRe: Error: Object reference not set to an instance of an object.
kumartadika
13:32 24 Apr '08  
I have another question, is there a way to add converted custom control to toolbox. when i try to add converted control to tool box it gives the error as "There are no components in the control that can be placed on the toolbox". Your help is really appreciated
GeneralRe: Error: Object reference not set to an instance of an object.
Dimitar Madjarov
18:34 24 Apr '08  
Yes, there is a way.
Create an standard server control with MS VS Studio 2005/2008 wizard.
And in the class which will be generate from this wizard inherited the class of your converted control.
This will work.

Dimitar Madjarov

GeneralRe: Error: Object reference not set to an instance of an object.
kumartadika
8:56 25 Apr '08  
How can we create a class with server control wizard? according to my knowledge we just place the wizard control on the web page and customize the wizard steps. but how can we create a class from this wizrad?
GeneralCan you help me in this issue...
srirammanoj
6:46 23 Apr '08  
Mr.Dimitar Madjarov,

Thanks for the article Big Grin .

Really a good piece of article and helpful to people who are struggling to convert their web user controls to custom controls(dlls).

Can you help me in this issue...

I am done with your steps and I got what I need like dlls, and used in other projects, its working fine in Microsoft Internet Explorer.But the functionality in that ASPX page is not working in other browsers like Mozilla. I can't see some controls, which results in not meeting the requirement D'Oh! itself. So, I was just wondering OMG about this issue Sniff .

what might be the problem here, can you please help me from this.


Manoj Sriram can u mail me at: srirammanoj@hotmail.com


Last Updated 8 Oct 2007 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010