This documentation is archived and is not being maintained.

CssStyleCollection Class

Contains the HTML cascading-style sheets (CSS) inline style attributes for a specified HTML server control. This class cannot be inherited.


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

Public NotInheritable Class CssStyleCollection

The CssStyleCollection type exposes the following members.

Public propertyCountGets the number of items in the CssStyleCollection object.
Public propertyItem(HtmlTextWriterStyle)Gets or sets the specified HtmlTextWriterStyle value for the HTML server control.
Public propertyItem(String)Gets or sets the specified CSS value for the HTML server control.
Public propertyKeysGets a collection of keys to all the styles in the CssStyleCollection object for a specific HTML server control.
Public propertyValueGets or sets the value of the style attribute of the HTML server control.

Public methodAdd(HtmlTextWriterStyle, String)Adds a style item to the CssStyleCollection collection of a control using the specified HtmlTextWriterStyle enumeration value and corresponding value.
Public methodAdd(String, String)Adds a style item to the CssStyleCollection of a control using the specified name/value pair.
Public methodClearRemoves all style items from the CssStyleCollection object.
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodRemove(HtmlTextWriterStyle)Removes a style item from the CssStyleCollection collection of a control using the specified HtmlTextWriterStyle enumeration value.
Public methodRemove(String)Removes a style item from the CssStyleCollection of a control using the specified style key.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)

Any style declared for a particular HTML server control is added to the collection when the containing Web Forms page is parsed. It automatically parses and exposes CSS properties through a dictionary pattern API. You can manipulate any CSS property on a server control through the Style property. Simply use the CSS property's key and value in the indexed collection.

The following code example iterates through the CssStyleCollection object for an HtmlInputText server control. The Keys property is used to determine which style attributes have been declared on the server control, and then binds the attribute names and values to a DataList object on the Web Forms page.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<script runat="server">

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim dt As New DataTable()
    Dim dr As DataRow
    dt.Columns.Add(New DataColumn("AttributeName", GetType(String)))
    dt.Columns.Add(New DataColumn("AttributeValue", GetType(String)))

    ' The Style property of the MyText control returns
    ' a CssStyleCollection object.
    Dim keys As IEnumerator = MyText.Style.Keys.GetEnumerator()

    While keys.MoveNext()

      Dim key As [String] = CType(keys.Current, [String])
      dr = dt.NewRow()
      dr(0) = key
      dr(1) = MyText.Style(key)
    End While
    Dim dv As New DataView(dt)
    MessageList.DataSource = dv

  End Sub

<html xmlns="" >
<head runat="server">
    <title>CssStyleCollection Example</title>
    <form id="form1" runat="server">
    <asp:DataList id="MessageList"
      <HeaderStyle Font-Bold="true"/>
         HtmlInputText control's CssStyleCollection
        <%# DataBinder.Eval(Container.DataItem, "AttributeName") %>
        <%# DataBinder.Eval(Container.DataItem, "AttributeValue") %>
    <br />
    <input id="MyText"
           value="Type a value here." 
           style="font: 14pt verdana;width:300;"

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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