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 Form程式碼模型的詳細資訊,請參閱ASP.NET Web Forms頁碼模型

重要

這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 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 時,控制項通常會呈現暗灰色。 如果控制項是輸入元素,瀏覽器會防止使用者按一下或輸入。 為伺服器控制項轉譯的 HTML 元素會藉由設定其 disabled 屬性或其 CSS class 屬性來標示為停用。 如需詳細資訊,請參閱 SupportsDisabledAttributeControlRenderingCompatibilityVersion

這個屬性會向下傳播控制項階層。 如果您停用容器控制項,該容器內的子控制項也會停用。 如需詳細資訊,請參閱 IsEnabled 屬性 (Property)。

注意

在自訂複合控制項中,此繼承行為不適用於尚未建立其子控制項的控制項。 建立子控制項時,您必須設定子控制項的啟用狀態,或覆寫 Enabled 屬性以呼叫 EnsureChildControls 方法。

注意

停用控制項只會防止使用者透過瀏覽器 UI 與控制項互動。 即使頁面上的控制項已停用,使用者還是可以製作提交由頁面處理的回傳要求。 在您處理回傳要求之前,請先檢查以確定控制項已啟用且可見。

並非所有控制項都支援這個屬性。 如需詳細資訊,請參閱個別控制項。

這個屬性無法由佈景主題或樣式表主題設定。 如需詳細資訊,請參閱 ThemeableAttributeASP.NET 主題和外觀

適用於

另請參閱