views:

1766

answers:

7

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 common solution which I could easily implement?

EDIT: maybe my question wasn't clear: I am specifically looking for a way which integrates gracefully in the ASP.NET Server Controls methodology.

A: 

On the Page_Unload event (or call this function in your body tag, <body onunload="onPageUnload();">), you can have a check if the form has data in it. Then you could run some Java:

<script language="text/javascript">
   function onPageUnload() {
        var oConfirm = window.confirm("You have unsaved form data, are you sure you want to close this window?");
        if(oConfirm){
            //Close
        }else{
            //Dont 
        }
    }
</script>

See how that works out for ya

EDIT: It may not prevent the closing of the window, but it will alert your user that they have unsaved data in your form.

Confirmed does NOT work cross-browser, I will investigate a different method and post it up here.

Anders
I don't think this will run if you close the browser. It also doesn't work cross-browser.
Ben Scheirman
+3  A: 

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

Eoin Campbell
A: 

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.

Kibbee
+1  A: 

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.

David Kemp
+2  A: 

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.

bechbd
Thank you! This implementation looks very promising.
splattne
+1  A: 

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]         |
---------------------------------
aemkei
+1  A: 

There's a great jQuery answer here:

http://stackoverflow.com/questions/140460

MGOwen