ASP.NET AJAX UpdatePanel - cancelling postback in InitializeRequest will not set back to re-enabled

Within an ASP.NET AJAX UpdatePanel on my page I have a submit button which has some custom javascript validation applied which set a global _cancelGeneralSettingsUpdate flag depending on the results of the validation.

Everything works unless you enter an invalid value, correct it, and resubmit. In this case the variable _cancelGeneralSettingsUpdate is correctly set to false but the initializeRequest function is not called before the page attempts to postback via AJAX, causing the postback to fail.

I need to be able to have postbacks succeed once the invalid input has been corrected and the user clicks the submit button again. Unfortunately I do not seem to be properly wired into the PageRequestManager pipeline once postback has been cancelled. If I refresh the page, everything works again, but cancelling the postback via args.set_cancel(true); puts me into a state in which I cannot make further postbacks via AJAX until the page is refreshed.

How can I re-enable postbacks or cause the initializeRequest to fire and set this before subsequent postbacks?

I have attached some of my code. Assume _cancelGeneralSettingsUpdate is set correctly before any attempt to postback, as I have verified this.

var _cancelGeneralSettingsUpdate = false;

function initializeRequest(sender, args) {
        var pageRequestManager = Sys.WebForms.PageRequestManager.getInstance();
        if (!pageRequestManager.get_isInAsyncPostBack() & args.get_postBackElement().id == '<%= UpdateWorkgroupGeneralButton.ClientID %>')
        {
            if (_cancelGeneralSettingsUpdate) {
                args.set_cancel(true);
                // Show error message
            }
            else {
                args.set_cancel(false);
            }
        }
        else {
            args.set_cancel(false);
        }
    }

Answers:

Answer

Thought I found the solution. The proper thing to do in my case is to call this function when my JavaScript validation fails

 Sys.WebForms.PageRequestManager.getInstance().abortPostBack();

In my example above, I was calling args.set_cancel(false); in a custom initializeRequest handler. While this worked for the first instance, it did not allow future postbacks to succeed after the user corrected their invalid input and attempted to resubmit the form.

This actually will only abort postbacks which are currently in-flight. In my case __doPostBack(...) had not yet been called, so Sys.WebForms.PageRequestManager.getInstance().abortPostBack(); silently failed to do what I was expecting.

End result was to use a two button system. Without bugfixing the AJAX library, this was the only reasonable option left.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.