共用方式為


WebControl.Style 屬性

定義

會取得一組文字屬性,這些屬性會被渲染成 Web 伺服器控制項外層標籤上的樣式屬性。

public:
 property System::Web::UI::CssStyleCollection ^ Style { System::Web::UI::CssStyleCollection ^ get(); };
[System.ComponentModel.Browsable(false)]
public System.Web.UI.CssStyleCollection Style { get; }
[<System.ComponentModel.Browsable(false)>]
member this.Style : System.Web.UI.CssStyleCollection
Public ReadOnly Property Style As CssStyleCollection

屬性值

CssStyleCollection A 包含 HTML 風格屬性,需在網頁伺服器控制項的外部標籤上渲染。

屬性

範例

以下範例說明如何利用該 Style 屬性在頁面上隱藏或顯示 Label 控制項。

備註

以下程式碼範例採用單檔案程式碼模型,若直接複製到程式碼背後的檔案中,可能無法正常運作。 此程式碼範例必須複製到一個具有 .aspx 副檔名的空文字檔中。 欲了解更多關於網頁表單程式碼模型的資訊,請參見 ASP.NET 網頁表單頁面程式碼模型

<%@ 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">
 
    void Button1_Click(Object sender, EventArgs e)
    {
        if (Label1.Style["visibility"] == "hidden")
            Label1.Style["visibility"] = "show";
        else
            Label1.Style["visibility"] = "hidden";
    }
 
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head2" runat="server">
    <title>Style Property of a Web Control</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
        <h3>Style Property of a Web Control</h3>
 
        <asp:Label id="Label1" Text="This is a label control." 
            BorderStyle="Solid" runat="server"/>

        <p>
            <asp:Button id="Button1" 
                Text="Click to hide or unhide the label"
                OnClick="Button1_Click" runat="server"/>
        </p>

    </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">
 
    Sub Button1_Click(sender As Object, e As EventArgs)
        If Label1.Style("visibility") = "hidden" Then
            Label1.Style("visibility") = "show"
        Else
            Label1.Style("visibility") = "hidden"
        End If
    End Sub
 
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head2" runat="server">
    <title>Style Property Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
        <h3>Style Property of a Web Control</h3>
 
        <asp:Label id="Label1" Text="This is a label control." 
            BorderStyle="Solid" runat="server"/>

        <p>
            <asp:Button id="Button1" Text="Click to hide or unhide the label"
                OnClick="Button1_Click" runat="server"/>
        </p>

    </div>
    </form>
</body>
</html>

備註

使用該 Style 集合來管理網頁伺服器控制項外部標籤中呈現的樣式屬性。 這個屬性會在所有瀏覽器上渲染所有控制項。

備註

不支援樣式屬性的瀏覽器會忽略渲染出來的 HTML。

任何透過強型別的樣式屬性設定的樣式值(例如, BackColor="Red"),都會自動覆寫此集合中對應的值。

此集合中設定的值不會自動反映在強型別的樣式屬性中。

適用於

另請參閱