DropDownList not firing onbeforeunload when AutoPostBack=“True”

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

Client/JS Framework for "Unsaved Data" Protection?


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****/


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


    if (on) {
        window.onbeforeunload = unloadMessage

    window.onbeforeunload = null

function unloadMessage() {

    return 'You have unsaved changes.';

//Moves javascript from href to onclick to prevent IE raising onbeforeunload unecessarily
function fixIEonBeforeUnload() {

    if (!$.browser.msie)
    $('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');
        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()) {

    $('.no-warn').click(function() {

//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.';


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);



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

//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")

    var prm = Sys.WebForms.PageRequestManager.getInstance();

//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())

//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;


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.