Creating Portable UserControls (dll) in VS2010

The following example shows how to combine any number of UserControls into a dll & re-use across applications.

Step1: Creating a UserControl
VS2010 –> File –> New Web Site –> ASP.NET Empty Web Site. I chose the Empty Web Site template so that after I publish I have the dll just for UserControls and not for any aspx page. This project will just contain UserControls, and eventually transformed into a dll. Now add a UserControl & pull few controls from the ToolBox to give it any look-&-feel you want. Now one important thing to note: In the ascx file, don’t forget to add the classname attribute. This will be used in the calling aspx page to declaratively access this UserControl. Below is the code sample from ascx file.

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="MyUserControl1.ascx.cs" Inherits="MyUserControl1" 
ClassName="MyUserControl11" %>


<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br /><br />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MyUserControl1 : System.Web.UI.UserControl
{
    public string EnteredText
    {
        get { return TextBox1.Text; }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = TextBox1.Text;
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

Step2: Now after regular build is successfull, we will publish this website to generate the dll.
Build –> Publish Web Site –> Uncheck  ‘Allow this precompiled site to be updateable’
Save the published site to any custom path you may want. Later on we will grab the dll from this path.

Step3: Create another separate WebSite to access this dll. Create a bin folder. Now in this new asp.net website, add the earlier generated dll in it’s bin folder. Next would be to register the user control & declaratively add the control in the aspx page. Code sample from aspx page as follows.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>
<%@ Register assembly="App_Web_mgojvytl" namespace="ASP" tagprefix="e12" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <e12:MyUserControl11 ID="d1" runat="server" ></e12:MyUserControl11>
    </div>
    </form>
</body>
</html>

That’s it. Hope this helps.

Advertisements
This entry was posted in General ASP.Net C#. Bookmark the permalink.

One Response to Creating Portable UserControls (dll) in VS2010

  1. praveen says:

    super it helped me a lottttttttt :) kudos :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s