0

I am using the following article to keep my session alive in my asp.net application.

How to Keep Session Alive

But when I checked the session variable value using alert in javascript I didn't find anything. alert('<%=Session["Heartbeat"]%>');

Actually In my webpage there is no postback. All the work I am doing from Jquery Ajax. When there is any postback only then the solution of that article works. But I want to keep my session alive without any postback.

Please help guys.....

Community
  • 1
  • 1
Shaiwal Tripathi
  • 601
  • 1
  • 12
  • 32
  • Please can anyone help me ? – Shaiwal Tripathi Apr 27 '16 at 04:02
  • you dont need post back. just create a webservice and call that web service from your front end using jquery. make sure that you allow your webservice to access session by putting [WebMethod(EnableSession=true)] on your function and then proceed to do what is said on your link – Sujit.Warrier Apr 27 '16 at 04:06
  • Hi @Mysterio11 I tried your idea but it's still not showing anything when I am checking it through alert in javascript. – Shaiwal Tripathi Apr 27 '16 at 04:23
  • Is this ASP.Net Web Form or ASP.Net MVC? – Win Apr 27 '16 at 04:24

2 Answers2

1

alert('<%=Session["Heartbeat"]%>'); will not work without fully postback, because <%=Session["Heartbeat"]%> is a server-side code.

In order for Session to keep alive, all you need is just a simple Ajax call to server.

Easiest way to implement ASHX generic handler, and call it via Ajax. For example,

Ping.ashx

<%@ WebHandler Language="C#" CodeBehind="Ping.ashx.cs" Class="PROJECT_NAMESPACE.Ping" %>

Ping.ashx.cs

public class Ping : IHttpHandler
{ 
   public void ProcessRequest(HttpContext context)
   {
      context.Response.ContentType = "text/plain";
      context.Response.Write("Ping");
   }

   public bool IsReusable { get { return false; }}
}
Win
  • 61,100
  • 13
  • 102
  • 181
0

Please try this example :-

<script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script language="javascript" type="text/javascript">

    function KeepSession() {
    // A request to server
    $.post("http://servername:port/appName/SessionCheck.aspx");

    //now schedule this process to happen in some time interval, in this example its 1 min
    setInterval(KeepSession, 60000);
}

    // First time call of function
    KeepSession(); 
 </script>
F11
  • 3,703
  • 12
  • 49
  • 83