-1

I have an input textbox and I need to access it from both JavaScript and server side C#. If I insert runat="server", the control will be accessible only from the server.

<input id="txtSearch" /> // Accessible only from JavaScript
<input id="txtSearch" runat="server"/> // Accessible only from C#
Ado
  • 65
  • 9

1 Answers1

0

While using runat="server", .net web form will do some modifications to your controls, in this case your input's ID will no long is txtSearch anymore, that would be something like "form_1abcasd_txtSearch". You could fix the issue by ClientIdMode="Static" to get rid of that.

If you are using jQuery, you could use $("input[id$='txtSearch']") to access the element as well.

Zay Lau
  • 1,856
  • 1
  • 10
  • 17
  • The solution about jQuery worked! Which one would be best ClientIdMode="Static" and $("input[id$='txtSearch']")? – Ado Aug 31 '16 at 12:39
  • I recommend `ClientIdMode` unless you can ensure you won't have duplicated ID tails, like `txtSearch` and `atxtSearch` – Zay Lau Aug 31 '16 at 12:43
  • @Ado you may also want to read more: http://blog.zay-dev.com/net-web-form-implementation-strategy-1-the-over-postback/ – Zay Lau Sep 01 '16 at 01:33