Custom Controls in ASP.NET

Visual Studio .NET provides a rich set of standard controls, which are also called intrinsic controls. These standard controls provide a wide range of functionality and allow you to design and develop a user-friendly interface for your applications. Additionally, Visual Studio .NET provides you custom controls that you can use if the existing controls do not meet your requirements.

For example, consider a Web application that needs to have multiple Web Forms and most of the Web Forms need a calculator. In this case, instead of adding standard controls to each Web Form for implementing the calculator, you can create one custom control to represent a calculator and use it across the Web Forms in your application. Thus, custom controls allow reusability.

You can create the following types of custom controls in Visual Studio .NET:

User control:

A Web Forms page that can be used as a control on other Web Forms pages. Thus, if you already have a Web Forms page and you need to construct a similar one, you can use the existing page as a user control.

Composite control:

A combination of existing controls that is used as a single control. You can create a composite control in any .NET programming language and use it on an ASP.NET page. For example, you can create a composite control comprising a button and a text box in C# and use it on an ASP.NET page.

In addition to the custom controls discussed, you can perform the following actions with controls:

Extend the functionality of the existing Web Form controls to meet your requirements. For example, consider a situation in which an existing Web Forms control meets almost all of your requirements, but you need some additional features in the control. In such a situation, you can add more features to your Web Form and customize the control. This can be done by inheriting from the control and overriding its properties, methods, or events.

Develop a custom control by inheriting directly from one of the Control base classes. You'll need to do this when none of the existing Web Forms controls meets any of your requirements. The benefit of using the existing classes to create custom controls is that they provide the basic framework needed by a Web Forms control. This way, you can concentrate more on programming the features that you want to incorporate.

Before you create your own custom controls, let us examine the base classes used by the controls.

Basic Structure of Web Forms Controls

This section equips you with the basic understanding of the elements involved in developing a Web Forms control. We will first discuss the classes that are used to create Web Forms. This is followed by a discussion of the interfaces that can be implemented in Web Forms controls.

Classes used for Web Forms controls Each Web Forms control is a class that inherits from the System.Web.UI. Control class directly or indirectly. Therefore, in this section, we examine the System.Web.UI.Control class and its inherited classes that are used to create a Web Forms control.

The System.Web.UI.Control class

The System.Web.UI.Control class defines all the properties, events, and methods that are common to all the Web Forms controls. You need to inherit your control from this class in the following cases:

When your control does not have a user interface

When your control includes other controls that render their own user interface


The System.Web.UI.WebControls.WebControl class

The System.Web.UI.WebControls.WebControl class is the base class for all Web controls. This class provides properties and methods to implement user interface functionality. It is inherited from the Control class. Some of the properties that are used to render additional user interface functionality are ForeColor, BackColor, BorderStyle, Width, and Height. Web controls, such as Label, TextBox, Button, Table, and Calendar, all inherit from the WebControl class. Therefore, if you have a control that has a user interface, it should inherit from the WebControl class.

The System.Web.UI.HtmlControls.HtmlControl class

The HtmlControl class is the base class for all HTML controls in Web Forms. This class inherits from the Control class. The controls provided by this class map directly to HTML elements. Therefore, these controls are useful for migrating ASP applications to ASP.NET.

Interfaces used for Web Forms controls

Several interfaces are available that you can implement depending upon your requirements. For example, if your control provides data binding, you need to implement the INamingContainer interface. In this section, we examine the interfaces that you might need to implement when you create controls.

INamingContainer interface

This interface is used when you need to create controls that satisfy any of the following conditions:

Provides data binding
Is a templated control
Routes events to its child controls

The INamingContainer interface doesn't have methods. When this interface is implemented by a control, the ASP.NET page framework creates a namespace for the control and ensures that each child control in the parent control has a unique ID.

IPostBackDataHandler interface

A control should implement the System.Web.UI.IPostBackDataHandler interface when it needs to update its state or raise events on the server after examining the postback data.

