CssStyleCollection 類別

定義

包含指定的 HTML 伺服器控制項的 HTML 階層式樣式表 (CSS) 內嵌樣式屬性 (Attribute)。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
繼承
CssStyleCollection

範例

下列程式碼範例會逐一查看 CssStyleCollection HtmlInputText 伺服器控制項的物件。The following code example iterates through the CssStyleCollection object for an HtmlInputText server control. Keys屬性是用來判斷已在伺服器控制項上宣告哪些樣式屬性,然後將屬性名稱和值系結至 DataList Web Form 頁面上的物件。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>
<%@ 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>

備註

當剖析包含的 Web Form 頁面時,會將任何針對特定 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. 您可以透過屬性操作伺服器控制項上的任何 CSS 屬性 StyleYou 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.

屬性

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.

方法

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.

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 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.

(繼承來源 Object)

適用於

另請參閱