views:

371

answers:

0

I hesitate to ask because I think this will be hard to explain.

I have an asp.net page with several input fields, cancel/submit buttons, and a gridview in an update panel. There's a button to add new records to the gridview, and that is handled with a jquery dialog (to maintain a consistent look/feel with rest of site) with a ajax call to save.

The page worked fine until I tried to implement a warning about leaving the page ("Are you sure you want to navigate away....etc"). I had to leave out the dialog part but that's all good.

$('input:text,input:checkbox,input:radio,textarea,select').not('#new_dialog *').one('change', function() {
            window.onbeforeunload = function() { return 'Leaving this page will cause any unsaved data to be lost.' };
        });

I also did a thing to allow inline editing of the grid by isolating any clicks within the grid to turn off the warning:

  $('#<%=GridView1.ClientID %> a').click(
        function() {

            //remove any existing warning from interfering with update panel
            if (window.onbeforeunload != null) {
                reinstateWarning = true;
                $('BODY').removeAttr('onbeforeunload');
            }
        });

and then turning it back on when the update is finished:

 function pageLoad(sender, args) {

            Sys.WebForms.PageRequestManager.getInstance().add_endRequest(
                 function(sender, args) {

                     if (reinstateWarning) {
                         window.onbeforeunload = function() { return 'Leaving this page will cause any unsaved data to be lost.' };
                     }
                 });
        }
    }

Which might not be the right way but was all I could think of to temporarily prevent the exit warning.

So that's all good but now when the new record gets saved via jquery dialog, I don't know how to get the grid to rebind and show the new record without losing both status of window.onbeforeunload and any edited but unsaved data in the regular textboxes.

So basically what I need to accomplish is to add a new record with the dialog, have that new record displayed in the gridview, all without doing a full postback.