The problem is
TextBox2.ID
returns the
sever-side ID of the control, not the one rendered to the browser. If you open you page in Chrome , right click the button and select "Inspect Element" or view the source code, you'll see what I mean.
You could do this:
<asp:Button ID="Button1" OnClientClick="getControlID('<%=TextBox1.ClientID %>');" runat="server" Text="Click Here" />
<asp:TextBox ID="TextBox2" runat="server" />
<asp:Button ID="Button2" OnClientClick="getControlID('<%=TextBox2.ClientID %>');" runat="server" Text="Click Here" />
To get the client ID.
Personally, I think the following is simpler ignoring any errors I have in the code:
<div>
<asp:TextBox ID="TextBox1" runat="server" />
<asp:Button ID="Button1" OnClientClick="getControlID(this);" runat="server" Text="Click Here" />
<asp:TextBox ID="TextBox2" runat="server" />
<asp:Button ID="Button2" OnClientClick="getControlID(this);" runat="server" Text="Click Here" />
</div>
<script type="text/javascript">
function getControlID(control) {
control.value = "It's working!";
return false;
}
</script>
Assuming I've got it right (I haven't tested the code) the button itself will be passed, removing the need for the GetById.
On o further point, you sould take a look at jquery: it really simplifies client work.