ID与UniqueID对比ClientID与UniqueClientID对比StaticClientID?

J.S*_*eve 8 javascript vb.net asp.net ajax dom

好吧,我对动态创建的控件的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
Run Code Online (Sandbox Code Playgroud)

为什么5个不同的ID?

什么时候应该使用不同的ID模式?

(我阅读了MSDN文档,但它们像往常一样,并不是特别有启发性.)

如果我不在乎ID是什么,只想添加一个控件并将其ID提供给动态添加的AJAX扩展器,我应该使用哪种模式/ ID组合?

iZ.*_*iZ. 17

  • ID是您在代码中使用的服务器端ID.
  • UniqueId对应于生成的HTML元素的"name"属性.
  • ClientID对应于生成的html元素的"id"属性.所以它取决于你需要哪个属性(名称与表单发送,id用于DOM操作).
  • 不确定uniqueclientid是什么:)

ASP.Net 4添加了clientIdMode,如果将其设置为"static",则允许您强制id属性与服务器端ID匹配(因此更可预测).