使用英语阅读

通过


WebControl.Style 属性

定义

获取将在 Web 服务器控件的外部标记上呈现为样式特性的文本特性的集合。

C#
[System.ComponentModel.Browsable(false)]
public System.Web.UI.CssStyleCollection Style { get; }

属性值

CssStyleCollection,它包含要在 Web 服务器控件的外部标记上呈现的 HTML 样式特性。

属性

示例

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

备注

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

ASP.NET (C#)
<%@ 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>

注解

Style使用 集合可管理 Web 服务器控件的外部标记中呈现的样式属性。 此属性将在所有控件的所有浏览器上呈现。

备注

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

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

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

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

另请参阅