共用方式為


CssStyleCollection.Add 方法

定義

將樣式項目加入至控制項的 CssStyleCollection

多載

Add(String, String)

使用指定的名稱/值組,將樣式項目加入至控制項的 CssStyleCollection

Add(HtmlTextWriterStyle, String)

使用指定的 CssStyleCollection 列舉值和對應值,將樣式項目加入至控制項的 HtmlTextWriterStyle 集合。

Add(String, String)

使用指定的名稱/值組,將樣式項目加入至控制項的 CssStyleCollection

public:
 void Add(System::String ^ key, System::String ^ value);
public void Add (string key, string value);
member this.Add : string * string -> unit
Public Sub Add (key As String, value As String)

參數

key
String

要加入至集合的新樣式屬性名稱。

value
String

要加入至集合的樣式屬性值。

例外狀況

keynull

範例

下列程式碼範例示範如何使用 方法的 Add 這兩個多載,以程式設計方式將串聯樣式表單新增至 HtmlSelectHtmlInputSubmitLabel 控制項。

<%@ 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 SubmitBtn_Click(object sender, EventArgs e)
  {
    SubmitBtn.Style.Add("letter-spacing", "10px");
    FirstSelect.Style.Add(HtmlTextWriterStyle.Color, FirstSelect.Items[FirstSelect.SelectedIndex].Value.ToString());
    Message.Style.Add(HtmlTextWriterStyle.Color, FirstSelect.Items[FirstSelect.SelectedIndex].Value.ToString());
    Message.Text = "The select style is: " + FirstSelect.Style.Value;
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>CssStyleCollection Add</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Select a color and then click submit.
    <br />
    <select id="FirstSelect" 
            style="font: 10pt verdana;color:black;" 
            runat="server">
        <option value="black">black</option>
        <option value="red">red</option>
        <option value="blue">blue</option>
        <option value="green">green</option>
    </select> 
    <input id="SubmitBtn" 
           value="Submit" 
           type="submit" 
           onserverclick="SubmitBtn_Click"
           runat="server" /><br/>
    <br />
    <asp:Label id="Message"
               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 SubmitBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    
    SubmitBtn.Style.Add("letter-spacing", "10px")
    FirstSelect.Style.Add(HtmlTextWriterStyle.Color, FirstSelect.Items(FirstSelect.SelectedIndex).Value.ToString())
    Message.Style.Add(HtmlTextWriterStyle.Color, FirstSelect.Items(FirstSelect.SelectedIndex).Value.ToString())
    Message.Text = "The select style is: " + FirstSelect.Style.Value

  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>CssStyleCollection Add</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Select a color and then click submit.
    <br />
    <select id="FirstSelect" 
            style="font: 10pt verdana;color:black;" 
            runat="server">
        <option value="black">black</option>
        <option value="red">red</option>
        <option value="blue">blue</option>
        <option value="green">green</option>
    </select> 
    <input id="SubmitBtn" 
           value="Submit" 
           type="submit" 
           onserverclick="SubmitBtn_Click"
           runat="server" /><br />
    <br />
    <asp:Label id="Message"
               runat="server"/>
    </div>
    </form>
</body>
</html>

備註

如果要加入的樣式專案存在於列舉中 HtmlTextWriterStyle ,請使用採用 HtmlTextWriterStyle 參數的方法多載 Add ,因為它具有強型別。 這表示,使用 屬性的 Item[] get 存取子檢視專案時,將會包含新增的專案。

另請參閱

適用於

Add(HtmlTextWriterStyle, String)

使用指定的 CssStyleCollection 列舉值和對應值,將樣式項目加入至控制項的 HtmlTextWriterStyle 集合。

public:
 void Add(System::Web::UI::HtmlTextWriterStyle key, System::String ^ value);
public void Add (System.Web.UI.HtmlTextWriterStyle key, string value);
member this.Add : System.Web.UI.HtmlTextWriterStyle * string -> unit
Public Sub Add (key As HtmlTextWriterStyle, value As String)

參數

key
HtmlTextWriterStyle

要加入到集合中的 HtmlTextWriterStyle 列舉值。

value
String

要加入至集合的樣式屬性值。

範例

下列程式碼範例示範如何使用 方法的 Add 這兩個多載,以程式設計方式將串聯樣式表單新增至 HtmlSelectHtmlInputSubmitLabel 控制項。

<%@ 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 SubmitBtn_Click(object sender, EventArgs e)
  {
    SubmitBtn.Style.Add("letter-spacing", "10px");
    FirstSelect.Style.Add(HtmlTextWriterStyle.Color, FirstSelect.Items[FirstSelect.SelectedIndex].Value.ToString());
    Message.Style.Add(HtmlTextWriterStyle.Color, FirstSelect.Items[FirstSelect.SelectedIndex].Value.ToString());
    Message.Text = "The select style is: " + FirstSelect.Style.Value;
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>CssStyleCollection Add</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Select a color and then click submit.
    <br />
    <select id="FirstSelect" 
            style="font: 10pt verdana;color:black;" 
            runat="server">
        <option value="black">black</option>
        <option value="red">red</option>
        <option value="blue">blue</option>
        <option value="green">green</option>
    </select> 
    <input id="SubmitBtn" 
           value="Submit" 
           type="submit" 
           onserverclick="SubmitBtn_Click"
           runat="server" /><br/>
    <br />
    <asp:Label id="Message"
               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 SubmitBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    
    SubmitBtn.Style.Add("letter-spacing", "10px")
    FirstSelect.Style.Add(HtmlTextWriterStyle.Color, FirstSelect.Items(FirstSelect.SelectedIndex).Value.ToString())
    Message.Style.Add(HtmlTextWriterStyle.Color, FirstSelect.Items(FirstSelect.SelectedIndex).Value.ToString())
    Message.Text = "The select style is: " + FirstSelect.Style.Value

  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>CssStyleCollection Add</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Select a color and then click submit.
    <br />
    <select id="FirstSelect" 
            style="font: 10pt verdana;color:black;" 
            runat="server">
        <option value="black">black</option>
        <option value="red">red</option>
        <option value="blue">blue</option>
        <option value="green">green</option>
    </select> 
    <input id="SubmitBtn" 
           value="Submit" 
           type="submit" 
           onserverclick="SubmitBtn_Click"
           runat="server" /><br />
    <br />
    <asp:Label id="Message"
               runat="server"/>
    </div>
    </form>
</body>
</html>

備註

如果要加入的樣式專案存在於列舉中 HtmlTextWriterStyle ,請使用 方法的 Add 這個 Add 多載,而不是不採用 HtmlTextWriterStyle 參數的多載。

另請參閱

適用於