views:

60

answers:

0

I have a FormView with some basic Cancel/Update/Insert/Edit ImageButtons, and I have a ListView inside the FormView. I need to warn the user whenever he inserts new items in the ListView and doesn't save the changes. So I have a flag (an hidden input) that I fill with "1" whenever I add/remove a new item in the ListView, I have a handler for the onbeforeunload event that checks if the flag value is "1". Then, I have some JS code that allows some controls to "pass through" onbeforunload without showing the alert.

My JS is:

        var FormViewMode = '<% = fvExecution.CurrentMode.ToString() %>';
    var obsEdit = false;
    window.onbeforeunload = function() {
        if (FormViewMode != "ReadOnly") {
            var edit = '<% = editFlag.ClientID %>';
            editFlag = document.getElementById(edit).value;
            if (editFlag == 1) {
                    event.returnValue = "You'll lose changes!";
            }
        }
    };
    $(document).ready(function() {
        $("#divInsert input[type=image],.obsItem input[type=image]").click(
            function() {
                var edit = '<% = editFlag.ClientID %>';
                document.getElementById(edit).value=1;
         });

        $("#divInsert input[type=image],input[id*='UpdateButton'],input[id*='InsertButton'],.obsItem input[type=image]").click(
            function() {
                window.onbeforeunload = null;
            }
        );
    });  

What I don't understand is that, for some reason, when I click in Update/Cancel, which are ImageButton controls, I get the alert box, but if I click "Cancel", I stay on the current page but all the ImageButton controls just stop doing postbacks. What is really strange is that all the other links in the page work well (show the alert box, if I click Cancel I can still click in other links).

The behaviour is somehow similar to the one reported here: http://forums.asp.net/p/1510877/3599900.aspx

I'm using IE8 by the way.