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,它包含要在 Web 服务器控件的外部标记上呈现的 HTML 样式特性。

属性

示例

以下示例演示如何使用 Style 属性在页面上隐藏或显示 Label 控件。

注意

下面的代码示例使用单文件代码模型,如果直接复制到代码隐藏文件中,可能无法正常工作。 必须将此代码示例复制到扩展名为 .aspx 的空文本文件中。 有关Web Forms代码模型的详细信息,请参阅 ASP.NET Web Forms页代码模型

<%@ 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使用 集合可管理 Web 服务器控件的外部标记中呈现的样式属性。 此属性将在所有控件的所有浏览器上呈现。

注意

不支持样式属性的浏览器将忽略呈现的 HTML。

例如,通过强类型样式属性设置的任何样式值 (, BackColor="Red") 将自动重写此集合中的相应值。

此集合中设置的值不会自动反映在强类型样式属性中。

适用于

另请参阅