Control.RenderingCompatibility 屬性


取得值,這個值會指定將與呈現 HTML 相容的 ASP.NET 版本。Gets a value that specifies the ASP.NET version that rendered HTML will be compatible with.

 virtual property Version ^ RenderingCompatibility { Version ^ get(); void set(Version ^ value); };
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 會轉譯其 disabled 屬性設為 "disabled" 的 span 元素。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 中,根據預設,span 元素會使用級聯樣式表(CSS) class 屬性來呈現,而不是 disabled 屬性。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.5The 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. 但若要在 3.5RenderingCompatibility 時取得相同的效果,當 IsEnabled 屬性為 false時,自訂控制項的程式碼必須將控制項的 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.