In this article I will explain with an example, how to set Session variable in JavaScript in ASP.Net using C# and VB.Net.
JavaScript is a Client Side language and hence directly it is not possible to set Session variable in JavaScript.
Thus, the solution is to make an AJAX call using XmlHttpRequest (XHR) and pass the value of JavaScript variable to a WebMethod and inside the WebMethod the value will be set in Session variable in ASP.Net using C# and VB.Net.
 
 
HTML Markup
The HTML Markup consists of an ASP.Net TextBox and a HTML Button. The HTML Button has been assigned an onclick event handler which calls SetSession JavaScript method to set value of JavaScript variable in Session.
Your Name :
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<input id="btSet" type="button" value="Set Session" onclick="SetSession()" />
 
 
Sending the value of JavaScript variable to WebMethod
When the Set Button is clicked, the SetSession JavaScript function is called.
Inside the JavaScript function, an AJAX call is made to the SetSession WebMethod using XmlHttpRequest (XHR) and the value of the UserName TextBox is sent to the WebMethod.
The WebMethod then returns a string which is displayed using JavaScript Alert Message Box.
<script type="text/javascript">
    function SetSession() {
        var name = document.getElementById("<%=txtUserName.ClientID%>").value;
        var request;
        if (window.XMLHttpRequest) {
            //New browsers.
            request = new XMLHttpRequest();
        }
        else if (window.ActiveXObject) {
            //Old IE Browsers.
            request = new ActiveXObject("Microsoft.XMLHTTP");
        }
        if (request != null) {
            var url = "Default.aspx/SetSession";
            request.open("POST", url, false);
            var params = "{name: '" + name + "'}";
            request.setRequestHeader("Content-Type", "application/json");
            request.onreadystatechange = function () {
                if (request.readyState == 4 && request.status == 200) {
                    alert(JSON.parse(request.responseText).d);
                }
            };
            request.send(params);
        }
    }
</script>
 
 
Server Side Web Method
Inside the SetSession WebMethod, the value sent from the Client Side is received as parameter and is set into the Session variable.
Finally, the value is read back from the Session variable and sent back to the Client along with Current Server’s Date and Time in string format.
Note: The method is declared as static (C#) and Shared (VB.Net) and also it is declared as WebMethod unless you do this you won’t be able to call the methods.
 
C#
[System.Web.Services.WebMethod]
public static string SetSession(string name)
{
    HttpContext.Current.Session["Name"] = name;
    return "Hello " + HttpContext.Current.Session["Name"] + Environment.NewLine + "The Current Time is: " + DateTime.Now.ToString();
}
 
VB.Net
<System.Web.Services.WebMethod()> _
Public Shared Function SetSession(ByVal name As String) As String
    HttpContext.Current.Session("Name") = name
    Return "Hello " & HttpContext.Current.Session("Name") & Environment.NewLine & "The Current Time is: " & DateTime.Now.ToString()
End Function
 
 
Screenshot
Set Session variable in JavaScript in ASP.Net
 
 
Browser Compatibility

The above code has been tested in the following browsers.

Internet Explorer  FireFox  Chrome  Safari  Opera 

* All browser logos displayed above are property of their respective owners.

 
 
Downloads