Mehmet Ince Mehmet Ince - 1 month ago 5
Javascript Question

How to access Session variables and set them in javascript?

In code-behind I set

Session
with some data.

Session["usedData"] = "sample data";


And the question is how can I get the Session value(in my example; "sample data") in javascript and set
Session["usedData"]
with a new value?

Answer

Accessing & Assigning the Session Variable using Javascript:

Assigning the ASP.NET Session Variable using Javascript:

 <script type="text/javascript">
function SetUserName()
{
    var userName = "Shekhar Shete";
    '<%Session["UserName"] = "' + userName + '"; %>';
     alert('<%=Session["UserName"] %>');
}
</script>

Accessing ASP.NET Session variable using Javascript:

<script type="text/javascript">
    function GetUserName()
    {

        var username = '<%= Session["UserName"] %>';
        alert(username );
    }
</script>