CssStyleCollection.Item[] 屬性

定義

取得或設定 HTML 伺服器控制項的 CSS 值。

多載

Item[String]

取得或設定 HTML 伺服器控制項指定的 CSS 值。

Item[HtmlTextWriterStyle]

取得或設定 HTML 伺服器控制項指定的 HtmlTextWriterStyle 值。

Item[String]

取得或設定 HTML 伺服器控制項指定的 CSS 值。

public:
 property System::String ^ default[System::String ^] { System::String ^ get(System::String ^ key); void set(System::String ^ key, System::String ^ value); };
public string this[string key] { get; set; }
member this.Item(string) : string with get, set
Default Public Property Item(key As String) As String

參數

key
String

CSS 屬性的索引。

屬性值

String

key 的值。

範例

下列程式碼範例會 Item[] 使用 屬性傳回控制項上 HtmlInputText 設定的樣式值。

<%@ Page Language="C#" %>

<!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)
  {
    MyText.Style.Add(HtmlTextWriterStyle.Width, "200");
    FirstMessage.Text = "The text box font color is: " + 
      MyText.Style["color"] + "<br />" +
      "The text box width is: " + 
      MyText.Style[HtmlTextWriterStyle.Width];
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>CssCollection This Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <input id="MyText"
             type="text" 
             value="Type a value here."
             style="font: 14pt Verdana; color: blue;"
             runat="server"/>
      <br />
      <asp:Label id="FirstMessage"
                 runat="server"/>
    </div>
    </form>
</body>
</html>
<%@ Page Language="VB" %>

<!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)

    MyText.Style.Add(HtmlTextWriterStyle.Width, "200")
    FirstMessage.Text = "The text box font color is: " _
      & MyText.Style("color") & "<br />" _
      & "The text box width is: " & MyText.Style(HtmlTextWriterStyle.Width)

  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>CssCollection This Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <input id="MyText"
             type="text" 
             value="Type a value here."
             style="font: 14pt Verdana; color: blue;"
             runat="server"/>
      <br />
      <asp:Label id="FirstMessage"
                 runat="server"/>
    </div>
    </form>
</body>
</html>

另請參閱

適用於

Item[HtmlTextWriterStyle]

取得或設定 HTML 伺服器控制項指定的 HtmlTextWriterStyle 值。

public:
 property System::String ^ default[System::Web::UI::HtmlTextWriterStyle] { System::String ^ get(System::Web::UI::HtmlTextWriterStyle key); void set(System::Web::UI::HtmlTextWriterStyle key, System::String ^ value); };
public string this[System.Web.UI.HtmlTextWriterStyle key] { get; set; }
member this.Item(System.Web.UI.HtmlTextWriterStyle) : string with get, set
Default Public Property Item(key As HtmlTextWriterStyle) As String

參數

屬性值

String

key,如果 key 不在伺服器控制項集合中,則為 null

範例

下列程式碼範例會 Item[] 使用 屬性傳回控制項上 HtmlInputText 設定的樣式值。

<%@ Page Language="C#" %>

<!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)
  {
    MyText.Style.Add(HtmlTextWriterStyle.Width, "200");
    FirstMessage.Text = "The text box font color is: " + 
      MyText.Style["color"] + "<br />" +
      "The text box width is: " + 
      MyText.Style[HtmlTextWriterStyle.Width];
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>CssCollection This Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <input id="MyText"
             type="text" 
             value="Type a value here."
             style="font: 14pt Verdana; color: blue;"
             runat="server"/>
      <br />
      <asp:Label id="FirstMessage"
                 runat="server"/>
    </div>
    </form>
</body>
</html>
<%@ Page Language="VB" %>

<!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)

    MyText.Style.Add(HtmlTextWriterStyle.Width, "200")
    FirstMessage.Text = "The text box font color is: " _
      & MyText.Style("color") & "<br />" _
      & "The text box width is: " & MyText.Style(HtmlTextWriterStyle.Width)

  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>CssCollection This Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <input id="MyText"
             type="text" 
             value="Type a value here."
             style="font: 14pt Verdana; color: blue;"
             runat="server"/>
      <br />
      <asp:Label id="FirstMessage"
                 runat="server"/>
    </div>
    </form>
</body>
</html>

備註

以程式設計方式新增至 CssStyleCollection 具有 Item[] 屬性之控制項之集合的樣式專案,或 Add 採用 HtmlTextWriterStyle 列舉值做為輸入的方法,會包含在屬性傳 Count 回的樣式專案計數中,並隨附于屬性所 Value 傳回的字串常值中。 這些屬性是強型別。 如果您想要新增至伺服器控制項的 HtmlTextWriterStyle 級聯樣式表單屬性存在於列舉中,請使用這些強型別的方法。 如果您想要加入的屬性不存在於列舉中 HtmlTextWriterStyle ,請使用 Item[] 屬性或 Add 方法。

嘗試存取 HtmlTextWriterStyle 尚未加入的 控制項中的 CssStyleCollection 列舉值,或存取已使用 Item[] 屬性或 方法加入的 Add CSS 屬性,會使用 Item[] 屬性傳回 null

另請參閱

適用於