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 Forms 程式碼模型的詳細資訊,請參閱ASP.NET Web Form 頁面程式碼模型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 Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。By default, ASP.NET Web pages validate that user input does not include script or HTML elements. 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。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. 針對伺服器控制項轉譯的 HTML 專案會藉由設定其disabled屬性或其 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 屬性 (Property)。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.

適用於

另請參閱