Duration (Pacific Standard Time):
To (Pacific Standard Time):
Impact:
User Action:

Style Class

 

Represents the style of a Web server control.

Namespace:   System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)


Public Class Style
	Inherits Component
	Implements IStateManager
Name Description
System_CAPS_pubmethod Style()

Initializes a new instance of the Style class using default values.

System_CAPS_pubmethod Style(StateBag)

Initializes a new instance of the Style class with the specified state bag information.

Name Description
System_CAPS_pubproperty BackColor

Gets or sets the background color of the Web server control.

System_CAPS_pubproperty BorderColor

Gets or sets the border color of the Web server control.

System_CAPS_pubproperty BorderStyle

Gets or sets the border style of the Web server control.

System_CAPS_pubproperty BorderWidth

Gets or sets the border width of the Web server control.

System_CAPS_protproperty CanRaiseEvents

Gets a value indicating whether the component can raise an event.(Inherited from Component.)

System_CAPS_pubproperty Container

Gets the IContainer that contains the Component.(Inherited from Component.)

System_CAPS_pubproperty CssClass

Gets or sets the cascading style sheet (CSS) class rendered by the Web server control on the client.

System_CAPS_protproperty DesignMode

Gets a value that indicates whether the Component is currently in design mode.(Inherited from Component.)

System_CAPS_protproperty Events

Gets the list of event handlers that are attached to this Component.(Inherited from Component.)

System_CAPS_pubproperty Font

Gets the font properties associated with the Web server control.

System_CAPS_pubproperty ForeColor

Gets or sets the foreground color (typically the color of the text) of the Web server control.

System_CAPS_pubproperty Height

Gets or sets the height of the Web server control.

System_CAPS_pubproperty IsEmpty

This API supports the product infrastructure and is not intended to be used directly from your code. A protected property. Gets a value indicating whether any style elements have been defined in the state bag.

System_CAPS_protproperty IsTrackingViewState

Returns a value indicating whether any style elements have been defined in the state bag.

System_CAPS_pubproperty RegisteredCssClass

Gets the cascading style sheet (CSS) class that is registered with the control.

System_CAPS_pubproperty Site

Gets or sets the ISite of the Component.(Inherited from Component.)

System_CAPS_protproperty ViewState

This API supports the product infrastructure and is not intended to be used directly from your code. Gets the state bag that holds the style elements.

System_CAPS_pubproperty Width

Gets or sets the width of the Web server control.

Name Description
System_CAPS_pubmethod AddAttributesToRender(HtmlTextWriter)

Adds HTML attributes and styles that need to be rendered to the specified System.Web.UI.HtmlTextWriter. This method is primarily used by control developers.

System_CAPS_pubmethod AddAttributesToRender(HtmlTextWriter, WebControl)

Adds HTML attributes and styles that need to be rendered to the specified System.Web.UI.HtmlTextWriter and Web server control. This method is primarily used by control developers.

System_CAPS_pubmethod CopyFrom(Style)

Duplicates the style properties of the specified Style into the instance of the Style class that this method is called from.

System_CAPS_pubmethod CreateObjRef(Type)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethod Dispose()

Releases all resources used by the Component.(Inherited from Component.)

System_CAPS_protmethod Dispose(Boolean)

Releases the unmanaged resources used by the Component and optionally releases the managed resources.(Inherited from Component.)

System_CAPS_pubmethod Equals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethod FillStyleAttributes(CssStyleCollection, IUrlResolutionService)

Adds the specified object's style properties to a System.Web.UI.CssStyleCollection object.

System_CAPS_protmethod Finalize()

Releases unmanaged resources and performs other cleanup operations before the Component is reclaimed by garbage collection.(Inherited from Component.)

System_CAPS_pubmethod GetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethod GetLifetimeService()

Retrieves the current lifetime service object that controls the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_protmethod GetService(Type)

Returns an object that represents a service provided by the Component or by its Container.(Inherited from Component.)

System_CAPS_pubmethod GetStyleAttributes(IUrlResolutionService)

Retrieves the System.Web.UI.CssStyleCollection object for the specified System.Web.UI.IUrlResolutionService-implemented object.

System_CAPS_pubmethod GetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethod InitializeLifetimeService()

Obtains a lifetime service object to control the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_protmethod LoadViewState(Object)

Loads the previously saved state.

System_CAPS_protmethod MemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_protmethod MemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethod MergeWith(Style)

