Problem: I am looking to create a time-out warning message on an asp.net page with a c# code behind based off my webconfig sessionState TimeOut Attribute.
Code on w
I've done this before by creating a web method in my code-behind file that checks for the timeout. Have your Javascript function get the timeout information via AJAX and display a warning according.
Example
This is the web method in my code-behind:
[WebMethod]
public static bool HasSessionTimedOut()
{
HttpSessionState session = HttpContext.Current.Session;
// I put this value into Session at the beginning.
DateTime? sessionStart = session[SessionKeys.SessionStart] as DateTime?;
bool isTimeout = false;
if (!sessionStart.HasValue)
{
isTimeout = true;
}
else
{
TimeSpan elapsed = DateTime.Now - sessionStart.Value;
isTimeout = elapsed.TotalMinutes > session.Timeout;
}
return isTimeout;
}
And this is my Javascript:
So, this is pretty rudimentary. Every 30 seconds, my Javascript function sends an AJAX request to my web method using ASP.NET's PageMethods object. It checks for a return value of true in the callback, which indicates that a timeout has occurred, and takes the appropriate action.