WebControl.Enabled 属性

定义

获取或设置一个值,该值指示是否启用 Web 服务器控件。

public:
 virtual property bool Enabled { bool get(); void set(bool value); };
[System.ComponentModel.Bindable(true)]
public virtual bool Enabled { get; set; }
[System.ComponentModel.Bindable(true)]
[System.Web.UI.Themeable(false)]
public virtual bool Enabled { get; set; }
[<System.ComponentModel.Bindable(true)>]
member this.Enabled : bool with get, set
[<System.ComponentModel.Bindable(true)>]
[<System.Web.UI.Themeable(false)>]
member this.Enabled : bool with get, set
Public Overridable Property Enabled As Boolean

属性值

Boolean

如果启用控件,则为 true;否则为 false。 默认值为 true

属性

示例

以下示例演示如何通过以编程方式设置其Enabled属性来启用和禁用TextBox从基类继承的WebControl控件。

备注

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

重要

此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

<%@ 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 SubmitBtn1_Click(object sender, EventArgs e)
    {
        TextBox1.Enabled = (!TextBox1.Enabled);
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head2" runat="server">
    <title>Enabled Property Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h3>Enabled Property of a Web Control</h3>
            <p>
                <asp:TextBox id="TextBox1" BackColor="LightBlue" 
                    runat="server">Light Blue</asp:TextBox>
            </p>
            <p>
                <asp:TextBox id="TextBox2" BackColor="LightGreen" 
                    runat="server">Light Green</asp:TextBox>
            </p>
            <asp:Button id="SubmitBtn1" runat="server"
                Text="Click to disable or enable the light blue text box" 
                OnClick="SubmitBtn1_Click" />
    </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 SubmitBtn1_Click(ByVal sender As Object, ByVal e As EventArgs)
        TextBox1.Enabled = Not (TextBox1.Enabled)
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head2" runat="server">
    <title>Enabled Property Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h3>Enabled Property of a Web Control</h3>
            <p>
                <asp:TextBox id="TextBox1" BackColor="LightBlue" 
                    runat="server">Light Blue</asp:TextBox>
            </p>
            <p>
                <asp:TextBox id="TextBox2" BackColor="LightGreen" 
                    runat="server">Light Green</asp:TextBox>
            </p>
            <asp:Button id="SubmitBtn1" runat="server"
                Text="Click to disable or enable the light blue text box" 
                OnClick="SubmitBtn1_Click" />
    </div>
    </form>
</body>
</html>

注解

Enabled 控件的属性设置为 false时,控件通常显示为灰色。 如果控件是输入元素,浏览器会阻止用户单击或键入它。 为服务器控件呈现的 HTML 元素通过设置其 disabled 属性或 CSS class 属性被标记为禁用。 有关详细信息,请参阅 SupportsDisabledAttributeControlRenderingCompatibilityVersion

此属性将向下传播控件层次结构。 如果禁用容器控件,则也会禁用该容器中的子控件。 有关更多信息,请参见 IsEnabled 属性。

备注

在自定义复合控件中,此继承行为不适用于尚未创建其子控件的控件。 创建子控件时,必须设置子控件的启用状态,或重写属性 Enabled 以调用 EnsureChildControls 该方法。

备注

禁用控件只会阻止用户通过浏览器 UI 与控件交互。 用户有可能创建一个请求,该请求提交由页面处理的回发,即使页面上的控件被禁用也是如此。 在处理回发请求之前,请检查以确保控件已启用并可见。

并非所有控件都支持此属性。 有关详细信息,请参阅各个控件。

无法通过主题或样式表主题设置此属性。 有关详细信息,请参阅 ThemeableAttribute ASP.NET 主题和外观

适用于

另请参阅