This is a migrated thread and some comments may be shown as answers.

RadGrid causes beforeunload to fire

1 Answer 149 Views
Grid
This is a migrated thread and some comments may be shown as answers.
n/a
Top achievements
Rank 1
n/a asked on 04 Jun 2020, 04:42 PM

    I am using a javascript to check for unsaved changes, but when editing, adding a new line or refreshing the grid, the unsaved changes message box appears.  how can i stop this?   javascript code below:

 

<script type="text/javascript">
    // Store form state at page load
    var initial_form_state = $('#form1').serialize();

    // Store form state after form submit
    $('#form1').submit(function () {
        initial_form_state = $('#form1').serialize();
    });

    // Check form changes before leaving the page and warn user if needed
    $(window).bind('beforeunload', function (e) {
        var form_state = $('#form1').serialize();
        if (initial_form_state != form_state) {
            var message = "You have unsaved changes on this page. Do you want to leave this page and discard your changes or stay on this page?";
            e.returnValue = message; // Cross-browser compatibility (src: MDN)
            return message;
        }
    });
</script>

1 Answer, 1 is accepted

Sort by
0
Doncho
Telerik team
answered on 09 Jun 2020, 02:19 PM

Hi,

The beforeunload event is fired when the document and its resources are about to be unloaded.

By default refreshing the RadGrid causes a complete PostBack of the page and therefore the page is unloaded and loaded again, respectively the beforeunload event is fired.

To avoid reloading the whole page, and firing the beforeunload event,  when a PostBack is forced by the RadGrid, you can enable AJAX for the RadGrid using one of the following approaches:

Check out the Understanding AJAX Controls article to get a better overview of Ajaxifying Controls.

I hope this will prove helpful!

Kind regards,
Doncho
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
Grid
Asked by
n/a
Top achievements
Rank 1
Answers by
Doncho
Telerik team
Share this question
or