Scenario: I have onbeforeunload
function on .aspx page which I am using to display a message before navigating away from current page (ASP.NET Application). This page is quite complex where post backs are happening at quite a few times e.g. I have multiple buttons, Rad Grid, Rad Window PopUps etc.
So, all the buttons call onbeforeunload
whenever they are clicked and message box is being called un-necessarily. To prevent onbeforeunload
message displaying I am just calling another cancelEvent
function on button's OnClientClick
event where I am just calling null. By doing this all the button post backs are handled but the RadGrid which I am using has Button Columns (Delete) and whenever a row is deleted from the grid it offcourse refreshes the grid which ultimately calls onbeforeunload
.
Could please anyone tell me how I can prevent onbeforeunload
when I delete anything from RadGrid. Following is the code snippet that I am using and working fine for buttons but I need help for grid delete column may be some event like Button's OnClientClick
event that I can use to call my cancelEvent
.
window.onbeforeunload = confirmExit;
function confirmExit() {
window.event.returnValue = 'If you navigate away from this page any unsaved changes will be lost!';
}
function cancelEvent(event) {
window[event] = function () {null}
}
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" CssClass="ActionButtons"
ValidationGroup="vsDataEntry" OnClientClick="return cancelEvent('onbeforeunload')"/>
Try doing something like the below
<div onclick="return cancelEvent('onbeforeunload')">
<telerik:RadGrid runat="server">
</telerik:RadGrid>
</div>
Explanation
When the click event is triggered it is bubbled through all the element in DOM. We are wrapping the Radgrid inside a div and whenever any click happens inside that div we are removing the onbeforeunload.