Friday, May 30, 2014

Add Custom Controls in your ASP.NET Project

This article is about to discuss about the adding custom controls in  ASP.NET project. So what is a Custom control. Its a server control add you can create and add to any other project you want like C K Editor, AJAX Control Toolkit, Telerik, Dev Express etc.

So, to do this you need to create a new Server Control.

Now in this project you will find a class called ServerControl1.cs with code.
Now out mission is to do some action like say if in any other project in a textbox if we write "A" it will return "OK" else it will return "Not OK".

So copy the code follow here and paste into your RenderContents method.

protected override void RenderContents(HtmlTextWriter output)
    if (this.Text == "A")
       output.Write("Its OK ---done by Arkadeep\n");
        output.Write("Not OK ---done by Arkadeep\n");

Now build your project and get the .dll file of your project.

Now in another project add a new web form and add a new textbox and button.
On that project add the reference of your server control project.

Now add a register in the web form.

<%@ Register Assembly="ServerControl1" Namespace="ServerControl1" TagPrefix="ccs" %>
 Now add this code,

<ccs:ServerControl1 runat="server" ID="a1" Visible="false"></ccs:ServerControl1>

This is your control which you have created in your server control project.

Now in the button_click event write the code.

a1.Text = TextBox1.Text;
a1.Visible = true;

In this way you call your server control to execute. Now run your program to execute and test.

Download the full source code here.


Post a Comment

Popular Posts