WebControl.SupportsDisabledAttribute 属性

定义

获取一个值,该值指示在控件的 IsEnabled 属性为 disabled 时,控件是否应将呈现的 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; }
member this.SupportsDisabledAttribute : bool
Public Overridable ReadOnly Property SupportsDisabledAttribute As Boolean

属性值

总是为 trueAlways true.

属性

示例

下面的示例显示了已禁用Label控件的标记:The 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

当 ASP.NET Enabled控件的属性设置为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 ,即使Enabled子控件的属性可能false设置为true,子控件的属性也会设置为。In 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 元素中的属性设置为 "disabled"。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.

大多数浏览器支持具有disabled属性的仅显示元素的默认禁用外观。Most 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

如果属性为true,则在禁用控件disabled时,ASP.NET 将呈现属性。 SupportsDisabledAttributeIf the SupportsDisabledAttribute property is true, ASP.NET renders a disabled attribute when the control is disabled. 如果此属性为false,则在禁用class控件时,ASP.NET 将呈现特性。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 元素不支持disabled特性。The control renders an HTML element that does not support the disabled attribute.

  • RenderingCompatibility属性指示 ASP.NET 的版本号低于4.0。The RenderingCompatibility property indicates an ASP.NET version number lower than 4.0.

属性不一定指示为控件呈现的 HTML 元素是否支持(允许disabled )特性。 SupportsDisabledAttributeThe SupportsDisabledAttribute property does not necessarily indicate whether the HTML element that is rendered for a control supports (allows) the disabled attribute. 如果控件的RenderingCompatibility属性指示 ASP.NET 的版本号低于4.0,则即使呈现的 HTML 不支持disabled特性, SupportsDisabledAttribute控件的属性true的值也可能会返回。If 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 会自动将 web.config 文件中controlRenderingCompatibilityVersion的属性( 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. 由于属性在RenderingCompatibility属性低于true 4.0 时始终返回,因此,这会导致 Web 控件呈现已禁用控件在早期版本中的 HTML。 SupportsDisabledAttributeBecause 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 属性。For more information, see the RenderingCompatibility property.

使用 CSS 设置禁用的外观Setting a Disabled Appearance By Using CSS

如果控件SupportsDisabledAttribute的属性为,并且false该控件处于class禁用状态,则 ASP.NET 会将WebControl.DisabledCssClass呈现的 HTML 元素的特性设置为属性的值。If 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".

若要为禁用的控件提供禁用的外观,必须为由WebControl.DisabledCssClass属性的值表示的类定义 CSS 规则。To 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.

如果为控件呈现的 HTML 元素的class CssClass属性中有值,则它在其属性中可能具有多个值。The 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 属性。For more information, see the DisabledCssClass property.

适用于

另请参阅