แก้ไข

แชร์ผ่าน


WebControl.Enabled Property

Definition

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)]
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

Property Value

true if control is enabled; otherwise, false. The default is true.

Attributes

Examples

The following example illustrates how to enable and disable a TextBox control, inherited from the WebControl base class, by setting its Enabled property programmatically.

Note

The following code sample uses the single-file code model and may not work correctly if copied directly into a code-behind file. This code sample must be copied into an empty text file that has an .aspx extension. For more information on the Web Forms code model, see ASP.NET Web Forms Page Code Model.

Important

This example has a text box that accepts user input, which is a potential security threat. 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>

Remarks

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. HTML elements that are rendered for a server control are marked as disabled by setting their disabled attribute or their CSS class attribute. For 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. For more information, see the IsEnabled property.

Note

In a custom composite control, this inheritance behavior does not apply to controls that have not yet created their child controls. 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.

Note

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. For more information, see ThemeableAttribute and ASP.NET Themes and Skins.

Applies to

See also