For example, the data sent to a TextBox control might result in the text box changing its text, as determined by its Text property. When the text changes, the text box also raises a TextChanged event. Thus, this control is examining the data, changing its state, and raising events.

IPostBackEventHandler interface

The IPostBackEventHandler interface is implemented in a control when you want to transfer events that are generated on the client side to the server. Events generated on the client side are postback events, hence the interface name. An example of implementing this interface is when a user submits a form on the client side. In this case, the server does not know that the form is submitted unless the IPostBackEventHandler interface generates the Click event on the server when the form is submitted. This ensures that the server is in sync with the events that occur at the client end.

Developing a composite control

You can create new controls using one or more existing controls. Such controls that aggregate a number of controls are referred to as composite controls. The primary difference between composite controls and user controls is that composite controls are compiled into assemblies and the compiled file is included into the project in which you want to include the control. In this section, we will create a composite control in C# and use it on an ASP page.

Concepts involved in creating a composite control

When you create a composite control, you need to do the following:

Define the class that implements the composite control. The class needs to inherit from the Control class and, optionally, the INamingContainer class.

Optionally override the CreateChildControls method of the Control class. The CreateChildControls method creates any child controls that need to be drawn on a page. This method is used in compositionbased rendering, wherein child controls are instantiated and rendered on the page.

Optionally implement the Render method of the Control class. You need to implement this method when you use the rendering logic instead of composition to render the ASP page. When you render controls, the performance overhead is less because controls need not be instantiated. Instead, the page is rendered as defined by the Render method. The Render method controls the output of the page at run-time.

Creating the control

You have examined the basic concepts to create a control. Let us now create a composite control. We will create the control in C#. The same programming logic can be used in VB.NET as well, except that the syntax will change. The control that we will create comprises a Calendar control, a TextBox control, a Submit button, and a Label control. The user is expected to select his or her date of birth from the calendar, specify their work experience in years, and click the Submit button to ascertain whether he or she is eligible for a job.

To create the custom control project, create a new Class Library project in C#.

The controls that you need to use on the form are in the System.Web namespace.

Therefore, include a reference to the System.Web.dll file. To include the reference, you
need to perform the following steps:

1. Select the Add Reference option from the Project menu. The Add Reference dialog box appears.
2. In the Add Reference dialog box, from the .NET tab, select System.Web.dll and click Select.
3. The component moves to the Selected Components list. Click OK to add the reference.

After you add a reference to the System.Web.dll file, you can write the code for the control. To code the control, select the class module from the Solution Explorer. In the class module, declare the namespaces that are used by the control by specifying the following statements:
using System;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

Next, declare the namespace for the control and declare a public class in the namespace. You should also declare the properties that you need to expose for the control. In the following code, we have declared the namespace, class, and a Text property for an EmpElg label. Additionally, we have also left placeholders for the CreateChildControls method and the Click event of the Submit button.

RELATED POST

CREATING ASSEMBLIES WITH ASP.NET

BUILDING HANDLERS IN ASP.NET

INTERFACES AND CLASSES CREATION IN ASP.NET

CACHING IN ASP.NET

CACHING IN ASP.NET PART TWO

WIRE LESS APPLICATION WITH ASP.NET

SECURITY IN ASP.NET PART ONE

SECURITY IN ASP.NET PART TWO

LOCALIZING ASP.NET APPLICATIONS

DEPLOYING ASP.NET APPLICATIONS

ASP.NET CONFIGURATION SYSTEM

WEB SERVICES IN ASP.NET

WEB SERVICES PART TWO

WEB SERVICE INFRASTRUCTURE

WEB SERVICE INFRASTRUCTURE PART TWO

EXCHANGING MESSAGES IN ASP.NET

MICROSOFT HAIL STORM

SOAP AND DOT NET PART ONE

SOAP AND DOT NET PART TWO

No comments:

Post a Comment