How to set a Session Variable via JavaScript

Jak
Jak
Member
858 Points
132 Posts

Hi,

 How to set a Session Variable via JavaScript?

Views: 9527
Total Answered: 1
Total Marked As Answer: 0
Posted On: 10-Jul-2015 03:13

Share:   fb twitter linkedin
Answers
Rahul Maurya
Rahul M...
Teacher
4822 Points
23 Posts
         

Hi Jak,

I is not possible to set session variable, directly.

But you can do by using web method on server side and then call the web method from javascript.

In Javascript:

<script type="text/javascript">
 PageMethods.SetSessionValue(txt,val); 
</script>

On server side:

[System.Web.Services.WebMethod] 
public static string SetSessionValue(string txt,string val)
Page objp = new Page();
objp.Session[txt] = val;
 
return "true";
}

 

Must enable page method as:

<asp:scriptmanager ID="Scriptmanager1" EnablePageMethods="true" runat="server"></asp:scriptmanager>
 
Posted On: 11-Jul-2015 04:30
 Log In to Chat