c# - .NET 4.5 中的 INamingContainer

标签 c# asp.net webforms inamingcontainer

我们最近从 .NET 3.5SP1 升级到了 .NET 4.5。我们发现使用 INamingContainer 接口(interface)的控件现在以不同方式呈现它们的 ClientID。

MS 文档说 INamingContainer 在控件的层次结构中创建了一个新的 ID 命名空间。

使用旧行为,我们的 ID 将如下所示:

<input id="MyContainer_txtName" />

但我们现在得到:

<input id="ctl00_ctl00_c_contentHolder_MyContainer_txtName" />

其中有 ct100 前缀和内容持有者控件名称。

使用它的示例类是:

public class MyTextBox : System.Web.UI.WebControls.TextBox, INamingContainer
{

}

为了向后兼容,我们设置了 ClientIDMode="AutoID"

我们可以像以前一样使用 ClientID="static" 手动为每个控件提供正确的 ID,但这对我们来说需要完成很多工作。

我想了解为什么 INamingContianer 没有像以前在旧版本的 .NET 中那样呈现。

提前致谢。

最佳答案

breaking change在 .NET4 中:

The ClientIDMode setting in ASP.NET 4 lets you specify how ASP.NET generates the id attribute for HTML elements. In previous versions of ASP.NET, the default behavior was equivalent to the AutoID setting of ClientIDMode. However, the default setting is now Predictable.

If you use Visual Studio 2010 to upgrade your application from ASP.NET 2.0 or ASP.NET 3.5, the tool automatically adds a setting to the Web.config file that preserves the behavior of earlier versions of the .NET Framework. However, if you upgrade an application by changing the application pool in IIS to target the .NET Framework 4, ASP.NET uses the new mode by default. To disable the new client ID mode, add the following setting in the Web.config file:

<pages ClientIDMode="AutoID" / >

关于c# - .NET 4.5 中的 INamingContainer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14378548/

相关文章:

.net - ASP.NET WebForm 的 'page lifecycle' 是什么?

webforms - 为什么 WHATWG 不允许 HTML 4 和 HTML5 中的嵌套表单?

asp.net - F# 命名空间或模块 'XXXX' 未定义

c# - Azure 文件共享 ListFilesAndDirectoriesSegmentedAsync() 身份验证失败

c# - 页面加载完成后,如何使用 ABCpdf 中的 AddImageUrl 生成 pdf

c# - 如何测量单个对象在 .NET 中占用的内存量

asp.net - Razor Pages ASP.Net Core 2.0 Routing - URL 中间是否可以有参数?

asp.net - EntityFramework Core 在加入后获得多个结果

c# - 在 IIS 6 中托管 .NET 4.0 Web 服务

c# - 正则表达式或其他方法可从特定位置在C#中使用字符串