I have a LinkButton on a page that performs a post-back, but also has an onClientClick
event. The idea is to set some session variables in the background from client-side data (don't ask).
I've placed a break point in the web method to step through our code, and what we're experiencing is that depending on the browser, the PageMethods may return a success message, failure message, or no message at all. Additionally, the web method may or may not get called, regardless of the PageMethods result.
Here's a handy little chart of our results:
Browser PageMethods WebMethod
-------------- ------------- --------------------
IE 8, 9, 10 Success Called successfully
Safari 5.1.7 Failure *Never called*
Firefox 25.0.1 *Neither* Called successfully
Chrome v31 Failure Called successfully
That's four different browsers, and four different results.
I've tried generating the link button in both server-side and client-side code with the same effect, and without even setting the session variables in the WebMethod, with the same results.
The code can be reproduced with the following simple code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script type="text/javascript">
function doStuff() {
var a = 'a';
var b = 'b';
PageMethods.doStuffWebMethod(a, b, doStuffSuccess, doStuffFail);
}
function doStuffSuccess() {
alert(Success!');
}
function doStuffFail() {
alert(Failure!');
}
</script>
<html>
<body style="background-color:#f3f4f6;" >
<form runat="server" name="mainForm" id="mainForm" action="Test.aspx">
<asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server"></asp:ScriptManager>
<asp:LinkButton runat="server" CausesValidation="false" OnClientClick="doStuff();">Do stuff!</asp:LinkButton>
</form>
</body>
</html>
and
protected void Page_Load(object sender, EventArgs e)
{
LinkButton lbAdd = new LinkButton();
lbAdd.Text = "Web method test";
lbAdd.CausesValidation = false;
lbAdd.OnClientClick = "doStuff();";
mainForm.Controls.Add(lbAdd);
}
[WebMethod]
public static void doStuffWebMethod(string a, string b)
{
try
{
//System.Web.HttpContext.Current.Session["a"] = a;
//System.Web.HttpContext.Current.Session["b"] = b;
string x = a + b;
}
catch (Exception ex)
{
//
}
}
The question:
Why is my web method failing in Safari, and giving me one of three different return messages in three other browsers?
How can I change this code to get it to work in the browsers mentioned?
*
shrug*
– Subterfuge