Click here to Skip to main content
14,693,117 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Web application: is there a way to save data on a page if session time:

I copy this code:
private void CheckSessionTimeout()

                 string msgSession = "Warning: Within next 3 minutes, if you do not do anything, our system will redirect to the login page. Please save changed data.";
                 //time to remind, 3 minutes before session end
                 int int_MilliSecondsTimeReminder = (this.Session.Timeout * 60000) - 3 * 60000;

                 //time to redirect, 5 miliseconds before session end
                 int int_MilliSecondsTimeOut = (this.Session.Timeout * 60000) - 5;

                 string str_Script = @"
                 var myTimeReminder, myTimeOut, mySaveTime;
                 clearTimeout(myTimeOut);" +
                 "var sessionTimeReminder = " + int_MilliSecondsTimeReminder.ToString() + "; " +
                 "var sessionTimeout = " + int_MilliSecondsTimeOut.ToString() + ";" +
                  "function doReminder(){ alert('" + msgSession + "'); }" +
                  "function doRedirect(){ window.location.href='../MainEntry/MainEntryForm.aspx'; }" + @"
                  myTimeReminder=setTimeout('doReminder()', sessionTimeReminder); 
                  mySaveTime=setTimeout('doRedirect()', sessionSaveTime);
                  myTimeOut=setTimeout('doRedirect()', sessionTimeout); ";


                      ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(),
                      "CheckSessionOut", str_Script, true);

I change web.config session timeout to 5 minutes
so I get the alert. But what i need is before the session times out to save information that user has already type on the form.

If there is an easier way to do this. Please point me in the right directions or give me some assistant with the code.
Updated 29-Dec-10 11:44am

1 solution

Hi Mo Hohn,

on the client side you could use AJAX to make repeated checks about the time span left before the session times out. This in itself is tricky as you don't want the check itself to stop the session from timing out.
Forget what I wrote above it is nonsense. There is no sure way to know when a session is about to expire. You could implement a client side javascript though that roughly keeps the time since the last interaction with the server and starts an "Auto Save" operation after lets say 10 minutes of inactivity. Even this leaves you with the problem that the session may already be gone because of a recycling of the worker process.
Also consider the implications that come with the storage of partial data:
When your data is automatically saved (the reason is actually irrelevant) the code on your server may have problems saving it because it is incomplete which can lead to errors due to foreign key constraints etc. It may seem to be a good idea, but needs to be thoroughly thought out to avoid running into unexpected problems later.
If your application is able to handle storage of incomplete data, I would try the approach using AJAX.

Best Regards,

postonoh 30-Dec-10 10:57am
I have created edit/update pages for partial save data. This allows user if they don't have all necessary information from a client they can save and return at a later date.
Manfred Rudolf Bihy 30-Dec-10 12:38pm
If you have that already implemented, then I would go for the javascript timer based autosave. Set the timer on page load and if no update was made in a specific time interval do your auto save stuff.
Good coding! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900