Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Ajax/Onbeforeunload problem

Posted on 2009-04-24
Medium Priority
Last Modified: 2012-05-06
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???
Question by:SaraDob
  • 4
  • 3
LVL 15

Expert Comment

ID: 24231668
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;
<body >
    <form id="form1" runat="server" onsubmit='form_OnSubmit();'>

Open in new window


Author Comment

ID: 24232505
Thanks for the reply..
I will try it on monday(office) and get back to you.
Thanks again for the reply

Author Comment

ID: 24242734
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??
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 15

Accepted Solution

oobayly earned 1000 total points
ID: 24243360
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":

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


Author Comment

ID: 24243868
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.
LVL 15

Expert Comment

ID: 24244051
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.

Author Closing Comment

ID: 31574338
Thanks for guiding me , perfectly and patiently to the solution

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question