I want to show a confirm dialog when the user selects an item in a DropDownList. If the user presses "Cancel", I want to stop the postback. Here is the function I add to the onchange event:
function imitateConfirmUnload(event) {
if (window.onbeforeunload = null)
return true;
return confirm("Are you sure you want to navigate away from this page?\n\nYou have unsaved changes\n\nPress OK to continue or Cancel to stay on the current page.");
}
And this is the relevant bit of code in my startup script to add the handler to the event:
function addConfirmToWarnClasses() {
$('.warn').change(function() {
imitateConfirmUnload()
});
}
The problem is that the postback occurs even if the user selects "Cancel". If I move the handler on to the click event, it works. But it feels clunky to me.
Edit
Correction: it doesn't work onclick, because the dialog prevents selection, so when the user selects OK, no change has taken place, and no postback when you want it!
Edit 2
The DropDownList is inside an UpdatePanel so that may affect behaviour.