0

To begin with I'm new to Web forms and JavaScript and I've spent two days Googling with no results. I'm trying to ask the user, before he exits the page whether they want to save the data they've input. I've got a textbox that tells whether any input has been performed which should trigger the javascript if statement. My code is working if the user moves away from the page using a control contained within the page. It is not working when they click the X in the upper right-hand corner or select File/Exit. Below is my code.

     <script type="text/javascript" >
     var needToConfirm = "False";
     var hdnNeedToConfirm = ""
     window.onbeforeunload = confirmExit;
     function confirmExit() {
         __doPostBack('<%= Page.Master.FindControl("MainContent").FindControl("PADForm").ClientID %>', '');
         __doPostBack('<%= Page.Master.FindControl("MainContent").FindControl("HiddenPanel").ClientID %>', '');
         hdnNeedToConfirm = document.getElementById('<%= Page.Master.FindControl("MainContent").FindControl("HdnNeedToConfirm").ClientID %>');
         if (hdnNeedToConfirm != null)
             needToConfirm = hdnNeedToConfirm.value;
         else
             needToConfirm = "False";
         if (needToConfirm == "True")
             window.open("SaveOrLose.aspx");
     }
</script>

I have two __dopostback statements in the code above, however the postbacks are not actually happening until AFTER the rest of the code is. Which means that the if statement fails, so the Save form is never opened.

I'd appreciate any help you could give!!

dastokesh
  • 3
  • 3

1 Answers1

0

You shouldn't call __doPostBack() from your code.

It's a function that should be called only by ASP.NET Web Forms to invoke server-side events / code.

If you inspect the implementation of the __doPostBack() function you will notice that is submits an HTML Form.

Therefore your program will never reach the point where it calls __doPostBack() a second time.

Glenn Ferrie
  • 10,290
  • 3
  • 42
  • 73
  • I put them in in order to catch the X and File/Exit events. I was previously using code-behind to open the Save form. I have text boxes that do not activate their ontextchanged events if the X or File/Exit are used so was trying another route. Thanks!! – dastokesh Jul 24 '14 at 15:22
  • can you (in pseudo code) explain what you want to accomplish when the view is closed. – Glenn Ferrie Jul 24 '14 at 16:21
  • If input has happened in the form – dastokesh Jul 24 '14 at 16:32
  • Sorry, hit the return... If input has happened in the form I want the hidden text field to be changed from "False" to "True" (through code-behind postback)and the Save form to be opened. If input has not happened I want the browser to exit. Right now the final postback is not happening if the user is in a text field and hits the X or File/Exit. – dastokesh Jul 24 '14 at 16:36
  • Check out this article: http://stackoverflow.com/questions/11844256/alert-for-unsaved-changes-in-form – Glenn Ferrie Jul 24 '14 at 16:41
  • Well, I've tried the code (I'm less familiar with jquery than I am with javascript, so...) and now the script is not even being hit. See below. Any help would be very much appreciated!! – dastokesh Jul 24 '14 at 19:52
  • – dastokesh Jul 24 '14 at 19:53