Control.RenderingCompatibility 属性

定义

获取一个值,该值指定呈现的 HTML 将与之兼容的 ASP.NET 版本。Gets a value that specifies the ASP.NET version that rendered HTML will be compatible with.

public:
 virtual property Version ^ RenderingCompatibility { Version ^ get(); void set(Version ^ value); };
[System.ComponentModel.Bindable(false)]
[System.ComponentModel.Browsable(false)]
public virtual Version RenderingCompatibility { get; set; }
member this.RenderingCompatibility : Version with get, set
Public Overridable Property RenderingCompatibility As Version

属性值

呈现的 HTML 将与之兼容的 ASP.NET 版本。The ASP.NET version that rendered HTML will be compatible with.

属性

注解

ASP.NET 将此属性设置为 Web.config 文件中 pages 元素的 controlRenderingCompatibilityVersion 特性的值。ASP.NET sets this property to the value of the controlRenderingCompatibilityVersion attribute of the pages element in the Web.config file. 如果未在 web.config 文件中设置 controlRenderingCompatibilityVersion 属性,则默认值为 ASP.NET 的当前版本。If the controlRenderingCompatibilityVersion attribute is not set in the Web.config file, the default value is the current version of ASP.NET.

注意

此属性有一个公共 set 访问器,但 set 访问器支持 .NET Framework 基础结构,不应在代码中直接使用。There is a public set accessor for this property, but the set accessor supports the .NET Framework infrastructure and is not intended to be used directly from your code. 如果您在代码中设置此值,则效果是不可预知的。If you set this value in your code, the effect is unpredictable.

每个版本的 ASP.NET 可能会以不同于早期版本的方式呈现 HTML。Each release of ASP.NET might render HTML differently from earlier releases. 例如,在 ASP.NET 3.5 中,如果 Label 控件的 IsEnabled 属性为 false,则默认情况下,ASP.NET 将呈现一个 span 元素,其 disabled 特性设置为 "disabled"。For example, in ASP.NET 3.5, if the IsEnabled property of a Label control is false, by default, ASP.NET renders a span element whose disabled attribute is set to "disabled". 在 ASP.NET 4 中,默认情况下,使用级联样式表(CSS) class 特性而不是 disabled 特性呈现 span 元素。In ASP.NET 4, by default, the span element is rendered with a cascading style sheet (CSS) class attribute instead of the disabled attribute. 这使您可以指定控件的禁用外观,并避免呈现无效的 HTML。This lets you specify the disabled appearance of the control and avoids rendering invalid HTML. (在 HTML 4.0 和 XHTML 1.1 中,span 元素不支持 disabled 特性。)(In HTML 4.0 and XHTML 1.1, the span element does not support the disabled attribute.)

如果 HTML 呈现发生更改,则 Web 应用程序可能包含无法正常工作的代码。A Web application might include code that would not function correctly if HTML rendering changes. 若要避免此问题,可以在 web.config 文件中设置 pages 元素的 controlRenderingCompatibilityVersion 属性,以指示要保持与兼容的早期版本。To avoid this problem, you can set the controlRenderingCompatibilityVersion attribute of the pages element in the Web.config file to indicate which earlier version you want to maintain compatibility with. 例如,如果将 RenderingCompatibility 属性设置为 3.5,禁用的 Label 控件将呈现 disabled 特性而不是 CSS 类。For example, if you set the RenderingCompatibility property to 3.5, a disabled Label control will render a disabled attribute and not a CSS class.

备注

可将此属性设置为 3.5的最早版本。The earliest version that you can set this property to is 3.5.

为了保持向后兼容性,使用 Visual Studio 将 Web 项目从早期版本升级到 ASP.NET 4 时,Visual Studio 会自动将 web.config 文件中的 controlRenderingCompatibilityVersion 属性设置为 3.5To maintain backward compatibility, 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 to 3.5. 如果希望已升级的网站使用 ASP.NET 4 中引入的算法呈现 HTML,则可以更改或删除 controlRenderingCompatibilityVersion 特性。If you want an upgraded Web site to render HTML using the algorithm that was introduced in ASP.NET 4, you can change or remove the controlRenderingCompatibilityVersion attribute.

大多数情况下,此属性控制的行为是自动的,无需在代码中检查 RenderingCompatibility 属性。Most of the time, the behavior controlled by this property is automatic and you do not have to check the RenderingCompatibility property in your code. 但是,如果你正在编程自定义控件,则可能必须包含用于根据此属性的设置更改控件行为的代码。However, if you are programming a custom control, you might have to include code that alters the control's behavior based on the setting of this property. 例如,ASP.NET 4 的自定义控件可能由 Label 控件组成,自定义控件通过生成更改 aspNetDisabled 类的 JavaScript 代码,可以指定控件的禁用外观。For example, a custom control for ASP.NET 4 might be composed of Label controls, and the custom control might specify the disabled appearance of the control by generating JavaScript code that alters the aspNetDisabled class. 如果 RenderingCompatibility 4.0 或更高版本,则此操作将按预期方式工作。This will work as expected if RenderingCompatibility is 4.0 or later. 但是,若要在 RenderingCompatibility 3.5时获得相同的效果,在 falseIsEnabled 属性时,自定义控件的代码必须将控件的 CssClass 属性设置为 "aspNetDisabled"。But to get the same effect when RenderingCompatibility is 3.5, the custom control's code must set the control's CssClass property to "aspNetDisabled" when the IsEnabled property is false.

适用于

另请参阅