如何从 asp.net 中的代码访问 JavaScript 变量 [英] How to access a JavaScript variable from code behind in asp.net

查看:26
本文介绍了如何从 asp.net 中的代码访问 JavaScript 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 JavaScript,它有以下代码:

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>

所以如果它的点击值增加了 100 我使用警报检查,现在如何访问我的代码隐藏中的 var count

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

推荐答案

为了做到这一点,您需要将计数变量存储在服务器端控件上.

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

示例:

<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
}

这篇关于如何从 asp.net 中的代码访问 JavaScript 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