Programming Microsoft ASP.NET 4 - Dino Esposito [136]
Style
Gets a CssStyleCollection collection object made of all the attributes assigned to the outer tag of the Web control.
SupportDisabledAttribute
Returns true for the WebControl base class.
TabIndex
Gets or sets the tab index of the control.
ToolTip
Gets or sets the text displayed when the mouse pointer hovers over the control.
Width
Gets or sets the width of the control. The width is expressed as a member of type Unit.
The ControlStyle and ControlStyleCreated properties are used primarily by control developers, while the Style property is what application developers typically use to set CSS attributes on the outer tag of the control. The Style property is implemented using an instance of the class CssStyleCollection. The CssStyleCollection class is a simple collection of strings like those you assign to the HTML style attribute.
Styling Web Controls
The ControlStyle property evaluates to an object of type Style—a class that encapsulates the appearance properties of the control. The Style class groups together some of the properties that were shown in Table 6-9, and it works as the repository of the graphical and cosmetic attributes that characterize all Web controls. The grouped properties are BackColor, BorderColor, BorderStyle, BorderWidth, CssClass, Font, ForeColor, Height, and Width. All properties of the Style class are strongly typed. The properties just mentioned are not persisted to the view state individually, but they benefit from the serialization machinery supported by the Style object.
It should be clear by now that the Style class is quite different from the Style property, whose type is CssStyleCollection. Note that style values set through the Style property are not automatically reflected by the (strongly typed) values in the Style object. For example, you can set the CSS border-style through the Style property, but that value won’t be reflected by the value of the BorderStyle property.
// Set the border color through a CSS attribute
MyControl.Style["border"] = "solid 1px black";
// Set the border color through an ASP.NET style property
MyControl.BorderColor = Color.Red;
So what happens if you run the preceding code snippet? Which setting would win? When a control is going to render, the contents of both the ControlStyle and Style properties are rendered to HTML style attributes. The ControlStyle property is processed first, so in the case of overlapping settings the value stuffed in Style, which is processed later, ultimately wins.
Managing the Style of Web Controls
The style properties of a Web control can be programmatically manipulated to some extent. For example, in the Style class, you can count on a CopyFrom method to duplicate the object and on the MergeWith method to combine two style objects.
currentStyle.MergeStyle(newStyle);
The MergeWith method joins the properties of both objects. In doing so, it does not replace any property that is already set in the base object but limits itself to defining uninitialized properties. Finally, the Reset method clears all current attributes in the various properties of the style object.
Note
I already mentioned this point a few times, but the best practice today is having ASP.NET controls emit style-ignorant markup. The emitted markup then will be decorated at will and made as colorful and attractive as it needs to be by using external CSS classes. In light of this, all of the control style features of ASP.NET lose much of their original appeal.
Methods of Web Controls
The WebControl class supports a few additional methods that are not part of the base Control class. These methods are listed in Table 6-10.
Table 6-10. Specific Methods of Web Controls
Method
Description
ApplyStyle
Copies any nonempty elements of the specified style object to the control. Existing style properties are overwritten.
CopyBaseAttributes
Imports from the specified Web control the properties AccessKey, Enabled, ToolTip, TabIndex, and Attributes. Basically, it copies all the properties not encapsulated in