Compile a Web User Control into a DLL ( .Net / c# )

posted by nblevins on Monday, June 09, 2008


So, for those parties interested, I have recently done some work trying to compile "controls" so that I can begin to have a self-contained code snippet library on my site.  After doing a bit of research, I have found what I feel to be the best way to go about this.  In this example, I am going to create a simple web user control to compile into a dll and add it to a separate website.

First things first...

There is a bit of different between the types of controls that you can create.  The most notable of control types are the user control and the web user control.  The big difference between the two is that user controls require that you either inherit from a existing control class or create a new one from scratch.  This includes hand coding all the front end view properties and such through the render method that all controls have... blah!

Another option is making an web user control (or .ascx file, which is probably more familiar).  The downfall of this control is that it is not edit-proof, and does not allow you to store it in places where it can "auto-refresh" itself.  You only option, if you want to share them cross-app, is to make a separate virtual directory of controls and share them across your sites... blah again...

Anyway...

The option that I like the best takes the best of both worlds.  We will use the awesome fludity of the .ascx file, but also compile them into .dll's (protection and versioning!!), which can be added as a reference, entered into the GAC, and all that other fun stuff.

Without futher ado...

The process is rather basic, but you can build on it as it goes.  Here is the basic steps to follow:

1)  Create a new website.
2)  Add a web user control to the site (.ascx).  You will find this in the "Add New Item" menu of your site.
3)  Do whatever you need to do on your user control.  Remember to make whatever properties you need access on the control declaration public in the C# (just encapsulate your fields!).
4)  Once complete, go to the Build menu and choose "Publish Web Site"
5)  You will be given the option to change some of your setings here.  Naturally you want to select where to build to.  Also, uncheck the "Allow this precompiled site to be updatable" for right now.  Finally, check the "Use fixed name and single page assemblies".  You can do some more fun stuff here, but play with that later when you master the basics.

So now you have your .dll (check in the bin folder of the site you complied).  In order to use it, just do this...

1)  Create a new site or open the one you want to stick it into.
2)  Add the .ddl to the bin. 
3)  Register the .dll on the page.  (do this using the @Register attribute of the page)

<%@ Register TagPrefix="ASP" Namespace="ASP" Assembly="App_Web_test.ascx.cdcab7d2" %>
<%@ Register TagPrefix="ASP" Namespace="ASP" Assembly=" App_Web_amazonservices.ascx.cdcab7d2" %>

*Remember to exclude the .dll from the Assembly name... 

Once you have done this, you will be good to go!

This process has some great advantages, which I am sure you all recognize.  Also, those publice encapsulated fields are now availiable for you to set on the .net side of things. 

I have created a web user control for amazon web services, which I will post a tutorial about at a later time.  Happy coding! 



Comments

Wednesday, June 11, 2008
Nice new design! Where are the posts??
Comment By: David Blevins

Where are the posts?
Friday, June 13, 2008
Well, those are in the works. I am finally wrapping up some of the code base and refactoring a few things. I am setting the 'engine' so that I can spawn other blogs sites that share the same database.

So, it is taking longer... they should be coming soon!
Comment By: Nathan Blevins

intelisense refferes an error
Tuesday, August 26, 2008
design looks like this

<%@ Register Assembly="App_Web_webusercontrol.ascx.cdcab7d2" TagPrefix="pre" Namespace="ASP" %>
<pre:webusercontrol_ascx runat="server" />

and error is: "Ambigous reference: ASP.webusercontrol_ascx ASP.webusercontrol_ascx"

all works though

but id like to know if you got the same error or why am i getting it.
Comment By: David Blevins

http://www.nathanblevins.com/Articles/Compile-a-Web-User-Control-into-a-DLL-.Net-c-.aspx
Sunday, September 28, 2008
Comment By: alkaesar

my web
Friday, October 03, 2008

Toolbox?
Thursday, October 16, 2008
How to get the control usable in the toolbox?

cheers
www.crackmycode.co.uk
Comment By: Andrew Ames

Sweet
Tuesday, February 17, 2009
If it wasn't for this I would be using the Custom Control and coding my control's UI in VB (which is a pain, if you've ever done nested tables).

Thanks a lot you've saved me a lot of time and increased my productivity.

Cheers
Giuseppe
Comment By: Giuseppe

wer
Saturday, June 06, 2009
<asdas>
Comment By: wer

thank you!
Sunday, August 09, 2009
I need some thing like that. I don't know it is work, let me try it first.

thank you!

www.localpcdoctor.com
Comment By: tarim

User Control attributes
Monday, August 10, 2009
Hi, above all, great post!
My user control has a public property (e.g. Visible) that I want to accept only values chosen in a specifed set (True/False).
How can I specify that?
The result I would like is that when somebody use my control, intellisense shows the available options for my "Visible" field, like it happens with standard controls.
Thank you.
Comment By: Carlo

Re: User Control attributes
Monday, August 10, 2009
Not so hard, I found it here:

http://dotnetdiscussion.net/2009/02/05/aspnet-create-a-user-control-property-with-selectable-options/

thanks anyway, hope this helps!
Comment By: Carlo

Null reference problem
Sunday, August 23, 2009
Hi
Thanks for posting this article. I have a problem though. In my ascx control I wrote a property to get/set the text of a child Literal control.

Public Property QuestionText() As String
Get
Return Me.litQuestion.Text
End Get
Set(ByVal value As String)
Me.litQuestion.Text = value
End Set
End Property

I've compile my ascx into a dll as instructed, but when I try to set this property in a web page of another project that uses the dll, I get a null reference exception, saying the litQuestion is null. Any ideas how to resolve this?

Many thanks
Comment By: Anthony

Don't forget to...
Friday, October 16, 2009
Don't forget to delete the original user control. Failure to do so will result in Ambiguous Namespace error.

Happy coding!
Comment By: Daniel

plz i need help
Monday, April 19, 2010
i did same as u told us to do but i have this error when i try to buil could u help
Parser Error Message: Type 'ASP.bin_app_web_accmanglist_ascx_cc671b29_dll' does not inherit from 'System.Web.UI.UserControl'
Comment By: sara samy

help
Monday, April 19, 2010
@ Register TagPrefix='uss' Src='Bin/App_Web_accmanglist.ascx.cc671b29.dll' tagname='AccMangList'
Comment By: sara samy

Name:
Email (not shown):
Home Page (optional):
Title:

Sorry, I don't support html entry... ;)