0

如何在执行 .net 代码之前获得一个 asp:button 来执行一些 jquery?

我基本上有一个 asp:button,它附加了一些 jquery-ajax 和一些 .NET 代码。我注意到,如果 jquery 很简单,比如显示警报,一切正常,但如果 jquery 更复杂,即它必须去连接数据库的 ajax,它似乎不起作用,它只是执行互联网。

我假设这与速度有关?.NET 代码在 jQuery 脚本完成之前启动?

所以我的问题是,我如何设置它,以便当单击 asp:button 时,jquery 会执行此操作,并且一旦 jquery 完成,.net 部分就会运行?

4

3 回答 3

1

尝试使用 OnClientClick 方法,如果返回 false 它将停止回发。

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclientclick.aspx

<asp:Button id="btn" runat="server" OnClientClick="return onclick()" text="Click" />

JavaScript:

function onclick()
{
   // do something
   return false; // this will stop the postback
}
于 2013-07-03T15:01:34.667 回答
0

Praveen 关于异步或同步的观点(如下,被否决)在这里非常重要。

“OnClientClick”答案只适用于同步的东西。如果您有 AJAX 或 Promise,我发现的最佳模式是:

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

OnClientClick="return false;"

2)。为按钮创建一个单独的 Javascript 事件处理程序:

$(document).ready(function (e) {
            $("[id$='btn']").click(function(){ 

3)。调用客户端 ASP 验证器(如果没有,请跳过)。

if (Page_ClientValidate())
            {

4)。给AJAX补全函数,或者在promise.then()中传递一个函数提交表单触发按钮点击事件

__doPostBack('<%= btn.ClientID %>', 'OnClick');

除非您正在处理 Promises 或 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>
于 2016-01-19T01:07:41.903 回答
-1
  1. 首先是你的 ajax 调用 async 还是 sync ?您将使用 defer 和 promise 概念进行 ajax 调用

2、创建一个隐藏按钮,ajax调用成功后点击jquery中的按钮。而真正的按钮返回false,所以它不会执行回发。

于 2013-07-03T14:58:46.460 回答