Introduction to Web Controls


The Web Control



The controls on a web page are created from classes. The classes for web controls are defined in the System.Web.UI.WebControls namespace created in the System.Web.dll assembly. Normally, if you create a Web Site project in either Microsoft Visual Studio or Microsoft Visual Web Developer, this assembly is automatically added to the References list and the namespace is included in the code file of the web page.

The class ancestor to the web controls is called WebControl. WebControl is used to lay a common foundation to the controls on a web page.

Creating a Web Control

To create a web control, you can drag it from the Toolbox and drop it on a form of a web page. To manually create a web control, you start with a tag as follows:


In this case, replace Web-Control to the class name of the control. Because this is presented like an HTML tag, you must close it. You have two options. You can close the tag as done in traditional HTML:


Alternatively, you can use the starting tag to close itself. The formula is:

<asp:Web Control />

Common Characteristics of Web Controls



Because they belong to the same ancestor, most, if not all web controls share some characteristics.

Running at the Server

The behavior of a control is managed by the server that specify its characteristics. To make this possible, you must indicate this when you add a control.

If you visually add a control, it automatically receives an attribute named runat and whose value is server. If you are manually creating a control, to specify this characteristic, add runat="server" to its list of attributes:

<asp:Web-Control runat="server"></asp:Web-Control>

Identifying a Web Control

Just like every object on a computer must be identifier, a web control must also follow this rule. To make it possible to identify a web control, its tag is equipped with an attribute named ID. This attribute makes it possible to refer to a control in your code. To specify this characteristic, assign it the desired name for the control. Here is an example:

<asp:Web-Control ID="ctlCommonName" runat="server"></asp:Web-Control>

Loading a Control

When a web page is opening on a web page visitor's computer, each control must occupy some space in memory. To do that, it must be loaded in memory. As this is going on, the control fires an event named Load. You can use it to take any necessary early action before the user can take possession of the control.

Initializing a Control

When a web page comes up, the controls on it must be initialized to get ready to be displayed on the page. When this happens, each control fires an event named Init. You can use this event to take some action on the control.

Unloading a Control

After a visitor has used a web page, he or she might switch to another page or close the browser. At that time, each control on the web page must free the computer memory it was using. As this is being done, each control fires an event named Unload. Most of the times you don't need to do anything. Still, if you want, you can catch this event and do what you judge necessary.


The Attributes of a Web Control

The web controls used on a web page are implemented by classes of their names in the .NET Framework. You can use the class of a control to manipulate or manage it. Because web controls are firstly made to function on a web page, they are created as HTML tags.

Each attribute as a name and can have a value. To visually identify an attribute, on a web form, click the control. In the Properties window, click the Properties button. To specify or change the value of an attribute, click its name and type the value.

To manually access an attribute, type its name in the control's tag. To specify its value, assign it to the attribute. The formula is:

<asp:Web-Control Attribute="Value"></asp:Web-Control>

The attributes of a web control are stored in a list named Attributes. To programmatically get the list of attributes of a control, type its ID, followed by a period, followed by Attributes.

Enabling or Disabling a Control

To use a control on the form, the control must be enabled. This characteristics is controlled by a Boolean attribute named Enabled. Its default value is True, which means the control can be used. If you want to disable a control by default, set this attribute to False.

The Access Key of Web Control

To navigate among the controls on a web page, a visitor can keep pressing Tab. To help the user quickly move to a control, you can make it possible to press Alt + a key on the keyboard. To support this, the controls are equipped with an attribute named AccessKey.

To visually specify an access key, on the form, click the control. In the Properties section of the Properties window, click AccessKey and type a letter, such as A. Then, to access that control, the visitor can press Alt + A.

The Width of a Control

When you add a control to a web form, it assumes some default dimensions, especially the width. If the default width is not right, you can change it. To visually change the width in Microsoft Visual Studio or Microsoft Visual Web Developer, you can click the control, position the mouse on the right side, and drag to the right or to the left. As an alternative, click the control. In the Properties window, click Width and type the desired value.

The Style Sheet Used on a Control

When designing a control, you can specify its characteristics in the Properties window or using the attributes of the control's tag. An alternative is to create a style sheet that contains the desired classes.

To visually specify the desired class of a style to be applied to a control, on the form, click the control. In the Properties section of the Properties window, click CssClass and type the name of the CSS class. To manually specify the CSS class, access its CssClass attribute and assign the CSS class name to it.


Home Copyright © 2009-2016, FunctionX, Inc.