2013-07-03 82 views
0

如何在执行.net代码之前获得一个asp:按钮来执行一些jquery?执行jQuery,然后执行.NET

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

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

所以我的问题是,我如何设置这个,所以当单击asp:按钮时,jquery是否是事情,并且一旦jquery完成,.net部分运行?

+0

你的意思是提交表单? – kingdamian42

+0

是的,所以如果它只是一个简单的jQuery,那么提交会被一个警告停止,例如。当警报关闭时,提交继续。但是,如果它是复杂的jQuery,我认为jquery不能够快速完成以停止提交?不知道为什么它不起作用。 – oshirowanen

+1

这是因为AJAX变得异步,而不是阻塞。您可以使用'e.preventDefault()'阻止jQuery中的提交,然后在AJAX的'success'回调中调用点击的POST。 –

回答

-1
  1. 首先是您的ajax调用异步或同步? 你会使用推迟和承诺概念

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

+0

它是异步的,或者他不会有问题,第二个建议不好,因为它不会触发服务器端的asp.net事件。 – Archer

+0

它只会触发服务器端事件,他需要在aspx中为其他按钮创建runat = server。 –

+0

它不会触发服务器上的按钮单击事件。 – Archer

0

普利文的观点(以下downvoted)约异步或同步是非常重要的位置。

“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'); 

除非你正在处理的承诺或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>