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 ,请使用 方法的 Add 重载,该方法采用参数 HtmlTextWriterStyle ,因为它已强类型化。 这意味着,使用 属性的 get 访问器查看项时,将包含添加的 Item[] 项。

另请参阅

适用于

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,请使用 方法的AddAdd载,而不是不采用参数的HtmlTextWriterStyle重载。

另请参阅

适用于