CssStyleCollection Class
TOC
Collapse the table of content
Expand the table of content

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)

System.Object
  System.Web.UI.CssStyleCollection

public sealed class CssStyleCollection

NameDescription
System_CAPS_pubpropertyCount

Gets the number of items in the CssStyleCollection object.

System_CAPS_pubpropertyItem[HtmlTextWriterStyle]

Gets or sets the specified HtmlTextWriterStyle value for the HTML server control.

System_CAPS_pubpropertyItem[String]

Gets or sets the specified CSS value for the HTML server control.

System_CAPS_pubpropertyKeys

Gets a collection of keys to all the styles in the CssStyleCollection object for a specific HTML server control.

System_CAPS_pubpropertyValue

Gets or sets the value of the style attribute of the HTML server control.

NameDescription
System_CAPS_pubmethodAdd(HtmlTextWriterStyle, String)

Adds a style item to the CssStyleCollection collection of a control using the specified HtmlTextWriterStyle enumeration value and corresponding value.

System_CAPS_pubmethodAdd(String, String)

Adds a style item to the CssStyleCollection of a control using the specified name/value pair.

System_CAPS_pubmethodClear()

Removes all style items from the CssStyleCollection object.

System_CAPS_pubmethodEquals(Object)

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

System_CAPS_pubmethodGetHashCode()

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

System_CAPS_pubmethodGetType()

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

System_CAPS_pubmethodRemove(HtmlTextWriterStyle)

Removes a style item from the CssStyleCollection collection of a control using the specified HtmlTextWriterStyle enumeration value.

System_CAPS_pubmethodRemove(String)

Removes a style item from the CssStyleCollection of a control using the specified style key.

System_CAPS_pubmethodToString()

Returns 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="C#" %>
<%@ Import Namespace="System.Data" %>

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

<script runat="server">

  protected void Page_Load(object sender, EventArgs e)
  {
    DataTable dt = new DataTable();
    DataRow dr;
    dt.Columns.Add(new DataColumn("AttributeName", typeof(String)));
    dt.Columns.Add(new DataColumn("AttributeValue", typeof(String)));

    // The Style property of the MyText control returns
    // a CssStyleCollection object.
    IEnumerator keys = MyText.Style.Keys.GetEnumerator();

    while (keys.MoveNext())
    {
      String key = (String)keys.Current;
      dr = dt.NewRow();
      dr[0] = key;
      dr[1] = MyText.Style[key];
      dt.Rows.Add(dr);
    }
    DataView dv = new DataView(dt);
    MessageList.DataSource = dv;
    MessageList.DataBind();

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>CssStyleCollection Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:DataList id="MessageList"
                  runat="server">
      <HeaderStyle Font-Bold="true"/>
      <HeaderTemplate>
         HtmlInputText control's CssStyleCollection
      </HeaderTemplate>
      <ItemTemplate>
        Attribute: 
        <%# DataBinder.Eval(Container.DataItem, "AttributeName") %>
        , 
        Value: 
        <%# DataBinder.Eval(Container.DataItem, "AttributeValue") %>
      </ItemTemplate>
    </asp:DataList>
    <br />
    An input control with a style attribute:
    <br />
    <input id="MyText"
           type="text"  
           value="Type a value here." 
           style="font: 14pt verdana;width:300;"
           runat="server"/>
    </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
Show:
© 2016 Microsoft