CssStyleCollection Class

Definition

包含指定 HTML 服务器控件的 HTML 级联式样式表 (CSS) 内嵌样式特性。Contains the HTML cascading-style sheets (CSS) inline style attributes for a specified HTML server control. 此类不能被继承。This class cannot be inherited.

public ref class CssStyleCollection sealed
public sealed class CssStyleCollection
type CssStyleCollection = class
Public NotInheritable Class CssStyleCollection
Inheritance
CssStyleCollection

Examples

下面的代码示例将循环访问CssStyleCollection HtmlInputText服务器控件的对象。The following code example iterates through the CssStyleCollection object for an HtmlInputText server control. 属性用于确定已在服务器控件上声明了哪些样式属性, 然后将属性名称和值绑定DataList到 Web 窗体页上的对象。 KeysThe 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>
<%@ Page Language="VB" %>
<%@ 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 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)
      dt.Rows.Add(dr)
    End While
    Dim dv As New DataView(dt)
    MessageList.DataSource = dv
    MessageList.DataBind()

  End Sub
</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 />
    <input id="MyText"
           type="text"  
           value="Type a value here." 
           style="font: 14pt verdana;width:300;"
           runat="server"/>
    </div>
    </form>
</body></html>

Remarks

当对包含 Web 窗体页进行分析时, 为特定 HTML 服务器控件声明的任何样式将添加到集合中。Any style declared for a particular HTML server control is added to the collection when the containing Web Forms page is parsed. 它通过字典模式 API 自动分析和公开 CSS 属性。It automatically parses and exposes CSS properties through a dictionary pattern API. 您可以通过Style属性操作服务器控件上的任何 CSS 属性。You can manipulate any CSS property on a server control through the Style property. 只需使用索引集合中的 CSS 属性的键和值。Simply use the CSS property's key and value in the indexed collection.

Properties

Count

获取 CssStyleCollection 对象中的项数。Gets the number of items in the CssStyleCollection object.

Item[HtmlTextWriterStyle]

获取或设置 HTML 服务器控件的指定 HtmlTextWriterStyle 值。Gets or sets the specified HtmlTextWriterStyle value for the HTML server control.

Item[String]

获取或设置 HTML 服务器控件的指定 CSS 值。Gets or sets the specified CSS value for the HTML server control.

Keys

获取特定 HTML 服务器控件的 CssStyleCollection 对象中所有样式的键集合。Gets a collection of keys to all the styles in the CssStyleCollection object for a specific HTML server control.

Value

获取或设置 HTML 服务器控件的 style 特性的值。Gets or sets the value of the style attribute of the HTML server control.

Methods

Add(HtmlTextWriterStyle, String)

使用指定的 CssStyleCollection 枚举值和相应的值将样式项添加到控件的 HtmlTextWriterStyle 集合。Adds a style item to the CssStyleCollection collection of a control using the specified HtmlTextWriterStyle enumeration value and corresponding value.

Add(String, String)

使用指定的名称/值对将样式项添加到控件的 CssStyleCollectionAdds a style item to the CssStyleCollection of a control using the specified name/value pair.

Clear()

将所有样式项从 CssStyleCollection 对象中移除。Removes all style items from the CssStyleCollection object.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
Remove(HtmlTextWriterStyle)

使用指定的 CssStyleCollection 枚举值从控件的 HtmlTextWriterStyle 集合移除样式项。Removes a style item from the CssStyleCollection collection of a control using the specified HtmlTextWriterStyle enumeration value.

Remove(String)

使用指定的样式键从控件的 CssStyleCollection 中移除样式项。Removes a style item from the CssStyleCollection of a control using the specified style key.

ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

Applies to

See also