Shaiwal Tripathi
Shaiwal Tripathi

Reputation: 627

Session does not stay alive

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.....

Upvotes: 0

Views: 470

Answers (2)

F11
F11

Reputation: 3816

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>

Upvotes: 0

Win
Win

Reputation: 62301

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; }}
}

Upvotes: 1

Related Questions