简体   繁体   English

ASP.Net 4中ClientIDMode的正确设置是什么,以获得ASP.Net 2.0渲染。

[英]What is the correct setting of ClientIDMode in ASP.Net 4 to get ASP.Net 2.0 rendering.

We have just updated our application from ASP.Net 2.0 to ASP.Net 4.0. 我们刚刚将我们的应用程序从ASP.Net 2.0更新到ASP.Net 4.0。

We have included in the web.config in the <system.web> element: 我们已经包含在<system.web>元素中的web.config中:

<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID" />

My understanding is that this is supposed to render the controls the same as .Net 2.0/3.5 would. 我的理解是,这应该使控件与.Net 2.0 / 3.5相同。

However... it's not... here is one example 但是......它不是......这是一个例子

This is rendered in 2.0: 这是在2.0中呈现的:

<input id="grdUserEntity__ctl1_chkSelectAll" type="checkbox"
   name="grdUserEntity:_ctl1:chkSelectAll" onclick="javascript:iSelectAll();" />

This is in 4.0: 这是在4.0:

<input id="grdUserEntity_ctl01_chkSelectAll" type="checkbox" 
   name="grdUserEntity$ctl01$chkSelectAll" onclick="javascript:iSelectAll();" />

The difference: 区别:

2.0 id=grdUserEntity__ctl1_chkSelectAll
4.0 id=grdUserEntity_ctl01_chkSelectAll

According to what I read that config setting will cause ASP.Net 4.0 to render the server controls and client id's identically to the previous version. 根据我读到的,配置设置将导致ASP.Net 4.0呈现服务器控件和客户端ID与先前版本相同。

What are we doing wrong? 我们做错了什么?

There was a change to how IDs were rendered from ASP.NET 2.0 to ASP.NET 3.5. 从ASP.NET 2.0到ASP.NET 3.5的ID呈现方式发生了变化。 Since you're going from 2.0 to 4.0, you're still seeing that difference. 因为你从2.0到4.0,你仍然看到了这种差异。 The change was due to XHTML compliance improvements. 这一变化是由于XHTML合规性的改进。

You can try switching back to the 2.0 rendering with the xhtmlCompliance compat setting. 您可以尝试使用xhtmlCompliance compat设置切换回2.0渲染。 Yet another compat setting, yes :) It should work, but honestly, I'm not sure how well tested that old compat setting is in 4.0, and I know it wouldn't be compatible with the UpdatePanel, if you were planning on using that. 另一个compat设置,是:)它应该工作,但老实说,我不确定如何测试旧的compat设置是在4.0,我知道它将与UpdatePanel不兼容,如果你打算使用那。

Is there a reason why you want to keep the 2.0 rendering? 您有理由保留2.0渲染吗? Just fear of regressions, or do you have any known actual regressions? 只是害怕回归,或者你有任何已知的实际回归?

XHTML setting: http://msdn.microsoft.com/en-us/library/ms178159.aspx XHTML设置: http//msdn.microsoft.com/en-us/library/ms178159.aspx

In a 4.0 application pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID" will completely mess up styling in the Site.master page. 在4.0应用程序pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"将完全pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID" Site.master页面中的样式。 For example, menus will be almost invisible in the designer and will completely lose their styling in the browser. 例如,菜单在设计器中几乎不可见,并且将在浏览器中完全失去其样式。 Basically, styling will be lost in the designer. 基本上,设计师将失去造型。

I found this by setting the target framework from 4.0 to 3.5 and back to 4.0. 我通过将目标框架从4.0设置为3.5并返回到4.0来找到了这一点。 After this, the styling was convoluted. 在此之后,造型令人费解。 After comparing all the files to a good app, I finally found this line in web.config . 在将所有文件与一个好的应用程序进行比较后,我终于在web.config找到了这一行。 I removed it, and my styling problems disappeared. 我删除它,我的造型问题消失了。

For future readers of this post, you can mitigate compatibility issues by using the <%=objectid.ClientId %> construct in your ASP.NET page. 对于本文的未来读者,您可以通过在ASP.NET页面中使用<%=objectid.ClientId %>构造来缓解兼容性问题。

Example: Suppose you textbox called txtInput (that ASP.NET renders as id=ctl00_cphMainContent_txtInput) that you need to reference in some client-side javascript code. 示例:假设您需要在某些客户端javascript代码中引用的文本框名为txtInput(ASP.NET呈现为id = ctl00_cphMainContent_txtInput)。 You could reference that object with the following javascript code in your ASP.NET page: 您可以在ASP.NET页面中使用以下javascript代码引用该对象:

str txtInputObjNm = "<%=txtInput.ClientId %>";

At runtime, it will be automatically translated into the following client-side javascript: 在运行时,它将自动转换为以下客户端javascript:

str txtInputObjNm = "ctl00_cphMainContent_txtInput";

If .NET "decides" to change the way the clientid is assigned, your code will still work. 如果.NET“决定”改变clientid的分配方式,那么您的代码仍然有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM