X
05Feb

Converting UserControl into a dll

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.

 

Related

What are Frames? How to handle frames in Selenium WebDriver with C#?

IFrame (FullForm: Inline Frame) is an HTML document that is included in another HTML document and is...

Read More >

What is Synchronization? Handling Synchronization in Selenium WebDriver using C#:

Synchronization meaning: when two or more components involved to perform any action, we expect these...

Read More >

Sending Test reports by Email using Office 365, Gmail

Wouldn’t it be great if Test Report are sent automatically across team as soon the Test Execut...

Read More >

ASP.NET: Audit Trail Implementation using Entity Framework

When you are working with certain projects which involves the Customer records, you might need to tr...

Read More >

Create Restful service using Web API

What is Asp.Net Web API?Asp.Net Web API is a framework for building HTTP services that can be consum...

Read More >

Create custom project templates in Visual Studio

Visual studio installation comes with the various predefined project templates, and we can use one o...

Read More >

WCF SOAP and REST Services

WCF (Windows Communication Foundation) is a programming platform and runtime system for building, co...

Read More >

What's New In C# 6.0?

Microsoft has released the preview version of Visual Studio 2015 and .NET 4.6 for developers to try ...

Read More >

Creating an RDLC report in Asp.Net

Step 1.  Create a new website and Add DataSet to the WebsiteRight Click on the project, Add new...

Read More >

How to add Overlay in Windows phone 8

Windows Phone is windows mobile operating system from Microsoft.In my project, there was a requireme...

Read More >

Share

Try DevOpSmartBoard Ultimate complete Azure DevOps End-to end reporting tool

Sign Up

  • Recent
  • Popular
  • Tag
Monthly Archive
Subscribe
Name

Text/HTML
Text/HTML
Contact Us
  • *
  • *