6 February 2014

Custom Controls in Asp.net

Custom controls are deployed as individual assemblies. They are compiled into a dynamic link library (DLL) and used as any other ASP.Net server control. They could be created in either of the following way:
·         By deriving a custom control from an existing control
·         By composing a new custom control combing two or more existing controls
·         By deriving from the base control class
To understand the concept, let us create a custom control, which will simply render a text message on the browser. To create this control, take the following steps:
  •      Create a new website. Right click the solution (not the project) at the top of the tree in the Solution Explorer.
  •     In the New Project dialog box, select ASP.Net Server Control from the project templates.
Explanation:
The above step adds a new project and creates a complete custom control to the solution, called Server Control1. In this example, let us name the project Custom Controls. To use this control, this must be added as a reference to the web site before registering it on a page. To add a reference to the existing project, right click on the project (not the solution), and click Add Reference.
Select the Custom Controls project from the Projects tab of the Add Reference dialog box. The Solution Explorer should show the reference.
  3) To use the control on a page, add the Register directive just below the @Page directive:


<%@ Register Assembly="Custom Controls" Namespace="Custom Controls"  TagPrefix="ccs" %>
 4) Next you can use the control, like any other controls.


 <form id="form1" runat="server">
    <div>
    <ccs: ServerControl1 runat="server" Text = "I am a Custom Server Control" />
    </div>
 </form>



No comments:

Post a Comment