chugh97
chugh97

Reputation: 9984

ASP.NET session in Javascript

I have the following code invoked from a button click - js event

function onOkReason() {
     alert("called");
     var session = $('<%=Session["A"]%>');
     if (session == null) {
         <%Session["A"]%> = "1";
         alert("1");
     }
     else {
         <%Session["A"]%> = null;
         alert("2");
     }

     alert(session);

 }

It does not seem to work...Any pointers as to what may be wrong...

Upvotes: 1

Views: 832

Answers (1)

Kieron
Kieron

Reputation: 27107

The <% %> is evaluated when the page is processed on the server, you can't then set it from the client - it's to late, the page has been processed and sent back to the clients browser.

You'll need to post back to the server (either full or an AJAX call) to set the data.

Upvotes: 4

Related Questions