-1

I am developing a new asp.net web form application with C#. I have the following problem. If the user is entering some information and before finishing the process choose to leave the current page, how can I alert them that the information will be lost. And if they decide they want to leave the page, delete the information from the database.

Thanks.

  • 2
    What constitutes "leaving the page"? Closing the tab? Closing the browser? Shutting down the computer? – n8wrl Mar 20 '15 at 18:22
  • If the user closes the tab or their browser, there is essentially no way for the server to know this because the client normally wont send a signal to the server to indicate that it has left the page. I think the closest you'll get to something like this is to play around with signalR http://www.asp.net/signalr/overview/guide-to-the-api/handling-connection-lifetime-events – Cody Hicks Mar 20 '15 at 18:23
  • Leaving the page includes everything that you mentioned and navigating to another web page. – oligodendrocito Mar 20 '15 at 18:25
  • possible duplicate of [How can I ask a web user for confirmation if he really wants to leave the page?](http://stackoverflow.com/questions/3998603/how-can-i-ask-a-web-user-for-confirmation-if-he-really-wants-to-leave-the-page) – JamesENL Mar 27 '15 at 05:32

2 Answers2

3

You can use 'beforeunload' event. It will be fired on tab close, window close, navigate away, relaod. BUT won't help in case of system shutdown. You can return a message from this event which will be presented to user (in a system window) with 'OK' and 'Cancel' button.

$(window).bind('beforeunload', function () {
    return " All unsaved changes will be lost. Are you sure you want to leave?";
   }
});
wonderbell
  • 1,126
  • 9
  • 19
0

i ll suggest u one thing... keep all the data in a temporary file in client side if the data has not problem with security..u can use localstorage...and finish all the processes ...if they finish all the process then send the data to server...otherwise before that if they close the pc or whatever data is not going to bother the server....and before closing tab use

$(window).bind('beforeunload', function () {
return " All unsaved changes will be lost. Are you sure you want to leave?";

} });