如何从asp.net中的代码访问JavaScript变量

Kri*_*ish 4 javascript asp.net variables code-behind

我正在使用JavaScript,它具有以下代码:

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

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

因此,如果点击该值加100,我使用警报检查,现在如何访问count我的代码隐藏中的var

Mer*_*ovi 15

您需要将count变量存储在服务器端控件上才能执行此操作.

例:

<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" />
Run Code Online (Sandbox Code Playgroud)

然后,在你的代码隐藏中使用:

int value;
if (Int32.TryParse(example.Value, out value))
{
     // Do something with your value here
}
Run Code Online (Sandbox Code Playgroud)