Troubles with calling a PageMethod from javascript [solved]
-
I've been struggling with the same problem for a few days now without getting anywhere. In my aspx page I try to call a PageMethod from a javascript and then inserting the result of that call into a div. MyPage.aspx
<ajaxToolkit:ToolkitScriptManager runat="server" ID="ScriptManager1" EnablePageMethods="true">
<Scripts>
<asp:ScriptReference Path="~/javascript/myscript.js" />
</Scripts>
</ajaxToolkit:ToolkitScriptManager>
...
...
<a href="javascript:myFunction();">Click to run script</a>
...
...
<div id='theDiv'></div>MyPage.aspx.cs
[System.Web.Services.WebMethod]
public static string ReturnTheString()
{
return "This is the string returned";
}And finally my javascript function where everything goes wrong myscript.js
function myFunction()
{
PageMethods.ReturnTheString(OnSucceeded, OnFailed);
}function OnSucceeded(result)
{
var myDiv = document.getElementById('theDiv');
myDiv.innerHTML = result;
}function OnFailed(error)
{
alert(error.get_message());
}The problem is that I don't get the expexted "This is the string returned" as result in my OnSucceeded js function. And when I debug my website and set up a breakpoint in ReturnTheString() the code never runs.. My result parameter is sometimes just empty and sometimes has the value of almost my whole page (starting with
-
I've been struggling with the same problem for a few days now without getting anywhere. In my aspx page I try to call a PageMethod from a javascript and then inserting the result of that call into a div. MyPage.aspx
<ajaxToolkit:ToolkitScriptManager runat="server" ID="ScriptManager1" EnablePageMethods="true">
<Scripts>
<asp:ScriptReference Path="~/javascript/myscript.js" />
</Scripts>
</ajaxToolkit:ToolkitScriptManager>
...
...
<a href="javascript:myFunction();">Click to run script</a>
...
...
<div id='theDiv'></div>MyPage.aspx.cs
[System.Web.Services.WebMethod]
public static string ReturnTheString()
{
return "This is the string returned";
}And finally my javascript function where everything goes wrong myscript.js
function myFunction()
{
PageMethods.ReturnTheString(OnSucceeded, OnFailed);
}function OnSucceeded(result)
{
var myDiv = document.getElementById('theDiv');
myDiv.innerHTML = result;
}function OnFailed(error)
{
alert(error.get_message());
}The problem is that I don't get the expexted "This is the string returned" as result in my OnSucceeded js function. And when I debug my website and set up a breakpoint in ReturnTheString() the code never runs.. My result parameter is sometimes just empty and sometimes has the value of almost my whole page (starting with