Posted by : Jebastin Wednesday, 29 January 2014

Usage of Session in ASP.NET using C# or jQuery. A session is a piece of data which is sent from a website and stored locally by the user’s browser. Sessions are needed because HTTP is stateless. This means that HTTP itself has no way to keep track of a user’s previous activities. One way to create state is by using session.

C# Web Service:

  1. [WebMethod(EnableSession = true)]
  2. public List<HistoricalProperty> FetchHistoricalData()
  3. {
  4.     List<HistoricalProperty> historyList = new List<HistoricalProperty>();
  5.     if (Context.Session["historicalData"] != null)
  6.     {
  7.         historyList = (List<HistoricalProperty>)Context.Session["historicalPrice"];
  8.         ...
  9.         ...
  10.         ...
  11.     }
  12.     else
  13.     {
  14.         ...
  15.         ...
  16.         ...
  17.         Context.Session["historicalData"] = historyList;
  18.     }
  19.     return historyList;
  20. }

jQuery:

 TO STORE:

  1. $(function() {
  2.  $.session.set("myVar", "Hello World!");
  3. });

TO READ:

  1. $(function() {
  2.  alert($.session.get("myVar"));
  3. });

CODE VIEW:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>jQuery Session</title>
  5. <script type='text/javascript' src='jquery-1.9.1.js'></script>
  6. <script type="text/javascript" src="jquery.session.js"></script>
  7. </head>
  8. <body>
  9. <script type='text/javascript'>
  10. $(window).load(function() {
  11.  // To Store
  12.  $(function() {
  13.  $.session.set("myVar", "Hello World!");
  14.  });
  15. // To Read
  16.  $(function() {
  17.  alert($.session.get("myVar"));
  18.  });
  19.  
  20. });
  21. </script>
  22. </body>
  23. </html>

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Link To This Post/Page

Spread The Word

Add this button to your blog:
JJ Technology Solutions

Blog Archive

Trackers

eXTReMe Tracker
facebook

- Copyright © JJ Technology Solutions - Powered by Source Code Solutions -