Canarys | IT Services

Blogs

Converting UserControl into a dll

Date:
Author:
Tags:
Share

User controls are containers into which you can put markup and Web server controls. You can then treat the user control as a unit and reuse it in many other ASP.NET pages. But here I show you how to combine any number of UserControls into a dll and re-use across applications.

Step1: Creating a UserControl

Create a new ASP.NET Empty Web Site. I choose 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 and 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.

<form id="form1" runat="server">
Mail Server Name:
<asp:TextBox ID="txtMailServer"  Text="pop.gmail.com" runat="server" />
<br />
Email ID:
<asp:TextBox ID="txtEmail" runat="server" />
<br />
Password:
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password" />

– See more at: http://www.ecanarys.com/blog-entry/reading-gmail-inbox-using-aspnet#sthash.aVbggWUw.dpuf

<form id="form1" runat="server">
Mail Server Name:
<asp:TextBox ID="txtMailServer"  Text="pop.gmail.com" runat="server" />
<br />
Email ID:
<asp:TextBox ID="txtEmail" runat="server" />
<br />
Password:
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password" />

– See more at: http://www.ecanarys.com/blog-entry/reading-gmail-inbox-using-aspnet#sthash.aVbggWUw.dpuf

<form id="form1" runat="server">
Mail Server Name:
<asp:TextBox ID="txtMailServer"  Text="pop.gmail.com" runat="server" />
<br />
Email ID:
<asp:TextBox ID="txtEmail" runat="server" />
<br />
Password:
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password" />
<br />
Port:
<asp:TextBox ID="txtPort" runat="server" Text="995" />
<br />
SSL:
<asp:CheckBox ID="chkSSL" checked="true" runat="server" />
<br />
<asp:Button ID="btnReadEmails" runat="server" Text="Read Emails" OnClick = "Read_Emails" />
<br /><hr />
<asp:GridView ID="gvEmails" runat="server" AutoGenerateColumns="false">
<Columns>
    <asp:BoundField HeaderText = "From" DataField = "From" />
    <asp:HyperLinkField HeaderText = "Subject" DataNavigateUrlFields = "MessageNumber" 
           DataNavigateUrlFormatString ="~/ShowMessageCS.aspx?MessageNumber={0}" 
           DataTextField = "Subject" />
    <asp:BoundField HeaderText = "Date" DataField = "DateSent" />
Columns>
asp:GridView>
form>
 

– See more at: http://www.ecanarys.com/blog-entry/reading-gmail-inbox-using-aspnet#sthash.aVbggWUw.dpuf

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

<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" />
CodeBehind file (ascx.cs)
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MyUserControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public string EnteredText
    {
        get { return TextBox1.Text; }
    }

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

}

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 and hdeclaratively 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_0amgn22c" namespace="ASP" tagprefix="UC" %>

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:a //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>
      <UC:MyUserControl ID="uc" runat="server" >UC:MyUserControl>
    div>
    form>
body>
html>

That’s it. Hope this helps.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Reach Us

With Canarys,
Let’s Plan. Grow. Strive. Succeed.