从jQuery的prevent ASP.net __doPostBack()提交()内的UpdatePanel [英] Prevent ASP.net __doPostback() from jQuery submit() within UpdatePanel

查看:97
本文介绍了从jQuery的prevent ASP.net __doPostBack()提交()内的UpdatePanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想停止对表格提交回传,如果我的自定义的jQuery验证返回false。

I'm trying to stop postback on form submit if my custom jQuery validation returns false.

有什么办法prevent的__doPostBack()函数提交()函数中整理而成?

Is there any way to prevent the __doPostback() function finishing from within the submit() function?

我会假设:

$('#aspnetForm').submit(function () { return false; });

会做的伎俩,但显然这不是这种情况:没有任何人有一个建议

would do the trick, but apparently that's not the case: does anyone have a suggestion?

在提交()函数阻止回发(如果您暂停在萤火断点也不会回传),但我似乎无法阻止事件发生后提交()函数就完成了!

The submit() function does block the postback (it won't postback if you pause at a breakpoint in firebug), but I can't seem to stop the event happening after the submit() function is complete!

干杯,埃德

修改

OK,我对一个快速的烂摊子,发现事实,我使用导致回发的按钮被绑定到一个UpdatePanel作为asyncpostbacktrigger似乎是这个问题:如果我删除它作为一个触发(即导致其产品完全回发)时,是没有问题的preventing回传,返回FALSE;

OK, I had a quick mess about and discovered that the fact that the button I'm using to cause the postback is tied to an updatepanel as an asyncpostbacktrigger seems to be the problem: If I remove it as a trigger (i.e. cause it to product a full postback), the is no problem preventing the postback with return false;

任何想法,为什么使用返回false异步回发就不会停止的?

Any ideas why the async postback would not be stoppable using return false?

推荐答案

您必须使用客户端的 PageRequestManager 妥善处理AJAX提交事件。 (如prevent一个异步回发。)

You have to use the client side PageRequestManager to properly handle AJAX submit events. (e.g. prevent an async postback.)

如果你是不是在页面的总量控制,有可能只是调用 __ doPostBack页面() JavaScript的没有经过任何页面逻辑去。
在这种情况下 - 除了上面 - ,你要存储旧窗口.__ doPostBack(),并提供自己的 - 在他的评论中提到@tucaz。
(......正如你提到的,它可以得到相当链接傻了眼。)
对于定期提交(非Ajax),可以提供事件处理程序,其他人指出。

If you are not in total control of the page, there can be JavaScript on the page that just calls __doPostBack() without going through any page logic. In this case - in addition to the above -, you have to store the old window.__doPostBack() and provide your own - as @tucaz mentioned in his comments. (...and as you mentioned, it can get quite perplexing with chaining.) For regular submits (non-AJAX), you can provide an event handler as others have pointed out.

本页面可能会有所帮助并具有一定的code样品使用 PageRequestManager 。特别是:

This page might be of help and has some code samples that use PageRequestManager. In particular:

initialize : function()
{
    ...

    this._onSubmitHandler = Function.createDelegate(this, this._onSubmit);
    this._onPartialUpdateEnd = Function.createDelegate(this, this._onUpdateEnd);

    if (typeof(Sys.WebForms)!== "undefined" && typeof(Sys.WebForms.PageRequestManager)!== "undefined")
    {
        Array.add(Sys.WebForms.PageRequestManager.getInstance()._onSubmitStatements, this._onSubmitHandler);
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(this._onPartialUpdateEnd);
    }
    else 
        $addHandler(document.forms[0], "submit", this._onSubmitHandler);
},

修改
按照上面的,这一点,例如工作正常,我(.NET 3.5 SP1, Button1的是UpdatePanel的触发器,等...):

Edit: Following the above, this, for example works fine for me (.Net 3.5 SP1, Button1 is trigger in the updatepanel, etc...):

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.js" charset="utf-8"
        type="text/javascript"></script>

</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <div>
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
            </div>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="Button1" />
        </Triggers>
    </asp:UpdatePanel>
    </form>
    <script type="text/javascript">
        (function($, undefined) {

            var submitHandler = function(e) {
                return false;
            }

            if (typeof (Sys.WebForms) !== "undefined" && typeof (Sys.WebForms.PageRequestManager) !== "undefined") {
                Array.add(Sys.WebForms.PageRequestManager.getInstance()._onSubmitStatements, submitHandler);
            } else {
                $("form").submit(submitHandler);
            }
        })(jQuery);
    </script>
</body>
</html>

这篇关于从jQuery的prevent ASP.net __doPostBack()提交()内的UpdatePanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