WebControl.Enabled WebControl.Enabled WebControl.Enabled WebControl.Enabled Property

定义

获取或设置一个值,该值指示是否启用 Web 服务器控件。Gets or sets a value indicating whether the Web server control is enabled.

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

属性值

如果启用控件,则为 true;否则为 falsetrue if control is enabled; otherwise, false. 默认值为 trueThe default is true.

示例

下面的示例通过以编程方式设置TextBox Enabled控件的属性,说明如何启用WebControl和禁用从基类继承的控件。The following example illustrates how to enable and disable a TextBox control, inherited from the WebControl base class, by setting its Enabled property programmatically.

备注

下面的代码示例使用单文件代码模型,如果直接复制到代码隐藏文件中,则可能无法正常工作。The following code sample uses the single-file code model and may not work correctly if copied directly into a code-behind file. 必须将此代码示例复制到扩展名为 .aspx 的空文本文件中。This code sample must be copied into an empty text file that has an .aspx extension. 有关 Web 窗体代码模型的详细信息,请参阅ASP.NET Web 窗体页代码模型For more information on the Web Forms code model, see ASP.NET Web Forms Page Code Model.

重要

此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。This example has a text box that accepts user input, which is a potential security threat. 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。By default, ASP.NET Web pages validate that user input does not include script or HTML elements. 有关详细信息,请参阅脚本侵入概述For more information, see Script Exploits Overview.

<%@ 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通常显示为灰色。When the Enabled property of a control is set to false, the control typically appears dimmed. 如果该控件是一个输入元素,则浏览器将阻止用户在其中单击或键入。If the control is an input element, the browser prevents the user from clicking or typing in it. 通过设置disabled为服务器控件呈现的 HTML 元素的特性或其 CSS class特性来将其标记为禁用。HTML elements that are rendered for a server control are marked as disabled by setting their disabled attribute or their CSS class attribute. 有关详细信息,请参阅 SupportsDisabledAttributeControlRenderingCompatibilityVersionFor more information, see SupportsDisabledAttribute and ControlRenderingCompatibilityVersion.

此属性向下传播控件层次结构。This property propagates down the control hierarchy. 如果禁用容器控件,则也会禁用该容器中的子控件。If you disable a container control, the child controls within that container are also disabled. 有关更多信息,请参见 IsEnabled 属性。For more information, see the IsEnabled property.

备注

在自定义复合控件中,此继承行为不适用于尚未创建其子控件的控件。In a custom composite control, this inheritance behavior does not apply to controls that have not yet created their child controls. 创建子控件时,必须设置其启用状态,或重写Enabled属性以EnsureChildControls调用方法。You must either set the enabled state of the child controls when they are created, or override the Enabled property to call the EnsureChildControls method.

备注

禁用控件只会阻止用户通过浏览器 UI 与控件进行交互。Disabling a control only prevents interaction with the control by the user through the browser UI. 用户可以创建一个请求,该请求将提交由页面处理的回发,即使已禁用页面上的控件。It is possible for a user to craft a request that submits a postback that is processed by the page even if controls on the page are disabled. 在处理回发请求之前,请检查以确保已启用并显示该控件。Before you process a postback request, check to make sure that the control is enabled and visible.

并非所有控件都支持此属性。Not all controls support this property. 有关详细信息,请参阅各个控件。See the individual controls for details.

无法通过主题或样式表主题设置此属性。This property cannot be set by themes or style sheet themes. 有关详细信息,请ThemeableAttribute参阅和ASP.NET 主题和外观For more information, see ThemeableAttribute and ASP.NET Themes and Skins.

适用于

另请参阅