30

I am working on asp.net project and each time i need to use jquery identifier $(#"objectID"). I have to change the ClientIDMode on each object to be static. Since I have noticed that the default client ID mode is Inherit so i set the MainContent Client ID mode to be static and i have found that all the object became static.

This will sure save a lot of time when working with jquery, but i just want to know is there any drawback from this and is there any reason why shouldn't ClientIDMode set to be static at the first place ?

Sarawut Positwinyu
  • 4,974
  • 15
  • 54
  • 80
  • So have you found any drawbacks? – Brian Ogden Mar 25 '13 at 22:46
  • We actually set it generally in web.config for all objects in some parts of the code, and choose good id's for objects - then in other projects where you have more complex dependencies and expect more recurring changes of the structure we set it on an object level (in the aspx page itself). – Simply G. Jun 01 '16 at 06:48

2 Answers2

21

You want to be careful about setting the ClientIDMode to Static for things like user controls, or you could end up with multiple elements with the same ID.

For data-bound controls like GridView, you'll also want to use the ClientIDRowSuffix property in order to ensure each row is differentiated.

This post has some good examples.

Graham Clark
  • 12,886
  • 8
  • 50
  • 82
6

Another way to deal with the IDs in JavaScript would be to do something like this:

var something = '<%= btnId.ClientID %>';

Example: If you have a button control like this:

<asp:Button ID="btnId" runat="server"></asp:Button>    

and the ID is translated to id="ct100_ContentPlaceHolder1_btnId" then you could use the variable something to access the control.

honk
  • 9,137
  • 11
  • 75
  • 83