Gourav Said..
Custom Controls in Asp.Net
Custom Controls are compiled code components that execute on the server, expose the object model, and render markup text, such as HTML or XML, as a normal Web Form or user control does. Unlike user controls, custom server controls are not placed in / App_Code folder but compiled to dll. You need to write complete control in code and there is no some visual designer (no markup code and no .ascx file). Because of that, custom controls are harder to create, but when created and compiled, they are easier to use. You can build installation package for custom controls and distribute them easy.
Base class for your custom control:To write a custom control, you should directly or indirectly derive the new class from the
System.Web.UI.Control class or from the
System.Web.UI.WebControls.WebControl class:
- You should derive from System.Web.UI.Control if you want the control to render nonvisual elements. For example, <meta> and <head> are examples of nonvisual rendering.
- You should derive from System.Web.UI.WebControls.WebControl if you want the control to render HTML that generates a visual interface on the client computer.
If you want to change the functionality of existing controls, such as a button or label, you can directly derive the new class with these existing classes and can change their default behavior. In brief, the Control class provides the basic functionality by which you can place it in the control tree for a Page class. The Web Control class adds the functionality to the base Control class for displaying visual content on the client computer. For example, you can use the WebControl class to control the look and styles through properties like font, color, and height.
Steps to create a simple custom control that extends from System.Web.UI.Control using Visual Studio: 1) Start Visual Studio and create a class library project, and give it a name, for example, CustomServerControlsLib.
2) Add a source file to the project, for example, SimpleServerControl.cs.
3) Include the reference of the
System.Web namespace in the references section.
4) Check whether the following namespaces are included in the SimpleServerControl.cs file.
System
System.Collections
System.ComponentModel
System.Data
System.Web
System.Web.SessionState
System.Web.UI
System.Web.UI.WebControls
5) Inherit the SimpleServerControls class with the Control base class.
public class SimpleServerControl : Control
6) Override the
Render method to write the output to the output stream.
protected override void Render(HtmlTextWriter writer)
{
writer.Write("Hello World from custom control");
}
Note The HtmlTextWriter class has the functionality of writing HTML to a text stream. The Write method of the HtmlTextWriter class outputs the specified text to the HTTP response stream and is the same as the Response.Write method.
7) Compile the class library project. It will generate the DLL output.
8) Open an existing or create a new ASP.NET Web application project.
9) Add a Web Forms page where the custom control can be used.
10) Add a reference to the class library in the references section of the ASP.NET project.
11) Register the custom control on the Web Forms page.
<% @ Register TagPrefix = "CC" Namespace = "CustomServerControlsLib" Assembly = "CustomServerControlsLib " % >
12) To instantiate or use the custom control on the Web Forms page, add the following line of code in the <form> tags.
<form id=Form1 method=post runat=server>
<CC:SimpleServerControl id="ctlSimpleControl" runat="server">
</CC:SimpleServerControl ></form>
13) Run the Web Forms page, and you will see the output from the custom control.
The Render Method: When you're working on a Web custom control, all of the magic happens in the Render method. In a nutshell, the Render method outputs the HTML to display the control in a Web browser. It's just like using Response.Write