Using Page Methods with Ajax in ASP.Net 3.5
In your codebehind, make sure you include these two references:
using System.Web.Script.Services; using System.Web.Services;
Then make sure the method you wish to connect with from javascript matches the following pattern. The method must have both attributes, be public and be static.
[WebMethod]
[ScriptMethod]
public static Object SaveData(Object data)
{
In your .aspx file (or master page) you must have a scriptmanager object with the following pattern.
<asp:ScriptManager ID="ScriptManager1" runat="server"
EnablePageMethods="true">
You are setup for page methods!
You will find that javascript intellisense won’t help you with finding page methods. To make sure it’s all there, you can look in the source of your page in a browser. You will find something like:
PageMethods.prototype =
_get_path:function()
{
var p = this.get_path();
if (p)
{
return p;
}
else
{
return PageMethods._staticInstance.get_path();
},
SaveData:function(data, succeededCallback,
failedCallback, userContext)
The last part is our asynchronous page method call. Like a webservice, you call it in javascript like this:
function SaveData()
{
PageMethods.SaveData(data, SaveData_onSuccess,
OnError, userContext);
}
function SaveData_onSuccess(data)
{
alert(data);
}