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.
Thank you for the feedback!We believe providing such will help all users benefit from it.
Thanks for the reply.
I was finally able to solve by setting the property EnableRenderingAnchors="false" in the WebDropDown.
Regards.
Hello,
After investigating this matter it appears it could be considered a common issue in IE. It is a cancelable event as stated at: https://msdn.microsoft.com/en-us/library/ie/ms536907(v=vs.85).aspx What is more it appears to be considered as feature of IE explorer to always bring this confirmation dialog up. For example this event could be triggered by some of the following:
Click an anchor that refers to another document.Invoke the anchor . click method.Invoke the window . navigate or NavigateAndFind method.Specify a new value for the location . href property.When the user clicks on the hyperlink or attempts to close the window, onbeforeunload event fires on the body and a dialog box displays.
By design all items in WebDropDown are rendered as list elements containing anchor in the following format <a href="BLOCKED SCRIPTvoid(0)">Item Text Comes Here</a>. The only scenario when the items are not containing such format is when using item templates. In all other browsers this anchor will not trigger this confirmation dialog, but as commented in IE it will. You could find more details regarding this issue at: How can I prevent window.onbeforeunload from being triggered by BLOCKED SCRIPT href links in IE? onbeforeunload event is too enthusiastic in IE9 I suggest re-assign the onbeforeunload event will be enough to workaround this behavior in IE.
Please let me know if you have additional questions regarding this matter.
By the way, it only happens in Explorer.
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?