9.2. Creating a Custom Data SourceIf you want the full power of data access at your disposal and do not want to stick to the data structure provided by the data source, you can implement your data source by yourself, as a server-side ASP.NET class. Since Atlas relies heavily on web services, you have to implement a DataService class. The associated class is implemented in the Microsoft.Web.Services namespace. Within the DataService class, you have to implement the default methods for a data object: they are listed in the enumeration System.ComponentModel.DataObjectMethodType and include the following:
9.1.3. Displaying Data from a Custom Data SourceFor demonstration purposes, we will first implement a web service SELECT method, which retrieves data fromyou guessed itthe Purchasing.Vendors table in the AdventureWorks database. You can, as before, implement a method that returns the desired data. By using the [DataObjectMethod(DataObjectMethodType.Select)] attribute, you declare the specific method as the "select" method. The actual naming of the method is arbitrary. As data type, you can again return a custom type, as shown in Example 9-5. Example 9-5. Returning a custom type
As an alternative, you can also return a DataTable, which requires less code, as shown in Example 9-6. Example 9-6. Returning a DataTable
These .asmx files, so far, do not contain something labeled with [WebMethod]. However, when you call one of these web services in the browser directly, you see that they have two of them: Getdata() and SaveData(). Both expect a parameters array for fine-tuning. Atlas offers automated support for this, so you are just calling the methods under the fixed DataObjectMethodType names: Delete, Insert, Select, and Update. The results are shown in Figure 9-3. Figure 9-3. The methods provided by the base class
On the ASP.NET side of things, two items are required: HTML markup to define the output template and xml-script markup to do the data binding. The former is the same as before: an HTML table. Remember to use <thead> and <tbody>, to satisfy Internet Explorer. The following HTML markup serves as the placeholder to which Atlas binds the data from the custom data source: <div > vendor list goes here</div> <div style="display: none;"> <div > <table> <thead> <tr><th>Account Number</th><th>Name</th></tr> </thead> <tbody> <tr > <td >vendor account number goes here</td> <td >vendor name goes here</td> </tr> </tbody> </table> </div> </div> The xml-script part however requires some changes from the preceding example. It starts off as usual: <script type="text/xml-script"> <page xmlns="http://schemas.microsoft.com/xml-script/2005"> <components> ... </components> </page> </script> Then, the data source needs to be referenced. Since this is no ordinary web service, the ScriptManager object will not work to reference the web service. Instead, the <dataSource> xml-script element comes into play. Provide the URL and an IDyou will need the latter later on! <script type="text/xml-script"> <page xmlns="http://schemas.microsoft.com/xml-script/2005"> <components> <dataSource serviceURL="ListViewVendorsDataService.asmx" /> ... </components> </page> </script> Next up is the ListView control; therefore, the <listView> element enters the stage. The most important step is to bind the data source from the preceding code snippet to the ListView control. The properties dataPath and property must be set to data, and dataContext must reference the ID of the <dataSource> element: <listView itemTemplateParentElement targetElement="output"> <bindings> <binding dataContext="vendorSource" dataPath="data" property="data" /> </bindings> ... </listView> The <layoutTemplate> and <itemTemplate> elements are the same as before, binding the data to the <table> element and its subelements. One thing is missing, however. The data is bound, but has not been loaded yet. The data source supports the property autoLoad. If set to "TRue", this automatically calls the Select method of the data source. See Example 9-7for the complete code for this task. Example 9-7. Displaying data from a custom data source
So there is no actual coding (except of the DataService web service) involved, just declarations. The output shows the first 10 elements in the Purchasing.Vendors table, formatted in an HTML <table> element. Therefore, the output of this script is identical to the one in Figure 9-2.
|