Wednesday, March 28, 2012

PageMethods and Objects

Is it possible to send objects as parameters for a PageMethod and to return objects as the result of a PageMethod? I have a large amount of data (and multiple data types) that I want to collect from a form then send it to the server, and it would simplify things greatly if I could create an object rather than sending a bunch of individual parameters.

Thanks

Patrick

Yes, you can.

Among the list of of useless responses that one is very close to the top. Now I'll admit I didn't ask how to use objects in page methods if it's possible, but I tend to think the how is implied by the fact that I took the time to ask the question in the first place. Sense I appear to be wrong in my assumptions here is the followup question...

How do you use objects as a parameter or return value when calling a PageMethod? As far as I can see if I have a class defined in my website (call it MyClass) there is no equivilant class available in the client script.

Patrick


Sorry for the short response. First take a look athttp://ajax.asp.net/docs/tutorials/ConsumingWebServicesWithAJAXTutorial.aspx (especially the last example on the page) and see if that answers your questions.

If you retrieve something from the web service (or page method), modify it on the client, and then send it back, you shouldn't have to do much. (The serialization should do all the work for you.) But it sounds like you might need to instantiate an object of this complex type on the clientfirst. In that case, notice the [GenerateScriptType(typeof(ColorObject))] attribute on the web service. That's the indicator to ASP.NET AJAX that the proxy it generates should include a definition of that type.


The short answer is that you can, and all you have to do is write your pagemethod signature as you would for a server-side call but w/ the webmethodattrbiute added. The framework reflects on those WebMethods and makes proxy classes in javascript to represent all the objects in the parameters and return types for you. Pretty cool feature, if you ask me. THe only downside (sorta) is that it chokes on certain complex objects (e.g. objects nested in objects), so you might have to write your own custom converter, per the link the other guy sent you.


Thanks for the link. I've taken a look and the samples look like they will work for me.

No comments:

Post a Comment