• Compile a Web User Control into a DLL


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…


The option that I like the best takes the best of both worlds.  We will use the awesome fluidity 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 settings 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 public encapsulated fields are now available 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!