【问题标题】:Calling a ScriptMethod on form submission?在提交表单时调用 ScriptMethod?
【发布时间】:2009-04-24 15:23:30
【问题描述】:

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

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

我是否必须改变我的方法,并在我的 ScriptMethod 返回后从 javascript 调用 submit,或者有没有办法让这 100% 的时间成功?通过成功,我的意思是在表单提交之前实际返回 100% 的时间。 ScriptMethod 实际上永远不会失败。

页面:

<%@ 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();
        }
    }
}

脚本服务:

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% 的时间都有效。这当然可能是巧合。

【问题讨论】:

    标签: asp.net ajax asp.net-ajax postback


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      你忘了:

      using TheForce;
      
      namespace Luke {
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-05-29
        • 2013-09-24
        • 1970-01-01
        • 1970-01-01
        • 2021-09-14
        • 2012-02-17
        • 1970-01-01
        相关资源
        最近更新 更多