Posted By

jink on 05/08/09


Tagged

session aspnet timeout prevent


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

benrudolph
Barlon


ASP.net: Session timeout prevention (keep alive).


 / Published in: ASP
 

URL: http://www.codeproject.com/KB/session/Reconnect.aspx

Call AddKeepAlive() in Page_Load of the page that needs it. Change the path to the correct keep alive page.

Page should disable caching, like in second code snippet.

  1. private void AddKeepAlive()
  2. {
  3. int int_MilliSecondsTimeOut = 2 * 60 * 1000; /*2 minutes*/ //Math.Max((this.Session.Timeout * 60000) - 30000, 5000);
  4. string path = VirtualPathUtility.ToAbsolute("~/KeepAlive.aspx");
  5.  
  6. string str_Script = @"<script>(function(){var r=0,w=window;if (w.setInterval)w.setInterval(function() {r++;var img=new Image(1,1);img.src='" + path + @"?count='+r;}," + int_MilliSecondsTimeOut.ToString() + @");})();</script>";
  7. Page.ClientScript.RegisterStartupScript(typeof(Page), UniqueID + "Reconnect", str_Script);
  8. }
  9.  
  10. //[KeepAlive.aspx:]
  11. <%@ OutputCache Location="None" VaryByParam="None" %>
  12. <?xml version="1.0" encoding="utf-8"?>
  13. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  14. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  15. <%=now %>
  16. </html>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: the_coder on June 12, 2009

Thanks a lot that works perfectly

ASP.NET 2.0, Ajax enabled.

You need to login to post a comment.