ASP.NET: Warning on changed data closing windows

后端 未结 6 1475
深忆病人
深忆病人 2021-01-06 22:26

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 us

相关标签:
6条回答
  • 2021-01-06 22:36

    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

    0 讨论(0)
  • 2021-01-06 22:37

    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.

    0 讨论(0)
  • 2021-01-06 22:44

    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.

    0 讨论(0)
  • 2021-01-06 22:47

    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]         |
    ---------------------------------
    
    0 讨论(0)
  • 2021-01-06 22:48

    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.

    0 讨论(0)
  • 2021-01-06 23:00

    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.

    0 讨论(0)
提交回复
热议问题