CssStyleCollection.Keys Property


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

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

public ICollection Keys { get; }

Property Value

Type: System.Collections.ICollection

A collection of keys contained in the CssStyleCollection for the specified HTML server control.

The following code example demonstrates how to iterate through the CssStyleCollection collection for an HtmlInputText server control. The Keys property is used to determine which style attribute has been declared on the server control and 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];
    DataView dv = new DataView(dt);
    MessageList.DataSource = dv;



<html xmlns="http://www.w3.org/1999/xhtml" >
<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 />
    An input control with a style attribute:
    <br />
    <input id="MyText"
           value="Type a value here." 
           style="font: 14pt verdana;width:300;"

.NET Framework
Available since 1.1
Return to top