Ado Ado - 1 year ago 86
C# Question

Acess HTML input from both client and server side

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#

Answer Source

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.