Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 954289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:11:31+00:00 2026-05-16T00:11:31+00:00

I have implemented an "unsaved changes" warning using techniques described on these pages: Client/JS

  • 0

I have implemented an "unsaved changes" warning using techniques described on these pages:

Client/JS Framework for "Unsaved Data" Protection?

http://kenbrowning.blogspot.com/2009/01/using-jquery-to-standardize.html

This works well except for a DropDownList on the page. It does an AutoPostBack, and I want onbeforeunload to fire because unsaved changes will be lost, but it isn’t working. Should it be raising the onbeforeunload event? Can I somehow make it raise the event?

Edit:
The DropDownList is inside an UpdatePanel, so that means it isn’t unloading the page and that would be why onbeforeunload isn’t being triggered. Is there any way I can trigger the event programmatically? Or do I have to roll my own imitation Confirm dialog?

Edit2
I now have a solution that adds the dialog to asynchronous postbacks from an UpdatePanel. I have edited the original script, adding the call to setConfirmAsyncPostBack() as described in my solution.

Here is my JavaScript:

/****Scripts to warn user of unsaved changes****/

//https://stackoverflow.com/questions/140460
//http://jonstjohn.com/node/23

//Activates the confirm message onbeforeunload.
function setConfirmUnload(on) {

    setConfirmAsyncPostBack();

    if (on) {
        removeCheckFromNoWarnClasses();
        fixIEonBeforeUnload();
        window.onbeforeunload = unloadMessage
        return;
    }

    window.onbeforeunload = null
}

function unloadMessage() {

    return 'You have unsaved changes.';
}

//Moves javascript from href to onclick to prevent IE raising onbeforeunload unecessarily
//http://kenbrowning.blogspot.com/2009/01/using-jquery-to-standardize.html
function fixIEonBeforeUnload() {
 
    if (!$.browser.msie)
        return;
    $('a').filter(function() {
        return (/^javascript\:/i).test($(this).attr('href'));
    }).each(function() {
        var hrefscript = $(this).attr('href');
        hrefscript = hrefscript.substr(11);
        $(this).data('hrefscript', hrefscript);
    }).click(function() {
        var hrefscript = $(this).data('hrefscript');
        eval(hrefscript);
        return false;
    }).attr('href', '#');
}

//Removes warnings from Save buttons, links, etc, that have been can be given "no-warn" or "no-warn-validate" css class
//"no-warn-validate" inputs/links will only remove warning after successful validation
//use the no-warn-validate class on buttons/links that cause validation. 
//use the no-warn class on controls that have CausesValidation=false (e.g. a "Save as Draft" button).
function removeCheckFromNoWarnClasses() {
  
    $('.no-warn-validate').click(function() {
        if (Page_ClientValidate == null || Page_ClientValidate()) {
            setConfirmUnload(false);
        }
    });

    $('.no-warn').click(function() {
        setConfirmUnload(false);
    });
}

//Adds client side events to all input controls to switch on confirmation onbeforeunload
function enableUnsavedChangesWarning() {
 
    $(':input').one('change', function() {
        window.onbeforeunload = function() {
            return 'You have unsaved changes.';
        }
    });

    removeCheckFromNoWarnClasses();
}

And in my ASP.NET page, when the user makes a change:

    if (changed)
    {
        ...
        //Confirm unload if there are unsaved changes. 
        //NB we also have to call fixIEonBeforeUnload() to fix links, done in in page load to include links that are rendered during callbacks
        ScriptManager.RegisterStartupScript(Page, GetType(), "unsavedchanges", "setConfirmUnload(true);", true);
    }
    else
        ...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:11:32+00:00Added an answer on May 16, 2026 at 12:11 am

    Also see How to prevent AutoPostBack when DropDownlist is selected using jQuery

    //http://msdn.microsoft.com/en-us/magazine/cc163413.aspx
    //https://stackoverflow.com/questions/2424327/prevent-asp-net-dopostback-from-jquery-submit-within-updatepanel
    //Adds an event handler to confirm unsaved changes when an asynchronous postback is initialised by an UpdatePanel
    function setConfirmAsyncPostBack() {
    
        if (typeof (Sys.WebForms) === "undefined" || typeof (Sys.WebForms.PageRequestManager) === "undefined")
            return;
    
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm.add_initializeRequest(confirmAsyncPostBack);
    }
    
    //An event handler for asynchronous postbacks that confirms unsaved changes, cancelling the postback if they are not confirmed
    //Adds the confirmation to elements that have a css class of "warn"
    function confirmAsyncPostBack(sender, args) {
        if (window.onbeforeunload != null && args.get_postBackElement().className == "warn" && !unloadConfirmed())
            args.set_cancel(true);
    }
    
    //Displays a confirmation dialog that imitates the dialog displayed by onbeforeunload
    function unloadConfirmed() {
    
        var confirmed = confirm("Are you sure you want to navigate away from this page?\n\n" + unloadMessage() + "\n\nPress OK to continue or Cancel to stay on the current page.");
        if (confirmed)
            window.onbeforeunload = null;
        return confirmed;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 485k
  • Answers 485k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think its the problem within the interface I am… May 16, 2026 at 7:42 am
  • Editorial Team
    Editorial Team added an answer You've pretty much said it all in your question. With… May 16, 2026 at 7:42 am
  • Editorial Team
    Editorial Team added an answer We have developed a GPS App for iPhone/Android with GWT… May 16, 2026 at 7:42 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.