ASP.NET

Introduction to Web Controls

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…

4 min read977 words6 code samples

ASP.NET With C#: Introduction to Web Controls

Introduction to Web Controls#

The Web Control#

Introduction#

Web page controls are built using classes found within the System.Web.UI.WebControls namespace, which resides in the System.Web.dll assembly. When starting a new Web Site project in Microsoft Visual Studio or Microsoft Visual Web Developer, this specific assembly is typically included in the project's references, and the namespace is automatically available in your code-behind files. The base class for these elements is WebControl, providing a standardized structure for all controls on a 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:

C#
<asp:Web-Control>

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:

C#
<asp:Web-Control></asp:Web-Control>

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

C#
<asp:Web Control />

Common Characteristics of Web Controls#

Introduction#

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:

C#
<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:

C#
<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#

When a user navigates away from a page or exits their browser, the controls on that page need to release the system memory they occupied. This cleanup process triggers an Unload event for each control. While handling this event is often unnecessary, you can implement logic here if your application requires specific end-of-lifecycle actions.

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:

C#
<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#

Control properties can be defined either through the Properties window or by setting attributes directly within the control's tag. Another approach involves using external style sheets with predefined classes. To apply a CSS class visually, select the control and enter the class name in the CssClass field of the Properties window. If you prefer manual configuration, you can assign the class name to the CssClass attribute of the control. Home Copyright � 2009 FunctionX