Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

asp.net - Assign session value using javascript

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...