代码之家  ›  专栏  ›  技术社区  ›  Aaron Daniels

在表单提交时调用ScriptMethod?

  •  0
  • Aaron Daniels  · 技术社区  · 16 年前

    当我使用onclientClick属性从ASP.NET按钮调用scriptmethod时,scriptmethod返回95%的时间(显示警报框),页面提交。

    如果我从HTML输入按钮调用该方法,scriptmethod将返回100%的时间,但当然页面不会提交。如果我把类型改成submit,我就回到后面的95%。

    在我的scriptmethod返回后,我是否必须更改我的方法,并从javascript调用submit,或者是否有一种方法可以让这个方法100%成功?我说的成功,是指在提交表单之前100%返回。scriptmethod从未真正失败过。

    Page:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ScriptServiceMadness._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>Untitled Page</title>    
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:ScriptManager ID="ScriptManager1" runat="server">
                <Services>
                    <asp:ServiceReference Path="Chewbacca.asmx" />
                </Services>
            </asp:ScriptManager>
        <div>
            <asp:TextBox ID="VerifyPostbackTextBox" runat="server" Text="true" />        
            <asp:Button ID="SubmitFormButton" runat="server" OnClientClick="attack()" Text="Works 90%" OnClick="SubmitFormButton_Click" />        
            <input type="button" id="InputButton" value="Works 100%" onclick="attack()" />
        </div>
        </form>
    
        <script type="text/javascript">
            function attack()
            {
                ScriptServiceMadness.Chewbacca.ShootImperialProbeDroid(true, speak, youvefailedme);            
            }
    
            function speak(result)
            {
                alert(result);            
            }
    
            function youvefailedme(error)
            {
                alert(error);
            }
        </script>
    </body>
    </html>
    

    代码落后:

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    namespace ScriptServiceMadness
    {
        public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void SubmitFormButton_Click(object sender, EventArgs e)
            {
                VerifyPostbackTextBox.Text = DateTime.Now.Ticks.ToString();
            }
        }
    }
    

    ScriptService:

    using System;
    using System.Data;
    using System.Web;
    using System.Collections;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    using System.ComponentModel;
    using System.Web.Script.Services;
    using System.Threading;
    
    namespace ScriptServiceMadness
    {
        /// <summary>
        /// Summary description for Chewbacca
        /// </summary>
        [ScriptService]
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [ToolboxItem(false)]
        public class Chewbacca : System.Web.Services.WebService
        {
            [ScriptMethod]
            [WebMethod]
            public string ShootImperialProbeDroid(bool letHanDoIt)
            {
                if (letHanDoIt)
                {
                    return "rrrroooorrrr";
                }
                else
                {
                    return "rrrraaaarrrr";
                }
            }
        }
    }
    

    有一件事很有趣……我在写这个问题的时候从sciptmethod中删除了bool参数,它似乎100%都能工作。这当然是巧合。

    2 回复  |  直到 10 年前
        1
  •  1
  •   Atanas Korchev    16 年前

    因为脚本方法是异步执行的,所以您不能保证它会在表单回发之前返回。当您联机部署并且您的用户与Web服务的连接较慢时,更改不会增加。我建议您在收到响应后,通过调用\doPostback例程手动回发。

        2
  •  0
  •   Chad Grant    16 年前

    你忘了:

    using TheForce;
    
    namespace Luke {
    }