Saturday, November 19, 2011

Create DLL for User Control ( Redistribution )

In this article i will tell you how to create dll from the user control and use that dll in other web application in .net

Step1: Create a class library solution in visual studio.

Step 2: In references add the reference of System.Web dll

Step3: In class file add the namespaces
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Reflection;

Step 4: Add the User control but remove ascx.cs file and designer file and change ascx page control directive
<%@ Control Language="C#" AutoEventWireup="true" %>
In my case user control is ( "VKBoard.ascx" ). then go to the property of the user control and change build action to embedded resource

Step5: in class file override the oninit () function

protected override void OnInit(EventArgs e)
{
base.OnInit(e);
// loads markup from embedded resource
string content = String.Empty;
Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(GetType(), "VKBoard.ascx");
using (StreamReader reader = new StreamReader(stream))
{
content = reader.ReadToEnd();
}
Control userControl = Page.ParseControl(content);
this.Controls.Add(userControl);

//Put content is my function for adding the text in the control or what you want to do with control add initialization time
PutContent();
}



Step 6: Compile the solution

Step 7: Go to the solution directory and then in bin folder you will get the debug or release folder.
take the dll from there

So now you have a dll

Step 8: Add the reference of the dll (in my case it is virtualkeyboardlib.dll)

Step 9: in aspx page ragister the control

<%@ Register Namespace="VirtualKeyBoardLib" Assembly="VirtualKeyBoardLib" TagPrefix="CC" %>

Step 10: Use the control in aspx page



for more knowledge i am posting the whole files code and image below:



Code for VirtualKeyBoardlib.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Reflection;

namespace VirtualKeyBoardLib
{
public class VirtualKeyBoardLib : UserControl
{
private List DataPosition = new List();
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
// loads markup from embedded resource
string content = String.Empty;
Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(GetType(), "VKBoard.ascx");
using (StreamReader reader = new StreamReader(stream))
{
content = reader.ReadToEnd();
}
Control userControl = Page.ParseControl(content);
this.Controls.Add(userControl);
PutContent();
}

private void PutContent()
{
Random randomnum = new Random();
for (int i = 0; i < 10; i++)
{

while (true)
{
int value = randomnum.Next(0, 10);
if (DataPosition.Contains(value))
continue;
else
{
DataPosition.Add(value);
string buttonid = "Button" + (value + 1);
Button btn = (Button)this.FindControl(buttonid);
btn.Text = i.ToString();
break;
}
}
}
}



}
}

VKBoard.ascx



Add dll in other project



Default.aspx


Output:



thanks for reading this article. If you really like this please comment on this article and give your valuable response.

No comments: