Wednesday, June 10, 2009
Reminder to self: How to use ASP.NET AJAX PageMethods
I keep forgetting how to use the ASP.NET AJAX PageMethods (.NET 3.5), so here is a short examples that gets me up to speed:
In Default.aspx.cs insert the following static method declared with the WebMethod attribute:
[WebMethod] public static Person DoSomethingServerSide( string name, string email) { Person p = new Person() { Name = name, Email = email }; return p; }
...and in the Default.aspx insert some JavaScript that leverage this PageMethod:
function DoSomethingClientSide() { PageMethods.DoSomethingServerSide( "Test", "test@test.com", Callback); } function Callback(res) { alert(res.Name + " " + res.Email); }
And that's it... no wonder I keep forgetting how easy it is.


My name is Lars Hundebøl and I work for 
propellerhead