Mehmet Ince Mehmet Ince - 1 year ago 142
Javascript Question

How to access Session variables and set them in javascript?

In code-behind I set

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
with a new value?

Answer Source

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"] %>');

Accessing ASP.NET Session variable using Javascript:

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

        var username = '<%= Session["UserName"] %>';
        alert(username );
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download