Export (0) Print
Expand All

WebControl.CssClass Property

Gets or sets the Cascading Style Sheet (CSS) class rendered by the Web server control on the client.

[Visual Basic]
Public Overridable Property CssClass As String
[C#]
public virtual string CssClass {get; set;}
[C++]
public: __property virtual String* get_CssClass();
public: __property virtual void set_CssClass(String*);
[JScript]
public function get CssClass() : String;
public function set CssClass(String);

Property Value

The CSS class rendered by the Web server control on the client. The default is String.Empty.

Remarks

Use the CssClass property to specify the CSS class to render on the client for the Web Server control. This property will render on browsers for all controls. It will always be rendered as the class attribute, regardless of the browser.

For example, suppose you have the following Web server control declaration:

<asp:TextBox id="TextBox1" ForeColor="Red" CssClass="class1" />

The following HTML is rendered on the client for the previous Web server control declaration:

<input type=text class="class1" style="ForeColor:red">
Note   On browsers that do not support CSS, setting the CssClass property will have no effect.

Example

[Visual Basic, C#, JScript] The following example illustrates how to use the CssClass property to change the style of a HyperLink control.

[Visual Basic] 

<%@ Page Language="VB" AutoEventWireup="True" %>

 <html>
 <head>
 
   <style>
 
       .CssStyle1   
       { 
           font: 12pt verdana; 
           font-weight:700;
           color:orange;
       }
 
       .CssStyle2
       { 
           font: 15pt times; 
           font-weight:250;
           color:blue;
       }
 
   </style>
 
   <script language="VB" runat="server">
 
    Sub Button1_Click(sender As Object, e As EventArgs)
        If HyperLink1.CssClass = "CssStyle1" Then
            HyperLink1.CssClass = "CssStyle2"
        Else
            HyperLink1.CssClass = "CssStyle1"
        End If
    End Sub
 
   </script>
 
 </head>
 <body>
 
   <h3>CssClass Property of a Web Control</h3>
 
 <form runat="server">
 
   <asp:HyperLink id="HyperLink1" NavigateUrl="http://www.microsoft.com" 
     CssClass="spanstyle" Text="Click here to go to the Microsoft site" 
     Target="_new" runat="server"/>
 
   <p>
 
   <asp:Button id="Button1" Text="Click to change the Css style of the above link"
     OnClick="Button1_Click" runat="server"/>
 
 </form>
 
 </body>
 </html>
 

[C#] 

<%@ Page Language="C#" AutoEventWireup="True" %>

 <html>
 <head>
 
   <style>
 
       .CssStyle1   
       { 
           font: 12pt verdana; 
           font-weight:700;
           color:orange;
       }
 
       .CssStyle2
       { 
           font: 15pt times; 
           font-weight:250;
           color:blue;
       }
 
   </style>
 
   <script language="C#" runat="server">
 
       void Button1_Click(Object sender, EventArgs e) {
           HyperLink1.CssClass=((HyperLink1.CssClass=="CssStyle1")?"CssStyle2":"CssStyle1");
       }
 
   </script>
 
 </head>
 <body>
 
   <h3>CssClass Property of a Web Control</h3>
 
 <form runat="server">
 
   <asp:HyperLink id="HyperLink1" NavigateUrl="http://www.microsoft.com" 
     CssClass="spanstyle" Text="Click here to go to the Microsoft site" 
     Target="_new" runat="server"/>
 
   <p>
 
   <asp:Button id="Button1" Text="Click to change the Css style of the above link"
     OnClick="Button1_Click" runat="server"/>
 
 </form>
 
 </body>
 </html>
 

[JScript] 

<%@ Page Language="JScript" AutoEventWireup="True" %>

 <html>
 <head>
 
   <style>
 
       .CssStyle1   
       { 
           font: 12pt verdana; 
           font-weight:700;
           color:orange;
       }
 
       .CssStyle2
       { 
           font: 15pt times; 
           font-weight:250;
           color:blue;
       }
 
   </style>
 
   <script language="JSCRIPT" runat="server">
 
    function Button1_Click(sender : Object, e : EventArgs){
        if(HyperLink1.CssClass == "CssStyle1")
            HyperLink1.CssClass = "CssStyle2"
        else
            HyperLink1.CssClass = "CssStyle1"
    }
 
   </script>
 
 </head>
 <body>
 
   <h3>CssClass Property of a Web Control</h3>
 
 <form runat="server">
 
   <asp:HyperLink id="HyperLink1" NavigateUrl="http://www.microsoft.com" 
     CssClass="spanstyle" Text="Click here to go to the Microsoft site" 
     Target="_new" runat="server"/>
 
   <p>
 
   <asp:Button id="Button1" Text="Click to change the Css style of the above link"
     OnClick="Button1_Click" runat="server"/>
 
 </form>
 
 </body>
 </html>
 

[C++] No example is available for C++. To view a Visual Basic, C#, or JScript example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

WebControl Class | WebControl Members | System.Web.UI.WebControls Namespace

Show:
© 2014 Microsoft