When I call a ScriptMethod from an ASP.NET button using the OnClientClick property, the ScriptMethod returns 95% of the time (alert box shows), and the page submits.
If I call the method from a HTML input button, the ScriptMethod returns 100% of the time, but of course the page doesn't submit. If I change the type to submit, I'm back to the succeeding 95%.
Do I have to change my approach, and call submit from javascript after my ScriptMethod returns, or is there a way to make this succeed 100% of the time? By success, I mean actually returning 100% of the time before the form submits. The ScriptMethod never actually fails.
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>
Code behind:
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";
}
}
}
}
One thing of interest...I removed the bool parameter from the SciptMethod while writing this question, and it seems to work 100% of the time. This of course may be coincidence.