Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [127]

By Root 5752 0
and events common to all HTML controls. Actually, many properties and all methods and events are simply inherited from the base class. Table 6-6 shows the list of properties specific to HTML controls.

Table 6-6. Specific Properties of an HTML Control

Property

Description

Attributes

Gets a collection object representing all the attributes set on the control with the corresponding value

Disabled

Gets or sets a Boolean value, which indicates whether the HTML control is disabled

Style

Gets a collection object representing all CSS properties applied to the control

TagName

Gets the name of the HTML tag behind the control

A disabled HTML server control is visible and always gets generated as HTML code. If the Disabled property is set to true, the disabled HTML attribute is inserted in the HTML output for the control. As mentioned earlier, if the Visible property is set to false, HTML is not generated for the control.

Working with HTML Attributes


Individual HTML controls feature more properties than just those listed in Table 6-6. Properties of HTML server controls map to HTML attributes, and the values assigned to the properties are replicated in the HTML output. For controls that don’t have an HTML direct counterpart, the Attributes collection is used to set attributes on the resulting HTML tag. This collection can also be used to set properties not mapped by the control’s interface and, if needed, to define custom HTML attributes. Any content of the Attributes collection is managed as a string.

Given the following HTML code snippet, let’s see how to programmatically set some attributes on the tag:

You bind a JavaScript script to the onload attribute of the tag. The resulting HTML code that the browser displays is as follows:

The Attributes property is rendered through a special type of class named AttributeCollection. In spite of the name, the content of the class is not directly enumerable using the for…each statement because the IEnumerable interface is not supported. The AttributeCollection class provides ad hoc methods to render attributes of a text writer object and to add and remove elements. Interestingly, if you add an attribute named Style, the class is smart enough to reroute the assigned content to the Style collection.

Note

In the previous example, the server-side code used to add the onload attribute to the body element has been written through a server