Ajax/Onbeforeunload problem

Hi all,
Most of you experts know about this problem.Please help get the correct solution.
I have List view, Ajax calendar extendars, Autocomplete extendars and Other asp server controls on a page.
i'm asking the user, if he really have to navigate away from the page , if he closes or navigates to other page, without saving data.
but On every postback(like List view Edit,Update,Insert activity), i get this message.
i have read about setting a variable on postback and checking it before unload of window.
I'm a novice and please help.

After onbeforeunload, can i call another page , which does some database calls???
LVL 1
SaraDobAsked:
Who is Participating?
 
oobaylyCommented:
Interesting, I tested it with a DropDownList and it worked in IE & Firefox. Just added  a GridView with a Delete column, and that only works in Firefox.

What's interesting is that both the DropDownList & the Delete button call __doPostBack(). The difference being that the Delete button is calling __doPostBack via the href attribute.

It looks like IE displays the Dialog before executing the javascript method (__doPostBack), which in my personal opinion is just plain stupid.

The general consensus appears to be "don't use javascript urls":
http://www.gamedev.net/community/forums/viewreply.asp?ID=2845355

This of course isn't an option for you as you're using .Net to generate the markup (which ironically doesn't work in Microsoft's own browser <sigh>).

A nasty way around this is to have a method that runs when the page loads. Then attach an onclick event to every Delete/Edit button in the ListView that sets the showUnloadMessage flag to false.
function body_OnLoad() {
  var links = document.getElementById('GridView1').getElementsByTagName('a');
  for (var i = 0; i < links.length; i++) {
    links[i].onclick = function() {
      showUnloadMessage = false;
    };
  }
}

Open in new window

0
 
oobaylyCommented:
Add variable to the page that is to be returned by the OnBeforeUnload event, this defaults to true so that the Confirm dialog is displayed to the user.
In addition, hook the form's onSubmit event and set this value to false so that the confirm dialog isn't displayed.

This means that when the form is submitted by any user input or a postback the dialog isn't shown.

Seems to work in Firefox & Internet Exploder 6
    <script type='text/javascript'>
      var showUnloadMessage = true;
      
      function form_OnSubmit(){
        showUnloadMessage = false;
      }
    
      function window_OnBeforeUnload(){
        if (showUnloadMessage) return showUnloadMessage;
      }
      window.onbeforeunload = window_OnBeforeUnload;
    </script>
</head>
<body >
    <form id="form1" runat="server" onsubmit='form_OnSubmit();'>

Open in new window

0
 
SaraDobAuthor Commented:
Thanks for the reply..
I will try it on monday(office) and get back to you.
Thanks again for the reply
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
SaraDobAuthor Commented:
oobayly:
tested it...It works well only on submit button.
BUT i have a listview on the page.When i click on Edit /insert/Update/Delete /cancel button, it gives this messages beacuse the listview postbacks.
how can this be handled??
Thanks
0
 
SaraDobAuthor Commented:
oobayly:
Ummm intresting analysing...
 I'm still a newbie and learning.So please pardon me, if i comment prematuredly.
In Listview, the Edit/cancel etc button ate Link button. So i put OnclientClick=javascript:form_submit function".
It works, but i have to click it twice..I dont know y? I'm at present testing on iE. i ahve too see if the samething happens in firefox also.
 
thanks for looking deeper into this.I will try the workaround you have posted , and will get back to you.
 
0
 
oobaylyCommented:
The reason it works if you click twice is that the first time you click the link it shows the Dialog. If you then click Cancel, the showUnloadMessage gets set to false. Then the second time you click the link, the form is submitted with showing the dialog because showUnloadMessage is already false.

The workaround I posted worked for me in both IE & Firefox.
0
 
SaraDobAuthor Commented:
Thanks for guiding me , perfectly and patiently to the solution
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.