WebControl.SupportsDisabledAttribute 属性
定义
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
属性值
总是为 true。Always 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
当 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 属性可能设置为,子控件的属性也会设置为 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 属性为时,ASP.NET 会将 false 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
如果 SupportsDisabledAttribute 属性为 true ,则 disabled 在禁用控件时,ASP.NET 将呈现属性。If 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 thedisabledattribute.RenderingCompatibility属性指示 ASP.NET 的版本号低于4.0。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 属性指示 ASP.NET 的版本号低于4.0,则 SupportsDisabledAttribute true 即使呈现的 HTML 不支持特性,控件的属性的值也可能会返回 disabled 。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 会自动 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 属性低于4.0 时始终返回,因此,这会导致 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 属性。For more information, see the RenderingCompatibility property.
使用 CSS 设置禁用的外观Setting a Disabled Appearance By Using CSS
如果 SupportsDisabledAttribute 控件的属性为, false 并且该控件处于禁用状态,则 ASP.NET 会将 class 呈现的 HTML 元素的特性设置为属性的值 WebControl.DisabledCssClass 。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".
若要为禁用的控件提供禁用的外观,必须为由属性的值表示的类定义 CSS 规则 WebControl.DisabledCssClass 。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.