by luisabreu via LA.NET [EN] on 10/28/2009 3:38:15 PM
In the previous posts, we’ve seen how to configure the DataView control so that it gets the data from a remote web service. In this post, we’ll see how we can take advantage of the fetchParameters to pass info to the web service. The idea is simple: we’ll update the previous sample so that it renders a table with clickable headers used for sorting the displayed data. Sorting the data will be done on the server. This means we need to start updating the server side code so that the web service method gets the info used for sorting (notice I’m only showing the changes made to the initial code):
public class PeopleService { static List<Person> _people = new List<Person> { new Person{ Name = "luis", Address = "fx"}, new Person{ Name = "john", Address = "lx"}, new Person{ Name = "peter", Address = "pt"} }; [OperationContract] [WebGet] public IEnumerable<Person> GetPeople(SortOrder sort) { var ordered = _people.OrderBy( p => (sort == SortOrder.Name ? p.Name : p.Address) ); return ordered; } } public enum SortOrder { Name, Address }
As you can see, changes are really minimal: we’ve added an enumeration used for indicating the sort order that should be applied to the collection and we’ve updated the GetPeople method so that it orders the collection accordingly. Now, let’s see the client code:
<head runat="server"> <title></title> <style type="text/css"> .sys-template{ display: none; } </style> <script src="Scripts/MicrosoftAjax/start.debug.js" type="text/javascript"> </script> <script type="text/javascript"> Sys.require([Sys.scripts.jQuery, Sys.scripts.WebServices, Sys.components.dataView], function () { Sys.loadScripts( ["PeopleService.svc/jsdebug"], function () { Sys.Application.activateElement( Sys.get("#view")); $("thead td").click(function () { var view = Sys.get("$view"); view.get_fetchParameters() .sort = ($(this).text() === "Name" ? SortOrder.Name : SortOrder.Address); view.fetchData(); }) }); }); </script> </head> <body xmlns:sys="BLOCKED SCRIPTSys" xmlns:dv="BLOCKED SCRIPTSys.UI.DataView"> <table> <thead> <tr> <td>Name</td> <td>Address</td> </tr> </thead> <tbody id="view" class="sys-template" sys:attach="dv" dv:autofetch="true" dv:httpverb="GET" dv:dataprovider="{{ PeopleService }}" dv:fetchoperation="GetPeople" dv:fetchparameters="{{ { sort: SortOrder.Name } }}"> <tr> <td>{{name}}</td> <td>{{address}}</td> </tr> </tbody> </table> </body>
Most of the code is similar to what we had before. There are, however, some differences:
And there you go: quick and easy, right? Stay tuned for more on MS AJAX.
Original Post: Using the fetchParameters property to pass values to the web service method
The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.