As pointed out, "often" the asp.net web page processor will change, or re-name controls in question, and this is especially the case when say using a repeater, gridview, listview etc.
And in fact, in many cases, when you use a master + child page setup, then again the asp.net web page processor will often change the name of the control.
Now, often, when writing JavaScript code, and say using jQuery to save some keyboard wearing out?
I often actually set the control in question to "disable" this automatic re-naming of controls. And keep in mind, in "most" cases, this re-name of controls is to prevent name collisions from occurring on a page. After all if you have a simple label with a id="HotelName", then what occurs if you have 10 rows in that GridView or repeater?
Answer:
The control is given a prefix with the gridview, then the control name, then the row number!!!
So, a hard and fast rule?
Well, if the control in question is NOT nested in some kind of data repeater, then there will ONLY be one of that control on the page, and in as above notes, EVEN in that case, you can wind up with a control re-name.
so, I'll often say do this:
<asp:TextBox ID="TextBox1" runat="server"
ClientIDMode="Static" >
</asp:TextBox>
<br />
<asp:Button ID="cmdTest" runat="server" Text="Set text box = hello"
OnClientClick="mytest();return false;"
/>
<script>
function mytest() {
$('#TextBox1').val("Hello")
}
</script>
So, why in above did I use/set ClientIDMode="static". Well, perhaps I am a bit lazy, perhaps I don't want to think or care if asp.net "might" change the name of the ID when the page is rendered.
As in a "lot of" cases, the "id", and .ClientID will in fact be the same, but often, it will not be.
Of course above is often written this way:
<script>
function mytest() {
$('#<%= TextBox1.ClientID %>').val("Hello")
}
</script>
In above, we use a "server" expression for the jQuery select of the text box. And that means I could remove the ClientIdMode="static".
However, the above will NOT help if we have a repeater, or gridview etc. SINCE that means a repeating control, and for a repeating control, then some kind of numbering system will be applied to that control.
So, for non repeating items? I often use "ID", and client mode = static, and that's due to be being somewhat lazy, and not having to type in that "%" server side expression.
So, for controls on a page, you have a choice here. However, for controls inside of a repeating item - say result of some row button click? No, you don't have a choice, and you have to get/grab the server generated "client id" in such cases.