Combines the style properties of the specified Style with the instance of the Style class that this method is called from.

System_CAPS_pubmethod Reset()

Removes any defined style elements from the state bag.

System_CAPS_protmethod SaveViewState()

A protected method. Saves any state that has been modified after the TrackViewState method was invoked.

System_CAPS_protmethod SetBit(Int32)

This API supports the product infrastructure and is not intended to be used directly from your code. A protected internal method. Sets an internal bitmask field that indicates the style properties that are stored in the state bag.

System_CAPS_pubmethod SetDirty()

Marks the Style so that its state will be recorded in view state.

System_CAPS_pubmethod ToString()

Returns a String containing the name of the Component, if any. This method should not be overridden.(Inherited from Component.)

System_CAPS_protmethod TrackViewState()

A protected method. Marks the beginning for tracking state changes on the control. Any changes made after tracking has begun will be tracked and saved as part of the control view state.

Name Description
System_CAPS_pubevent Disposed

Occurs when the component is disposed by a call to the Dispose method. (Inherited from Component.)

Name Description
System_CAPS_pubinterface System_CAPS_privmethod IStateManager.LoadViewState(Object)

This API supports the product infrastructure and is not intended to be used directly from your code. Loads the previously saved state.

System_CAPS_pubinterface System_CAPS_privmethod IStateManager.SaveViewState()

This API supports the product infrastructure and is not intended to be used directly from your code. Returns the object containing state changes.

System_CAPS_pubinterface System_CAPS_privmethod IStateManager.TrackViewState()

This API supports the product infrastructure and is not intended to be used directly from your code. Starts tracking state changes.

System_CAPS_pubinterface System_CAPS_privproperty IStateManager.IsTrackingViewState

Gets a value that indicates whether a server control is tracking its view state changes.

The Style class encapsulates the properties that control the appearance of a Web server control and can be applied to multiple Web server controls to provide a common appearance. You can specify the background color and font color of a control by setting the BackColor and ForeColor properties, respectively. On controls that can display a border, you can control the border width, the border style, and the border color by setting the BorderWidth, BorderStyle, and BorderColor properties. The size of a Web server control can also be specified by using the Height and Width properties.

This example demonstrates how to use a Style object to change the style properties of multiple controls at once. Each time one of the Style property values changes, each control must call its ApplyStyle method. Note that not all the controls included support all the properties demonstrated. If a control does not support a particular property, the appearance of the control will not change when the property value is changed.

System_CAPS_note Note

