Saving a Web User Control as a reusable DLL

by Admin 8. October 2009 22:25

If you have been folowing the previous posts you would now have successfully created a simple Web User Control and used it within a single page website. In the first post we created the Web User Control named CustomDropDown. In the second and third posts we added Events to the CustomDropDown control. The final step in the process is to make this control easily reusable across a multitude of web sites. To do this, we compile the Web User Control as a dll.

Creating a DLL from a Web User Control

Before we set about saving the CustomDropDown control as a dll, we need to make some small changes. The first is to add a ClassName to the @Control directive in the CustomDropDown.ascx file.

<%@ Control Language="VB" AutoEventWireup="false" ClassName="Acme.CustomDropDown" CodeFile="CustomDropDown.ascx.vb" Inherits="UserControls_CustomDropDown" %>

Secondly, to ensure that the custom Event class CustomDropDownEventArgs is accessable from the DLL, we need to copy the code from the class .vb file into the CustomDropDown.ascx.vb file. Make sure you copy the complete class code - i.e. everything including the Public Class CustomDropDownEventArgs tags. The complete code for the CustomDropDown.ascx.vb file is shown below.

Partial Class UserControls_CustomDropDown
    Inherits System.Web.UI.UserControl

    Public Event ListChanged(ByVal sender As Object, ByVal e As CustomDropDownEventArgs)

    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged

        Me.Label1.Text = Me.DropDownList1.SelectedValue

        Dim myEvent As New CustomDropDownEventArgs()
        myEvent.FullName = Me.DropDownList1.SelectedItem.Text
        myEvent.PayRollID = Me.DropDownList1.SelectedItem.Value
        RaiseEvent ListChanged(sender, myEvent)

        RaiseEvent ListChanged(sender, myEvent)
    End Sub
End Class
Public Class CustomDropDownEventArgs
    Private _fullname As String
    Private _payrollID As String

    Public Property FullName() As String
            Return _fullName
        End Get
        Set(ByVal value As String)
            _fullname = value
        End Set
    End Property
    Public Property PayRollID() As String
            Return _payrollID
        End Get
        Set(ByVal value As String)
            _payrollID = value
        End Set
    End Property
End Class

We can now build and publish the website using the commands in Visual Studio.

  • Un-tick the Allow this precompiled site to be updatable, this ensures that everything, including the mark-up on the ascx page is compiled.
  • Tick the Use fixed naming and single page assemlies, this will ensure that the ascx file, including the code behind file will be saved in its own DLL and not compiled with any other elements.

Adding the DLL to a new Website

Once you have published the control, you can now add it to a new or existing website. To utilise the control, simply use the Add Reference command and browse to the location where you published the website in the step above. The DLL's will be saved under the \bin folder.

To include the control on a page in your new website you need to add two lines of code to the source. The first line registers the control and is similar (but not identical) to the @Register directive added automatically by Visual Studio when you drag and drop the Web User Control onto a page.

<%@ Register TagPrefix="myControl1" Namespace="Acme" Assembly="App_Web_customdropdown.ascx.6bb32623" %>

Two things to note about the above tag, firstly make sure the Namespace element matches the namespace you added to the Classname element in the previous steps and you only need to inlcude the root Namespace component of the Classname. The second it that the Assemby name must match the DLL name that you added a reference to, but does not include the file extension '.DLL'.

Once you have set up a reference and registered it on the page, you can add the tag whereever you need the control to appear in your mark-up. While you are there, add a label control to the page so that you can test the Event handling

<myControl1:CustomDropDown ID="CustomDropDown1" runat="server" /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> 

 Adding Custom Event Handling

The final step is to add the custom event handling we outline in Adding Custom Events to Web User Controls As we named the control the same as the Web User Control in the previous post, we can simply cut and past the CustomDropDown1_ListChanged code from the default.aspx page into our new page.

Private Sub CustomDropDown1_ListChanged(ByVal sender As Object, ByVal e As CustomDropDownEventArgs) Handles CustomDropDown1.ListChanged
        Me.Label1.Text = e.FullName
End Sub

Tags: , ,