WebControl.SupportsDisabledAttribute 屬性

定義

取得值,這個值表示當控制項的 disabled 屬性為 IsEnabled 時,控制項是否應該將呈現之 HTML 項目的 false 屬性設為 "disabled"。Gets a value that indicates whether the control should set the disabled attribute of the rendered HTML element to "disabled" when the control's IsEnabled property is false.

public:
 virtual property bool SupportsDisabledAttribute { bool get(); };
[System.ComponentModel.Browsable(false)]
public virtual bool SupportsDisabledAttribute { get; }
[<System.ComponentModel.Browsable(false)>]
member this.SupportsDisabledAttribute : bool
Public Overridable ReadOnly Property SupportsDisabledAttribute As Boolean

屬性值

Boolean

一定是 trueAlways true.

屬性

範例

下列範例顯示已停用控制項的標記 LabelThe following example shows markup for a disabled Label control:

<asp:Label id="Label1" runat="server" Text="Test" Enabled="false"/>

在 ASP.NET 3.5 和更早版本中,此標記會呈現下列 HTML:In ASP.NET 3.5 and earlier versions, this markup renders the following HTML:

<span id="Label1" disabled="disabled">Test</span>

在 ASP.NET 4 中,如果屬性的值 RenderingCompatibility 是4.0 或更大,而且 DisabledCssClass 屬性設定為其預設值,則相同的標記會呈現下列 HTML:In ASP.NET 4, if the value of the RenderingCompatibility property is 4.0 or greater, and if the DisabledCssClass property is set to its default value, the same markup renders the following HTML:

<span id="Label1" class="aspNetDisabled">Test</span>

備註

在衍生類別中,這個屬性會指出當控制項停用時,ASP.NET 應該如何呈現控制項的 HTML。In derived classes, this property indicates how ASP.NET should render HTML for a control when the control is disabled.

停用 ASP.NET Web 控制項Disabling ASP.NET Web Controls

Enabled ASP.NET 控制項的屬性設定為 false (也就是停用控制項時) ,其意圖是 ASP.NET 應該在頁面中轉譯控制項,但會將它顯示為停用的狀態 (通常會呈現暗灰色的) 。When the Enabled property of an ASP.NET control is set to false (that is, when the control is disabled), the intent is that ASP.NET should render the control in the page, but display it in a disabled state (typically dimmed). 如果父控制項已停用,該控制項的子系也會顯示為停用。If a parent control is disabled, children of that control are also displayed as disabled. 在此情況下,子控制項的 IsEnabled 屬性會設定為 false ,即使其 Enabled 屬性可能設定為也一樣 trueIn that case, the child control's IsEnabled property is set to false, even though its Enabled property might be set to true.

在 ASP.NET 3.5 和更早版本中,當控制項的 IsEnabled 屬性為時 false ,ASP.NET 會 disabled 在針對控制項呈現的 HTML 專案中,將屬性設定為「已停用」。In ASP.NET 3.5 and earlier versions, when a control's IsEnabled property is false, ASP.NET sets the disabled attribute to "disabled" in the HTML element that it renders for the control. 不過,HTML 4.01 標準只允許在 disabled 用於使用者輸入的特定元素上使用屬性。However, the HTML 4.01 standard allows the disabled attribute only on certain elements that are used for user input. 例如,允許專案的 disabled 屬性 input ,但不允許 span 元素。For example, the disabled attribute is allowed for input elements, but not for span elements.

大部分的瀏覽器都支援具有屬性之僅顯示元素的預設停用外觀 disabledMost browsers support a default disabled appearance for display-only elements that have disabled attributes. 不過,為了符合 HTML 標準,ASP.NET 4 預設會省略不正確屬性。However, in order to comply with HTML standards, by default ASP.NET 4 omits invalid attributes.

如何使用 SupportsDisabledAttribute 屬性How the SupportsDisabledAttribute Property is Used

如果 SupportsDisabledAttribute 屬性為 truedisabled 當停用控制項時,ASP.NET 會呈現屬性。If the SupportsDisabledAttribute property is true, ASP.NET renders a disabled attribute when the control is disabled. 如果這個屬性為 false ,ASP.NET class 會在控制項停用時轉譯屬性。If this property is false, ASP.NET renders a class attribute when the control is disabled.

