How to access JavaScript variable from code in asp.net
I use JavaScript and has the following code:
<script type="text/javascript"> var count = 0; jQuery('td').click(function () { if ($(this).hasClass('process')) { count = count+100; alert('count'); } }); </script> therefore, if his click adds the value 100 i, check with a warning, now how to access var count in my code.
To do this, you will need to save the count variable on the server control.
Example:
<script type="text/javascript"> var count = 0; jQuery('td').click(function () { if ($(this).hasClass('process')) { count = count + 100; alert(count); // Store the value in the control $('#<%= example.ClientID %>').val(count); } }); </script> <asp:HiddenField ID="example" runat="server" /> Then in your code just use:
int value; if (Int32.TryParse(example.Value, out value)) { // Do something with your value here } JavaScript is a client-side technology. The code runs on the server. You cannot directly access javascript variables in your code. You will need to send information to the server, for example, sending them back in the form of form fields or query string parameters using an ajax request.
jQuery is a great library that simplifies the task of sending ajax requests, but many others also exist.