1

I've got a mixture of JQuery and Ajax partial postbacks on a webforms page and I'm trying to stop the user navigating away from a page without their changes having been saved.

It's working pretty well unless one of the data validators on the page is invalid. There is a jquery method on the submit button that basically nulls the onbeforeunload event. In the event that the validators are invalid, I dont want that to happen, so I turned off the client side validation, and in the method, called Page.Validate(), then if the page was invalid, I tried to register a startup script as described here to reset like so:

if (!Page.IsValid) {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "", "setConfirmUnload(true);", true);
            return;
        }

If I run the click submit and the page is invalid, the call to setConfirmUnload(true) happens, but the method cant be found (presumably because I re-set this up every time a postback happens?

The actual error is " JavaScript runtime error: 'setConfirmUnload' is undefined", possibly because the bindEvents() hasnt yet re-run and therefore created it?

I thnk my problem stems from the fact that I am using partial postbacks and autopostbacks, setting up data as the user modifies the form. By default that blitzes my jquery, so I rebind the jquery calls on document.ready() as well as on postback using

<script type="text/javascript">
        // attach the event binding function to every partial update
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function (evt, args) {
            bindEvents();
        });
    </script>

And my Bind Events method looks like:

 function bindEvents() {

        // stuff removed for brevity here...


        $(function () {
            // Prevent accidental navigation away
            $(':input, select').bind(
                'change', function () { setConfirmUnload(true); });

            $('.noprompt-required').click(
                function () { setConfirmUnload(false); });

            function setConfirmUnload(on) {
                window.onbeforeunload = on ? unloadMessage : null;
            }
            function unloadMessage() {
                return ('You have entered new data on this page. ' +
                        'If you navigate away from this page without ' +
                        'first saving your data, the changes will be lost.');
            }

            window.onerror = UnspecifiedErrorHandler;
            function UnspecifiedErrorHandler() {
                return true;
            }

        });

    }        

If that's the case, is there a way I can force the script to run after the bindEvents() call has happened?

1
  • Sorry, not sure whhat you mean? I'm asking is there a way to make the register startup script logic fire after the postback has completed? Commented Sep 15, 2013 at 22:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.