使用JavaScript指定会话值 [英] Assign session value using javascript

查看:117
本文介绍了使用JavaScript指定会话值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用JavaScript来分配会话值

我可以从会话,但分配retrive值不工作

  VAR TempSession ='<%= Convert.ToInt32(会话[状态])%GT;';
如果(TempSession == 6)
{
   警报(TempSession);
   TempSession = 1;
}


解决方案

尝试使用Ajax调用设置会话值: -

JS: -

 <脚本SRC =htt​​p://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js> < / SCRIPT>
<脚本类型=文/ JavaScript的>
    VAR TempSession ='<%= Convert.ToInt32(会话[状态])%GT;';
    如果(TempSession == 6){
        警报(TempSession);
        $阿贾克斯({
            输入:POST,
            网址:'WebForm1.aspx的/的SetValue,
            数据:{VAL:1},
            的contentType:应用/ JSON的;字符集= UTF-8,
            数据类型:JSON,
            成功:函数(MSG){
                警报(新建会话值+ msg.d);
            }
        });
    }< / SCRIPT>

在隐藏文件中的code: -

  [的WebMethod]
公共静态字符串的SetValue(字符串VAL)
{
    HttpContext.Current.Session [状态] = VAL;
    返回HttpContext.Current.Session [状态]的ToString()。
}

Is there any way to assign session value using javascript

i can retrive the value from session but assigning is not working

var TempSession = '<%= Convert.ToInt32(Session["Status"]) %>';
if(TempSession ==6)
{
   alert(TempSession );
   TempSession =1;
}

解决方案

Try using ajax call for setting the session value:-

JS:-

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"> </script>
<script type="text/javascript">
    var TempSession = '<%= Convert.ToInt32(Session["Status"]) %>';
    if (TempSession == 6) {
        alert(TempSession);
        $.ajax({
            type: 'POST',
            url: 'WebForm1.aspx/SetValue',
            data: '{ val:1 }',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function (msg) {
                alert("New Session value is " + msg.d);
            }
        });
    }

</script>

In your code behind file:-

[WebMethod]
public static string SetValue(string val)
{
    HttpContext.Current.Session["Status"] = val;
    return HttpContext.Current.Session["Status"].ToString();
}

这篇关于使用JavaScript指定会话值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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