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.
– See more at: http://www.ecanarys.com/blog-entry/reading-gmail-inbox-using-aspnet#sthash.aVbggWUw.dpuf
– See more at: http://www.ecanarys.com/blog-entry/reading-gmail-inbox-using-aspnet#sthash.aVbggWUw.dpuf
– 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" />
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.