asp.net.ph

Style.CssClass Property

System.Web.UI.WebControls Namespace   Style Class


Sets or retrieves the Cascading Style Sheet ( CSS ) class applied to a Web control.

Syntax


Inline <asp:control CssClass = strClass ... >
Script Style.CssClass = strClass

Property Value


strClass String specifying the CSS class that is applied to the control.

This property is read/write with no default value.

Remarks

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.

Example

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:

  1. the user chooses a class name from the <select> control named myStyle.
  2. <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>
  3. the submit button's click event is wired to a handler named chgClass.
  4. <input type=submit Value = "Apply" runat = "server"
       onServerClick = "chgClass">
  5. 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.
  6. void chgClass ( Object src, EventArgs e ) {
       // get the selected style and apply
       greeting.CssClass = myStyle.Value;
    }

 Show me 

See Also

Style Members   Base Web Control Properties Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph