Jan 31 2006

Convert UserControls to Custom Controls

Category: ASP.NET 2.0 - GeneralBil@l @ 21:40

I found a nice article today on MSDN titled as:
Turning an .ascx User Control into a Redistributable Custom Control

I will summarize as much as possible of that article, since it is really a valuable one.

It talks about creating your own UserControls, then converting them into CustomControls.

Create a New Web Site in Visual Studio 2005 (I guess it should work in VWD too, haven't tried it). Name it WebUserControl (Namespace Name!!!)

  • Add a new Web User Control
  • Design the control the way you want. For the same of this post, I created this simple user control shown below, you can use either Code-Beside or Code-Inline:
<%@ Control Language="C#" ClassName="WebUserControl.HelloWorld" %>

<script runat="server">

    protected void btnSubmit_Click(object sender, EventArgs e)
        this.lblMsg.Text = Server.HtmlEncode(this.txtMsg.Text);

<strong>Hello World Custom-User Control<br />
<br />
Enter Your Name:
<asp:TextBox ID="txtMsg" runat="server"></asp:TextBox>
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" /><br />
<br />
<br />
<asp:Label ID="lblMsg" runat="server"></asp:Label>
  • Compile the control, and then add it to the default.aspx page that came with the website upon its creation. Make sure it works, and then delete that default.aspx page.
  • Now we need to publish the usercontrol, why? In ASP.NET 2.0, every aspx page or ascx file, will be converted to its own DLL file either through precompiling or at run time. In this example, we will publish the website, which requires precompiling the website, and this generating the DLL that resembles the user control under test.
    • Go to Build --> Publish Web Site
    • Type in the Target Location, where the precompiled files shall be placed.
    • Deselect the *Allow this precompiled to be updatable*, why? In the updatable mode, only the code-beside will be precompiled and not the ascx file itself. In our case we need both to be in one DLL.
    • Select *Use fixed naming and single page assemblies*. "This will gurantee that your user control will be compiled into a single assembly that will have a name based on the ascx file itself ..."
  • Now, the user control is precompiled. Create a new website, right click the application, Add Reference, choose the DLL just created.
  • On an aspx page, ad the following below the page directive:
<%@Register TagPrefix="bhaidar" Namespace="WebUserControl" Assembly="App_Web_helloworld.ascx.cdcab7d2"%>

The Namespace is the same as the Namespace in which the UserControl was created.
The Assembly, is the name of the DLL file generated

  • Inside the page body, add the following control:
<bhaidar:helloworld id="MyUC" runat="server"/>

Although this method made your user controls become custom controls yet, the custom controls won't have any desing time view and this is really a loss!!!

Hope you enjoyed this post and found it beneficial.





Comments are closed