Applying Styles to Web Server Controls
Web server controls provide an additional level of support for styles by adding several strongly typed properties for commonly used
style settings, such as background and foreground color, font name and size, width, height, and so on. These style properties represent a
subset of the style behaviors available in HTML and are represented as "flat" properties exposed directly on the
System.Web.UI.WebControls.WebControl base class. The advantage of using these properties is that they provide
compile-time type checking and statement completion in development tools such as Microsoft Visual Studio .NET.
The following sample shows a WebCalendar control with several styles applied to it (a calendar without styles applied is included for
contrast). Note that when setting a property that is a class type, such as Font, you need to use the subproperty syntax
PropertyName-SubPropertyName .
The System.Web.UI.WebControls namespace includes a Style base class that encapsulates common style
attributes (additional style classes, such as TableStyle and TableItemStyle, inherit from
this common base class). Many Web server controls expose properties of this type for specifying the style of individual rendering
elements of the control. For example, the WebCalendar exposes many such style properties: DayStyle,
WeekendDayStyle, TodayDayStyle, SelectedDayStyle, OtherMonthDayStyle,
and NextPrevStyle. You can set individual properties of these styles using the subproperty syntax
PropertyName-SubPropertyName, as the following sample demonstrates.
A slightly different syntax allows each Style property to be declared as a child element nested within Web server control tags.
<ASP:Calendar ... runat="server">
<TitleStyle BorderColor="darkolivegreen" BorderWidth="3"
BackColor="olivedrab" Height="50px" />
</ASP:Calendar>
The following sample shows alternative syntax but is functionally equivalent to the preceding one.
As with HTML server controls, you can apply styles to Web server controls using a CSS class definition. The WebControl base class exposes a String property named CssClass for setting the style class:
If an attribute is set on a server control that does not correspond to any strongly typed property on the control, the attribute
and value are populated in the Attributes collection of the control. By default, server controls will render these attributes
unmodified in the HTML returned to the requesting browser client. This means that the style and class attributes can be
set on Web server controls directly instead of using the strongly typed properties. While this requires some understanding of the actual
rendering of the control, it can be a flexible way to apply styles as well. It is especially useful for the standard form input controls,
as illustrated in the following sample.
Web server control styles can also be set programmatically, using the ApplyStyle method of the base WebControl class, as in the following code.
<script language="VB" runat="server">
Sub Page_Load(Src As Object, E As EventArgs)
Dim MyStyle As New Style
MyStyle.BorderColor = Color.Black
MyStyle.BorderStyle = BorderStyle.Dashed
MyStyle.BorderWidth = New Unit(1)
MyLogin.ApplyStyle (MyStyle)
MyPassword.ApplyStyle (MyStyle)
MySubmit.ApplyStyle (MyStyle)
End Sub
</script>
Login: <ASP:TextBox id="MyLogin" runat="server" />/<p/>
Password: <ASP:TextBox id="MyPassword" TextMode="Password" runat="server" />
View: <ASP:DropDownList id="MySelect" runat="server"> ... </ASP:DropDownList>
VB
The following sample demonstrates the code above.
- ASP.NET's HTML server control and Web server control families provide first-class support for CSS styles.
- Styles may be applied by setting either the style or the class attributes of a control. These settings are accessible
programmatically through the control's Attributes collection. In the case of HTML server controls, individual values for style-attribute
keys can be retrieved from the control's Style collection.
- Most commonly used style settings are exposed on Web server controls as strongly typed properties of the control itself.
- The System.Web.UI.WebControls namespace includes a Style base class that encapsulates common style
attributes. Many Web server controls expose properties of this type to control individual rendering elements.
- Styles may be set programmatically on Web server controls using the ApplyStyle method of the WebControl base class.
Copyright 2001-2002 Microsoft Corporation. All rights reserved.