Click here to Skip to main content
15,905,612 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to prompt user before closing the browser window in asp.net where application is using Master pages or instead the user's session should be killed.

It should prompt some thing like " Are you sure you want to leave from the current application? " where if the user click's on "cancel" then it should be returned to the application as it was before and if the user clicks on "ok" then user session should be killed and respective changes should be brought to the database.

Note : In some scenarios the application prompts the same message when ever the page gets UnLoad i.e Window.unload.
Posted

1 solution

You add an onbeforeunload event to your body element: onbeforeunload="beforeUnloadForm();"

This would become like:
<body onload="initForm();" onbeforeunload="beforeUnloadForm();">


and in the javascript function you put something like this:

function beforeUnloadForm()
{
   if (window.hasChangesPending)
   {
      window.event.returnValue = "Pending changes have not been saved.";
   }
}


When trying to leave the page or close the browser the user now must confirm.

Good luck!

Ps. I do not know if you get a chance to execute code after the user confirmed. But you could already send it to the server and handle it after a certain time out or discard if the user hits cancel.
 
Share this answer
 
v2

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