Ben Dotnet is right about using ScriptService in asp.net WebForms. In addition to using the ScriptService decorator, the GenerateScriptType decorator is important in order to include the complex type that you want to use. I found articles that were related to Ben, in addition to this: http://www.webreference.com/programming/asp-net-ajax/complex-data-types/index.html
This is how I was able to do exactly what you are trying. First, I defined a custom type that I wanted to use in my code behind the file.
namespace TestProject { public class SampleData { public int id { get; set; } public string StartDate { get; set; } public string EndDate { get; set; } public SampleData() { } } public partial class SamplePage : System.Web.UI.Page { } }
Then I created the WebMethod / ScriptMethod method in the SamplePage code as follows:
[WebMethod] [ScriptMethod] [GenerateScriptType(typeof(SampleData))] public static bool EditReminder(SampleData data) { return true; }
Then, on the client page, I managed to create an object of type SampleData and pass it using PageMethods such as this. Remember to include the namespace, this is necessary.
function some_javascript_function () { var sample_data = new TestProject.SampleData() sample_data.id = 1; sample_data.StartDate = '6/24/1976'; sample_data.EndDate = '3/20/2012'; PageMethods.EditReminder(sample_data, OnEditReminderComplete) } function OnEditReminderComplete () { if (result) alert("Success!"); else alert("Failure!"); }
Also, be sure to enable the script manager and enable page methods such as this somewhere on your page:
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
Steve source share