How to access a JavaScript variable from code behind in asp.net
Asked Answered
P

4

6

i am using a JavaScript and it have the below code:

<script type="text/javascript">
var count = 0;

jQuery('td').click(function () {
    if ($(this).hasClass('process')) {
       count = count+100;
       alert('count');
}
});
</script>

so if its click the value is added by 100 i check using an alert, now how to access the var count in my code-behind

Piano answered 1/3, 2012 at 12:30 Comment(5)
You're missing all }s, but apart from that - do you want to send count to the server?Lagasse
you could $.post() it to the server, which will receive it as it would a normal form input.Forcefeed
yes i want to send to code behindPiano
@krish is it possible for you to tell me why my answer was de-selected as accepted? I understand it was complete enough and explained correctly what you had to do in order to solve your problem. I ask because it's my best interest to provide good answers.Lossa
@Meryovi: i m Sorry for this mistake... actually i refer your answer for second time for another project and unknowingly selected other ans as accepted... again thanks for your ans help me in this proj. also.Piano
L
17

You will need to store the count variable on a server-side control in order to do this.

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-behind simply use:

int value;
if (Int32.TryParse(example.Value, out value))
{
     // Do something with your value here
}
Lossa answered 1/3, 2012 at 12:34 Comment(0)
N
3

Try this:

Add a HiddenField and pass value of count to HiddenField from Jquery

$(function() {
            var count = 100;
            $("#Button1").click(function() {
                $("#HiddenField1").val(count);                
            });
        });
Nifty answered 1/3, 2012 at 12:42 Comment(0)
B
1

JavaScript is a client side technology. The code behind runs on the server. You cannot directly access the javascript variables in your code behind. You'd need to send the information to the server, for example by sending them back as form fields or query string parameters via an ajax request.

jQuery is a great library to simplify the task of sending ajax requests, but many others exist, too.

Bye answered 1/3, 2012 at 12:34 Comment(0)
B
1

Your count variable is not visible to the server, so you have to let it 'readable' somehow... The appropriate solution depends on how you have to deal with it in the code-behind, an option could be to assign the count value to an hidden input field, which is then submitted to the server...

Barcelona answered 1/3, 2012 at 12:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.