Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
480
Clicking on the selected item fires onbeforeunload.
posted

Hi,

I have a webdropdown with autopostback = true. I have rewritten the _doPostback method to show a confirm navigation dialog if there are changes. If you click "No", then it prevents the postback and selects again the previous value in the dropdown.

The method looks like this:

doPostBack: function () { /* overide the postback function for the custom message use */
if (typeof __doPostBack == 'function') {
controls.postBack = __doPostBack;
__doPostBack = function (Arg1, Arg2) {
if (view.hasChanges()) {
$.confirm({
message: 'You have made changes to the selection without clicking the ‘Save selection’ button and your changes will be lost. Do you want to continue without saving?',
yesFn: function () {
myTask.controls.postBack(Arg1, Arg2);
},
noFn: function () {
//If the sender is a dropdown select again the old value
if (!(control === null) && control.constructor.prototype._thisType === "dropDown") {
//Unselect the new selected value
var selectedItem = control.get_items().getItem(control.get_selectedItemIndex());
if (selectedItem != null) {
selectedItem.unselect();
selectedItem.inactivate();
}
//Select the old value again
var items = control.get_items();
for (var i = 0; i < items.getLength(); i++) {
if (items.getItem(i).get_text() === action.oldValue) {
items.getItem(i).set_selected();
items.getItem(i).select();
control.set_selectedItemIndex(i);
control.set_currentValue(items.getItem(i).get_text(), true);
}
}
}
}
});
} else {
controls.postBack(Arg1, Arg2);
}
};
}
},

If you click "No", it unselects the new value and selects again the initial value. This seems to be working, the value in the textbox is correct and the value is highlighted in the dropdown. However, if you click again in the highlighted value, it's firing the onbeforeunload event, and I get the IE default confirm navigation message. 

Why is it firing this event, if in theory you are selecting the current selected value? This does not happen the first time the page is loaded, only after you select "No" in the dialog, ad the old value is selected by code.

Parents
  • 480
    posted

    I rectify, it indeed happens also the first time the page is loaded. 

    How can I prevent this from firing when I click on the actual selected value?

Reply Children