當我使用OnClientClick屬性從ASP.NET按鈕調用ScriptMethod時,ScriptMethod返回95%的時間(警告框顯示),並提交頁面。在表單提交上調用ScriptMethod?
如果我從HTML輸入按鈕調用方法,ScriptMethod返回100%的時間,但當然頁面不提交。如果我改變提交類型,我會回到接下來的95%。
我是否必須改變我的方法,並在我的ScriptMethod返回後調用javascript提交,還是有辦法使這個成功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();
}
}
}
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的布爾參數,而寫這問題,它似乎在100%的時間內工作。這當然可能是巧合。