ASP.NET AJAX UpdatePanel - 在 InitializeRequest 中取消回发不会设置回重新启用

发布于 2024-09-27 08:32:34 字数 1278 浏览 4 评论 0原文

在我的页面上的 ASP.NET AJAX UpdatePanel 中,我有一个提交按钮,其中应用了一些自定义 JavaScript 验证,该验证根据验证结果设置全局 _cancelGeneralSettingsUpdate 标志。

除非您输入无效值、更正并重新提交,否则一切正常。在这种情况下,变量 _cancelGeneralSettingsUpdate 已正确设置为 false,但在页面尝试通过 AJAX 回发之前未调用 initializeRequest 函数,从而导致回发失败。

一旦纠正了无效输入并且用户再次单击提交按钮,我需要能够成功回发。不幸的是,一旦回发被取消,我似乎没有正确连接到 PageRequestManager 管道。如果我刷新页面,一切都会恢复正常,但通过 args.set_cancel(true); 取消回发; 让我陷入一种状态,在页面刷新之前我无法通过 AJAX 进行进一步的回发。

如何重新启用回发或导致initializeRequest 触发并在后续回发之前进行设置?

我附上了一些我的代码。假设在尝试回发之前 _cancelGeneralSettingsUpdate 设置正确,我已经验证了这一点。

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);
        }
    }

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);
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奈何桥上唱咆哮 2024-10-04 08:32:34

想法我找到了解决方案。在我的例子中,正确的做法是在 JavaScript 验证失败时调用此函数

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

在上面的示例中,我在自定义 initializeRequestargs.set_cancel(false); > 处理程序。虽然这在第一个实例中有效,但在用户更正其无效输入并尝试重新提交表单后,它不允许将来的回发成功。

这实际上只会中止当前正在进行的回发。就我而言,尚未调用 __doPostBack(...),因此 Sys.WebForms.PageRequestManager.getInstance().abortPostBack(); 默默地未能执行我的操作正在期待。

最终结果是使用两个按钮系统。如果不修复 AJAX 库的错误,这是唯一剩下的合理选择。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文