ID vs UniqueID vs ClientID vs UniqueClientID vs StaticClientID?
好吧,我对动态创建的控件的 ID 很困惑。
Public Class TestClass
Inherits Panel
Implements INamingContainer
Function TestClassInit() Handles Me.Init
Dim pnlMainPanel As New Panel
Me.Controls.Add(pnlMainPanel)
Dim pnlTest1 As New Panel
pnlMainPanel.Controls.Add(pnlTest1)
pnlTest1.ClientIDMode = UI.ClientIDMode.Inherit ' DEFAULT
'pnlTest1.ID = "ctl01"
'pnlTest1.UniqueID = "ctl00$MainPanel$ctl01"
'pnlTest1.ClientID = "MainPanel_ctl01"
'pnlTest1.UniqueClientID = "ctl00_MainPanel_ctl01"
'pnlTest1.StaticClientID = ""
pnlTest1.ClientIDMode = UI.ClientIDMode.Predictable
'pnlTest1.ClientID = "MainPanel_ctl01" (no change)
pnlTest1.ClientIDMode = UI.ClientIDMode.AutoID
'pnlTest1.ClientID = "ctl00_MainPanel_ctl01"
pnlTest1.ClientIDMode = UI.ClientIDMode.Static
'pnlTest1.ClientID = ""
End Function
End Class
为什么有5个不同的ID?
什么时候应该使用不同的 ID 模式?
(我阅读了 MSDN 文档,但它们和往常一样,并不是特别有启发性。)
如果我不关心 ID 是什么,而只想添加一个控件和将其 ID 提供给动态添加的 AJAX 扩展程序,我应该使用哪种模式/ID 组合?
Okay, I'm pretty confused about the IDs of dynamically created controls.
Public Class TestClass
Inherits Panel
Implements INamingContainer
Function TestClassInit() Handles Me.Init
Dim pnlMainPanel As New Panel
Me.Controls.Add(pnlMainPanel)
Dim pnlTest1 As New Panel
pnlMainPanel.Controls.Add(pnlTest1)
pnlTest1.ClientIDMode = UI.ClientIDMode.Inherit ' DEFAULT
'pnlTest1.ID = "ctl01"
'pnlTest1.UniqueID = "ctl00$MainPanel$ctl01"
'pnlTest1.ClientID = "MainPanel_ctl01"
'pnlTest1.UniqueClientID = "ctl00_MainPanel_ctl01"
'pnlTest1.StaticClientID = ""
pnlTest1.ClientIDMode = UI.ClientIDMode.Predictable
'pnlTest1.ClientID = "MainPanel_ctl01" (no change)
pnlTest1.ClientIDMode = UI.ClientIDMode.AutoID
'pnlTest1.ClientID = "ctl00_MainPanel_ctl01"
pnlTest1.ClientIDMode = UI.ClientIDMode.Static
'pnlTest1.ClientID = ""
End Function
End Class
Why the 5 different IDs??
When should you use the different ID modes?
(I read the MSDN docs, but they were, as usual, not particularly illuminating.)
If I don't care what the ID is, and just want to add a control & give its ID to a dynamically added AJAX extender, which mode/ID combo should I use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ASP.Net 4 添加了 clientIdMode,如果将其设置为“静态”,它允许您强制 id 属性与服务器端 id 匹配(从而更可预测)。
ASP.Net 4 adds clientIdMode which allows you to force the id attribute to match the serverside id (and thus be more predictable) if you set it to "static".
使用 ClientID 属性。
ClientIDMode 支持 100% 设置控件使用的实际 ID 的能力……或不支持。你的选择。基本上它有助于编写 javascript 代码。
Use the ClientID property.
ClientIDMode is there to support the ability to 100% set the actual ID used by the control..or not. Your choice. Basically it aids in writing javascript code.
ClientIDMode 已添加到 ASP.NET 4 中,以允许您根据需要进行控制。它对于 jQuery 等客户端库特别有用。 ASP.NET 的魔力取决于控件的独特性。如果您决定使用静态,请确保它们是唯一的,因为您可能会遇到一些意外的运行时错误。
The ClientIDMode was added to ASP.NET 4 to allow control if you want it. It is particularly useful for client side libraries such as jQuery. ASP.NET's magic hinges on the uniqueness of controls. If you decided to use Static make sure they are unique, as you may face some unexpected runtime errors down the road.