当您有这样的 ASP 控件时:
<asp:TreeView ID="TreeItems" runat="server"></asp:TreeView>
它生成的 html 会破坏名称。如果我想直接访问生成的项目的 id,我可以尝试找出它对名称的影响并查找该 ID。
生成的项目名称是否保证按照 Microsoft 的任何标准以特定方式完成?我只是担心如果他们发布以不同方式实现这一点的新版本 .NET,就会出现这种情况。有没有办法在代码中生成自己修改的名称?
When you have an ASP control like this:
<asp:TreeView ID="TreeItems" runat="server"></asp:TreeView>
The html that it generates mangles the names. If I want to access the ids of the generated items directly, I can try and figure out what it mangles the names to and look for that ID.
Are the names of the items generated guaranteed to be done in a specific way by whatever standard there is from Microsoft? I'm just afraid of this breaking if they release a new version of .NET that does it in a different way. Is there a way to generate the name mangling myself in code?
发布评论
评论(4)
不能保证一致(这是一个实现细节),
请改用 ClientID(它为您提供生成的 id)。
It isn't guranteed to be consistent (it's an implementation detail)
Use ClientID instead (which gives you the generated id).
我相信使用 .NET 4 上的 ASP.NET,您可以指定以某种方式生成 ID。
请参阅: http: //weblogs.asp.net/asptest/archive/2009/01/06/asp-net-4-0-clientid-overview.aspx
此外,如果您使用 jQuery,则可以确定标称ID 将位于客户端 ID 中,因此您可以通过以下方式选择它:
请参阅:http://api.jquery.com/attribute-contains-selector/
I believe with ASP.NET on .NET 4, you can specify that the IDs be generated a certain way.
See: http://weblogs.asp.net/asptest/archive/2009/01/06/asp-net-4-0-clientid-overview.aspx
Additionally, if you are using jQuery, you can be sure that the nominal ID will be in the client ID, so you could select it with something like:
See: http://api.jquery.com/attribute-contains-selector/
如果您想在客户端脚本中使用它,您可以执行以下操作:
当它呈现给客户端时,它看起来像这样:
If you want to use it in a client-side script, you can do the following:
When it renders out to the client it'll look like something like this:
不,不能保证它是一致的。
只要使用相同版本的框架,它就应该是一致的,但是服务器的更新可能会改变这一点,因此您不应该依赖于知道生成的名称。
使用
ClientID
属性可以找出控件的生成 ID。No, it's not guaranteed to be consistent.
It should be consistent as long as the same version of the framework is used, but updates to the server may change this, so you shouldn't rely on knowing the generated name.
Use the
ClientID
property to find out the generated id of a control.