Reputation: 10267
Both of these checks work:
If cntrl.ID.ToString().Contains("ckbx") Then
If cntrl.ClientID.ToString().Contains("ckbx") Then
Is there any advantage to querying ClientID over ID?
BTW, I assign directly to ID, but not to ClientID (if that's even possible). That code is:
Dim chk = New CheckBox()
chk.ID = "ckbx" + i.ToString()
chk.Checked = True
formCustCatMaint.Controls.Add(chk)
Upvotes: 1
Views: 43
Reputation: 8004
Is there any advantage to querying ClientID over ID?
Short answer is it depends on useage...
ID is used to assign an identifier to a server control which can be later used to access that control. You can use either the field generated in the code-behind or pass the value of the ID property to the FindControl
method. There is a catch though; the ID property is unique only within the current container: page, user control, control with item template etc. If a server control is defined inside the item template of some other control (Repeater, DataGrid) or user control, its ID property is no longer unique.
ClientID is generated following the same rules (the ID of the control prefixed by the ID of its NamingContainer). The only difference is the separator - for the ClientID it is the "_" (underscore) symbol. The ClientID property is globally unique among all controls defined in an ASP.NET page.
Also worth mentioning that the values of the ID or ClientID will be the same if the control is defined in the master page. However it can lead to unexpected errors. If the ID of the control is hard-coded inside the JavaScript
statement the code will only work provided the control is defined in the Page or master page. Moving the control and the JavaScript
code into a user-control with ID "UserControl1" will fail at runtime.
You can read more there or here.
Upvotes: 2