The following code sample uses the single-file code model and may not work correctly if copied directly into a code-behind file. This code sample must be copied into an empty text file that has an .aspx extension. For more information on the Web Forms code model, see ASP.NET Web Forms Page Code Model.

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Drawing" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    Private primaryStyle As New Style()

    Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not Page.IsPostBack Then
            ' Add data to the borderColorList, 
            ' backColorList, and foreColorList controls.
            Dim colors As New ListItemCollection()
            colors.Add(Color.Black.Name)
            colors.Add(Color.Blue.Name)
            colors.Add(Color.Green.Name)
            colors.Add(Color.Orange.Name)
            colors.Add(Color.Purple.Name)
            colors.Add(Color.Red.Name)
            colors.Add(Color.White.Name)
            colors.Add(Color.Yellow.Name)
            borderColorList.DataSource = colors
            borderColorList.DataBind()
            backColorList.DataSource = colors
            backColorList.DataBind()
            foreColorList.DataSource = colors
            foreColorList.DataBind()

            ' Add data to the borderStyleList control.
            Dim styles As New ListItemCollection()
            Dim styleType As Type = GetType(BorderStyle)
            Dim s As String
            For Each s In [Enum].GetNames(styleType)
                styles.Add(s)
            Next s
            borderStyleList.DataSource = styles
            borderStyleList.DataBind()

            ' Add data to the borderWidthList control.
            Dim widths As New ListItemCollection()
            Dim i As Integer
            For i = 0 To 10
                widths.Add(i.ToString() & "px")
            Next i
            borderWidthList.DataSource = widths
            borderWidthList.DataBind()

            ' Add data to the fontNameList control.
            Dim names As New ListItemCollection()
            names.Add("Arial")
            names.Add("Courier")
            names.Add("Garamond")
            names.Add("Times New Roman")
            names.Add("Verdana")
            fontNameList.DataSource = names
            fontNameList.DataBind()

            ' Add data to the fontSizeList control.
            Dim fontSizes As New ListItemCollection()
            fontSizes.Add("Small")
            fontSizes.Add("Medium")
            fontSizes.Add("Large")
            fontSizes.Add("10pt")
            fontSizes.Add("14pt")
            fontSizes.Add("20pt")
            fontSizeList.DataSource = fontSizes
            fontSizeList.DataBind()

            ' Set primaryStyle as the style for each control.
            Label1.ApplyStyle(primaryStyle)
            ListBox1.ApplyStyle(primaryStyle)
            Button1.ApplyStyle(primaryStyle)
            Table1.ApplyStyle(primaryStyle)
            TextBox1.ApplyStyle(primaryStyle)
        End If
    End Sub

    Sub ChangeBorderColor(ByVal sender As Object, ByVal e As System.EventArgs)
        primaryStyle.BorderColor = _
            Color.FromName(borderColorList.SelectedItem.Text)
        Label1.ApplyStyle(primaryStyle)
        ListBox1.ApplyStyle(primaryStyle)
        Button1.ApplyStyle(primaryStyle)
        Table1.ApplyStyle(primaryStyle)
        TextBox1.ApplyStyle(primaryStyle)
    End Sub

    Sub ChangeBackColor(ByVal sender As Object, ByVal e As System.EventArgs)
        primaryStyle.BackColor = _
            Color.FromName(backColorList.SelectedItem.Text)
        Label1.ApplyStyle(primaryStyle)
        ListBox1.ApplyStyle(primaryStyle)
        Button1.ApplyStyle(primaryStyle)
        Table1.ApplyStyle(primaryStyle)
        TextBox1.ApplyStyle(primaryStyle)
    End Sub

    Sub ChangeForeColor(ByVal sender As Object, ByVal e As System.EventArgs)
        primaryStyle.ForeColor = _
            Color.FromName(foreColorList.SelectedItem.Text)
        Label1.ApplyStyle(primaryStyle)
        ListBox1.ApplyStyle(primaryStyle)
        Button1.ApplyStyle(primaryStyle)
        Table1.ApplyStyle(primaryStyle)
        TextBox1.ApplyStyle(primaryStyle)
    End Sub

    Sub ChangeBorderStyle(ByVal sender As Object, ByVal e As System.EventArgs)
        primaryStyle.BorderStyle = _
            CType([Enum].Parse(GetType(BorderStyle), _
            borderStyleList.SelectedItem.Text), BorderStyle)
        Label1.ApplyStyle(primaryStyle)
        ListBox1.ApplyStyle(primaryStyle)
        Button1.ApplyStyle(primaryStyle)
        Table1.ApplyStyle(primaryStyle)
        TextBox1.ApplyStyle(primaryStyle)
    End Sub

    Sub ChangeBorderWidth(ByVal sender As Object, ByVal e As System.EventArgs)
        primaryStyle.BorderWidth = _
            Unit.Parse(borderWidthList.SelectedItem.Text)
        Label1.ApplyStyle(primaryStyle)
        ListBox1.ApplyStyle(primaryStyle)
        Button1.ApplyStyle(primaryStyle)
        Table1.ApplyStyle(primaryStyle)
        TextBox1.ApplyStyle(primaryStyle)
    End Sub

    Sub ChangeFont(ByVal sender As Object, ByVal e As System.EventArgs)
        primaryStyle.Font.Name = _
            fontNameList.SelectedItem.Text
        Label1.ApplyStyle(primaryStyle)
        ListBox1.ApplyStyle(primaryStyle)
        Button1.ApplyStyle(primaryStyle)
        Table1.ApplyStyle(primaryStyle)
        TextBox1.ApplyStyle(primaryStyle)
    End Sub

    Sub ChangeFontSize(ByVal sender As Object, ByVal e As System.EventArgs)
        primaryStyle.Font.Size = _
            FontUnit.Parse(fontSizeList.SelectedItem.Text)
        Label1.ApplyStyle(primaryStyle)
        ListBox1.ApplyStyle(primaryStyle)
        Button1.ApplyStyle(primaryStyle)
        Table1.ApplyStyle(primaryStyle)
        TextBox1.ApplyStyle(primaryStyle)
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Applied Style Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    <table cellpadding="6" border="0">
        <tr>
            <td rowspan="10" style="border:solid 1px Gray">
                <p>
                    <asp:label id="Label1" 
                        Text="Border Properties Example" Runat="server">
                        Label Styles
                    </asp:label>
                </p>
                <p>
                    <asp:button id="Button1" runat="server" 
                        Text="Button Styles">
                    </asp:button>
                </p>
                <p>
                    <asp:listbox id="ListBox1" Runat="server">
                        <asp:ListItem Value="0" Text="List Item 0">
                        </asp:ListItem>
                        <asp:ListItem Value="1" Text="List Item 1">
                        </asp:ListItem>
                        <asp:ListItem Value="2" Text="List Item 2">
                        </asp:ListItem>
                    </asp:listbox>
                </p>
                <p>
                    <asp:textbox id="TextBox1" 
                        Text="TextBox Styles" Runat="server">
                    </asp:textbox>
                </p>
                <p>
                    <asp:table id="Table1" Runat="server">
                        <asp:TableRow>
                            <asp:TableCell Text="(0,0)"></asp:TableCell>
                            <asp:TableCell Text="(0,1)"></asp:TableCell>
                        </asp:TableRow>
                        <asp:TableRow>
                            <asp:TableCell Text="(1,0)"></asp:TableCell>
                            <asp:TableCell Text="(1,1)"></asp:TableCell>
                        </asp:TableRow>
                    </asp:table>
                </p>
            </td>
            <td align="right">
                <asp:Label ID="Label2" runat="server" 
                    AssociatedControlID="borderColorList" 
                    Text="Border Color:">
                </asp:Label>
            </td>
            <td>
                <asp:dropdownlist id="borderColorList" 
                    Runat="server" AutoPostBack="True" 
                    OnSelectedIndexChanged="ChangeBorderColor">
                </asp:dropdownlist>
            </td>
        </tr>
        <tr>
            <td align="right">
                <asp:Label ID="Label3" Runat="server" 
                    AssociatedControlID="borderStyleList"
                    Text="Border Style:">
                </asp:Label>
            </td>
            <td>
                <asp:dropdownlist id="borderStyleList" 
                    Runat="server" AutoPostBack="True"
                    OnSelectedIndexChanged="ChangeBorderStyle">
                </asp:dropdownlist>
            </td>
        </tr>
        <tr>
            <td align="right">
                <asp:Label ID="Label4" Runat="server" 
                    AssociatedControlID="borderWidthList"
                    Text="Border Width">
                </asp:Label>
            </td>
            <td>
                <asp:dropdownlist id="borderWidthList" 
                    Runat="server" AutoPostBack="True"
                    OnSelectedIndexChanged="ChangeBorderWidth">
                </asp:dropdownlist>
            </td>
        </tr>
        <tr>
            <td align="right">
                <asp:Label ID="Label5" Runat="server" 
                    AssociatedControlID="backColorList"
                    Text="Back Color:">
                </asp:Label>
            </td>
            <td>
                <asp:dropdownlist id="backColorList" 
                    Runat="server" AutoPostBack="True"
                    OnSelectedIndexChanged="ChangeBackColor">
                </asp:dropdownlist>
            </td>
        </tr>
        <tr>
            <td align="right">
                <asp:Label ID="Label6" Runat="server" 
                    AssociatedControlID="foreColorList"
                    Text="Foreground Color:">
                </asp:Label>
            </td>
            <td>
                <asp:dropdownlist id="foreColorList" 
                    Runat="server" AutoPostBack="True"
                    OnSelectedIndexChanged="ChangeForeColor">
                </asp:dropdownlist>
            </td>
        </tr>
        <tr>
            <td align="right">
                <asp:Label ID="Label7" Runat="server" 
                    AssociatedControlID="fontNameList"
                    Text="Font Name:">
                </asp:Label>
            </td>
            <td>
                <asp:dropdownlist id="fontNameList" 
                    Runat="server" AutoPostBack="True"
                    OnSelectedIndexChanged="ChangeFont">
                </asp:dropdownlist>
            </td>
        </tr>
        <tr>
            <td align="right">
                <asp:Label ID="Label8" Runat="server" 
                    AssociatedControlID="fontSizeList"
                    Text="Font Size:">
                </asp:Label>
            </td>
            <td>
                <asp:dropdownlist id="fontSizeList" 
                    Runat="server" AutoPostBack="True" 
                    OnSelectedIndexChanged="ChangeFontSize">
                </asp:dropdownlist>
            </td>
        </tr>
    </table>

    </div>
    </form>
</body>
</html>
.NET Framework
Available since 1.1

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top