Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.8k views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share
...