
114
|
Chapter 5: ASP.NET Server Controls
This is the Title of the Book, eMatter Edition
Copyright © 2008 O’Reilly & Associates, Inc. All rights reserved.
CSS Styles
A more extensive technique for modifying control appearance involves the use of
CSS styles. The base
HtmlControl class (from which all HTML controls inherit)
exposes a Style property, which contains a collection of CSS styles that are
rendered at runtime as attributes on the tag generated by the control. The base
WebControl class (from which all web controls inherit) also exposes a Style prop-
erty and adds a CssClass property, which renders the value of the property as a
class attribute on the control. This property allows you to set the style of a
control using a CSS class defined in a stylesheet, rather than by setting individual
styles. Like many other properties of web and HTML controls, the Style and
CssClass properties can be set either declaratively (using attributes) or program-
matically. Example 5-15 illustrates the use of both properties. Example 5-16
shows the HTML that would be rendered to the browser by Example 5-15. Note
that the ViewState hidden field has been removed for clarity.
Figure 5-12. Control properties
Example 5-15. ControlStyles.aspx
<%@ Page Language="vb" %>
<html>
<head>
<title>Control Properties Example</title>
<script runat="server">
Sub Page_Load( )
Label2.Style("background-color") = "silver"
End Sub
</script> ...