Assign JavaScript variable value to ASP.Net Session variable using JQuery AJAX

Last Reply on Oct 30, 2012 04:08 AM By Mudassar

Posted on Oct 29, 2012 07:44 AM

How to assign value to Session variable using jQuery AJAX and ASP.Net

You are viewing reply posted by: Mudassar on Oct 30, 2012 04:08 AM.
Posted on Oct 30, 2012 04:08 AM Modified on on Oct 30, 2012 04:08 AM

This way

Client Side

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
     <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    <script type = "text/javascript">
        function SetSession() {
            var value = '237';
            $.ajax({
                type: "POST",
                url: "CS.aspx/SetSession",
                data: '{value: "' + value + '" }',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: OnSuccess,
                failure: function (response) {
                    alert(response.d);
                }
            });
        }
        function OnSuccess(response) {
            alert(response.d);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <input type = "button" value = "Set Session" onclick = "SetSession()" />
    </form>
</body>
</html>



Code

 [WebMethod]
    public static void SetSession(string value)
    {
        HttpContext.Current.Session["Value"] = value;
    }