Skip to content Skip to sidebar Skip to footer

ASP.NET: Warning On Changed Data Closing Windows

I'd like to warn users when they try to close a browser window if they didn't save the changes they made in the web form. I'm using ASP.NET 3.5 (with ASP.NET Ajax). Is there a comm

Solution 1:

Here is an ASP.NET extender control that will help you with this:

http://www.codeproject.com/KB/ajax/ajaxdirtypanelextender.aspx

Hope this helps. It may not perfectly fit your needs but it at least shows you how.


Solution 2:

you'll want to leverage off the

window.onbeforeunload

event. Similar to Gmail if you attempt to close the window when you haven't saved a composed email.

Theres some sample JS here.

http://forums.devarticles.com/javascript-development-22/how-to-stop-browser-from-closing-using-javascript-8458.html


Solution 3:

Simply add the window.onbeforeunload event. This shows up a modal window where you can decide to stay or leave the page.

window.onbeforeunload = function (event) {
  var message = 'All changes will get lost!';
  if (typeof event == 'undefined') {
    event = window.event;
  }
  if (event) {
    event.returnValue = message;
  }
  return message;
}

All browsers will provide a standard message ("Are you sure you want to navigate away from this page?") plus your custom message.

So the window will look like this:

---------------------------------
| Are you sure you want to      |
| navigate away from this page? |
|                               |
| All changes will get lost!    |
|                               |
|     [Cancel]     [Ok]         |
---------------------------------

Solution 4:

You could create a control that extends IExtenderProvider and have that create the JavaScript to track the changes (or more likely register the controls with a change tracking mechanism).

You're still going to have to implement the change tracking in Javascript though, unless you want a lot of Postbacks.

I did this at a previous employer using Prototype.Js. I think the only difficult thing was the difference between the way that Internet Explorer and Firefox require the "cancel navigation" flag to be set.


Solution 5:

as others have mentioned, you'll want to look at the window.onunload/onbeforeunload events. The problem with this event is that it is called whenever the page is unloaded. Not only when the browser window is closed, but also when you click on a link within the same browser window to go to a different page on the same site. You still may be able to do what you want to do, but it helps if you can anticipate this behaviour.


Post a Comment for "ASP.NET: Warning On Changed Data Closing Windows"