Javascript 在使用客户端验证器在.NET页面中回发时,如何最好地处理承诺或AJAX?

Javascript 在使用客户端验证器在.NET页面中回发时,如何最好地处理承诺或AJAX?,javascript,asp.net,ajax,asynchronous,promise,Javascript,Asp.net,Ajax,Asynchronous,Promise,我一直在努力将异步Javascript添加到现有的.NET代码中,因为已有表单包含客户端验证器和与回发事件相关的按钮 到目前为止,我找到的处理AJAX或提交页面时需要运行的承诺的最佳方法是:如果有人有更干净的解决方案,可以保留.NET客户端验证和事件模型,我希望得到建议 1) 。使用javascript停止按钮的默认行为: OnClientClick="return false;" 2) 。为按钮创建单独的Javascript事件处理程序: $(document).ready(function

我一直在努力将异步Javascript添加到现有的.NET代码中,因为已有表单包含客户端验证器和与回发事件相关的按钮

到目前为止,我找到的处理AJAX或提交页面时需要运行的承诺的最佳方法是:如果有人有更干净的解决方案,可以保留.NET客户端验证和事件模型,我希望得到建议

1) 。使用javascript停止按钮的默认行为:

OnClientClick="return false;"
2) 。为按钮创建单独的Javascript事件处理程序:

$(document).ready(function (e) {
            $("[id$='btn']").click(function(){ 
3) 。调用客户端ASP验证程序(如果没有,请跳过)

4) 。传递一个函数以提交触发按钮单击事件的表单

__doPostBack('<%= btn.ClientID %>', 'OnClick');
uu doPostBack('OnClick');
除非您处理的是承诺或AJAX,否则这一切看起来都非常复杂

以下是完整的示例页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="aaaaa_test.aspx.cs" Inherits="test" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    <script  type="text/javascript">

        $(document).ready(function (e) {

            //2). use the click event to capture the canceled submit.
            $("[id$='cmdSubmit']").click(function(){
                //3). Allow the asp client-side page validators to do their thing...
                if (Page_ClientValidate())
                {
                    ReturnsPromise().then(function () {
                        //4). once the Asynchronous stuff is done, re-trigger the postback
                        //    and make sure it gets handled by the proper server-side handler.
                        __doPostBack('<%= cmdSubmit.ClientID %>', 'OnClick');
                    });
                }
            });

            // very simple promise, usually this woud be an AJAXy thing.....
            //  a placeholder for your real promise.
            function ReturnsPromise()
            {
                //this is just a simple way to generate a promise for this example.
                return Promise.resolve($("[id$='hiddenField']").val($("[id$='txtInput']").val()));
            }

        });

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:HiddenField ID="hiddenField" runat="server" />
        Input:<asp:TextBox ID="txtInput" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="valInput" runat="server" ErrorMessage="Add Text" ControlToValidate="txtInput" EnableClientScript="True" Text="Add Text"></asp:RequiredFieldValidator>
        <br />
        <!-- 1). use client script to stop the default button behavior with OnClientClick="return false;" -->
        <asp:Button ID="cmdSubmit" runat="server" OnClick="cmdSubmit_Click" Text="Submit" OnClientClick="return false;" />

    </div>
    </form>
</body>
</html>

$(文档).ready(函数(e){
//2) 。使用单击事件捕获已取消的提交。
$(“[id$='cmdSubmit']”)。单击(函数(){
//3) 。允许asp客户端页面验证程序执行其操作。。。
如果(第\u页ClientValidate())
{
ReturnsPromise()。然后(函数(){
//4) 。完成异步操作后,重新触发回发
//并确保它由适当的服务器端处理程序处理。
__doPostBack(“'OnClick');
});
}
});
//非常简单的承诺,通常这会是一件很简单的事情。。。。。
//你真正的承诺的占位符。
函数ReturnsPromise()
{
//这只是为本例生成承诺的一种简单方法。
返回Promise.resolve($(“[id$='hiddenField']”)val($(“[id$='txtInput']]”)val());
}
});
输入:


承诺并不是每一段异步代码的答案——特别是如果您错误地认为承诺会产生异步代码synchronouse@JaromandaX-随机称某人“错了”是非常奇怪的。尤其是因为我找不到你评论的任何意义。上面代码的要点是让本机.net Post在提交之前“等待”异步承诺或AJAX完成。你对我的文章的观点或问题是什么?承诺并不是每一段异步代码的答案——特别是如果你错误地认为承诺会产生异步代码synchronouse@JaromandaX-随机称某人“错了”是非常奇怪的。尤其是因为我找不到你评论的任何意义。上面代码的要点是让本机.net Post在提交之前“等待”异步承诺或AJAX完成。你对我的帖子有什么看法或问题?
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="aaaaa_test.aspx.cs" Inherits="test" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    <script  type="text/javascript">

        $(document).ready(function (e) {

            //2). use the click event to capture the canceled submit.
            $("[id$='cmdSubmit']").click(function(){
                //3). Allow the asp client-side page validators to do their thing...
                if (Page_ClientValidate())
                {
                    ReturnsPromise().then(function () {
                        //4). once the Asynchronous stuff is done, re-trigger the postback
                        //    and make sure it gets handled by the proper server-side handler.
                        __doPostBack('<%= cmdSubmit.ClientID %>', 'OnClick');
                    });
                }
            });

            // very simple promise, usually this woud be an AJAXy thing.....
            //  a placeholder for your real promise.
            function ReturnsPromise()
            {
                //this is just a simple way to generate a promise for this example.
                return Promise.resolve($("[id$='hiddenField']").val($("[id$='txtInput']").val()));
            }

        });

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:HiddenField ID="hiddenField" runat="server" />
        Input:<asp:TextBox ID="txtInput" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="valInput" runat="server" ErrorMessage="Add Text" ControlToValidate="txtInput" EnableClientScript="True" Text="Add Text"></asp:RequiredFieldValidator>
        <br />
        <!-- 1). use client script to stop the default button behavior with OnClientClick="return false;" -->
        <asp:Button ID="cmdSubmit" runat="server" OnClick="cmdSubmit_Click" Text="Submit" OnClientClick="return false;" />

    </div>
    </form>
</body>
</html>