System.Web.UI.WebControls Namespace Style Class
Sets or retrieves the Cascading Style Sheet ( CSS ) class applied to a Web control.
Inline |
<asp:control CssClass = strClass ... > |
Script |
Style.CssClass = strClass |
strClass |
String specifying the CSS class that is applied to the control. |
This property is read/write with no default value.
CssClass is used to associate a particular CSS style rule with a Web server control. The style rule may be defined in either an embedded or linked stylesheet.
The below snippet shows a sample declaration of an embedded stylesheet, with three CSS class definitions named cool, soso, and warm.
<style type = "text/css">
<!--
#greeting {padding:10}
.cool {background:navy; color:lime}
.soso {background:gainsboro; color:dimgray}
.warm {background:maroon; color:gold}
-->
</style>
NOTE: This property will always render as an HTML class attribute, regardless of whether the current client browser supports the attribute or not. However, setting the CssClass property may not yield the desired effect on browsers that do not support CSS.
The following example shows how to declaratively set the CssClass property of a Web control at design time, using one of the classes defined in the sample stylesheet above.
<asp:textbox id = "myTextBox" cssclass = "cool" runat = "server" />
The example below shows how to programmatically set the CssClass property at run time, depending on user input. Essentially, here's how the code works:
- the user chooses a class name from the <select> control named myStyle.
<p>Choose your style: <select id = "myStyle" runat = "server">
<option value = "cool">Cool</option>
<option value = "soso">So-So</option>
<option value = "warm">Warm</option>
</select>
- the submit button's click event is wired to a handler named chgClass.
<input type=submit Value = "Apply" runat = "server"
onServerClick = "chgClass">
- chgClass simply retrieves the class name selected from myStyle, and applies this as the value of the CssClass property of the <asp:Label> control named greeting.
void chgClass ( Object src, EventArgs e ) {
// get the selected style and apply
greeting.CssClass = myStyle.Value;
}
Show me
Style Members Base Web Control Properties