false如果下列所有條件都成立,則衍生類別會覆寫此屬性以傳回:Derived classes override this property to return false if all the following conditions are true:

  • 控制項會呈現不支援屬性的 HTML 專案 disabledThe control renders an HTML element that does not support the disabled attribute.

  • RenderingCompatibility屬性指出低於4.0 的 ASP.NET 版本號碼。The RenderingCompatibility property indicates an ASP.NET version number lower than 4.0.

SupportsDisabledAttribute屬性不一定會指出針對控制項所呈現的 HTML 元素是否支援 (允許) disabled 屬性。The SupportsDisabledAttribute property does not necessarily indicate whether the HTML element that is rendered for a control supports (allows) the disabled attribute. 如果控制項的 RenderingCompatibility 屬性指出低於4.0 的 ASP.NET 版本號碼, SupportsDisabledAttribute 即使轉譯 true 的 HTML 不支援屬性,控制項的屬性值也可能會傳回 disabledIf a control's RenderingCompatibility property indicates an ASP.NET version number lower than 4.0, the value of the control's SupportsDisabledAttribute property might return true even if the rendered HTML does not support the disabled attribute.

停用之控制項的回溯相容性Backward Compatibility for Disabled Controls

當您使用 Visual Studio 將 Web 專案從較舊的版本升級至 ASP.NET 4 時,Visual Studio 會自動 controlRenderingCompatibilityVersion 在 Web.config 檔中設定屬性, (會將 RenderingCompatibility 屬性) 設定為較早的版本號碼。When you use Visual Studio to upgrade a Web project to ASP.NET 4 from an earlier version, Visual Studio automatically sets the controlRenderingCompatibilityVersion attribute in the Web.config file (which sets the RenderingCompatibility property) to the earlier version number. 因為屬性 SupportsDisabledAttribute true RenderingCompatibility (property)低於4.0 時一律會傳回屬性(property),所以這會導致 Web 控制項轉譯已停用之控制項的 HTML,如同在舊版中所做的一樣。Because the SupportsDisabledAttribute property always returns true when the RenderingCompatibility property is lower than 4.0, this causes Web controls to render HTML for disabled controls as they did in the earlier version. 如果您想要升級的網站使用 ASP.NET 4 中引進的演算法轉譯 HTML,您可以變更或移除 controlRenderingCompatibilityVersion 屬性。If you want an upgraded Web site to render HTML using the algorithm introduced in ASP.NET 4, you can change or remove the controlRenderingCompatibilityVersion attribute. 如需詳細資訊,請參閱 RenderingCompatibility 屬性 (Property)。For more information, see the RenderingCompatibility property.

使用 CSS 設定停用的外觀Setting a Disabled Appearance By Using CSS

如果 SupportsDisabledAttribute 控制項的屬性為 false ,且控制項已停用,ASP.NET 會將轉譯的 HTML 專案的 class 屬性設定為屬性的值 WebControl.DisabledCssClassIf the SupportsDisabledAttribute property of a control is false and the control is disabled, ASP.NET sets the class attribute of the rendered HTML element to the value of the WebControl.DisabledCssClass property. 屬性的預設值 WebControl.DisabledCssClass 為 "aspNetDisabled"。The default value of the WebControl.DisabledCssClass property is "aspNetDisabled".

若要為停用的控制項提供停用的外觀,您必須為屬性值所表示的類別定義 CSS 規則 WebControl.DisabledCssClassTo provide a disabled appearance for disabled controls, you must define a CSS rule for the class that is represented by the value of the WebControl.DisabledCssClass property.

如果控制項的 class 屬性中有一個值,則針對控制項轉譯的 HTML 元素可能會在其屬性中有一個以上的值 CssClassThe HTML element that is rendered for a control might have more than one value in its class attribute if there is a value in its CssClass property. 如需詳細資訊,請參閱 DisabledCssClass 屬性 (Property)。For more information, see the DisabledCssClass property.

適用於

另請參閱