In the previous article, we talked about how to use the Ado.net data Service client library to easily access to the presence of the server, but it's hard to accept that the data still needs to be stitched up with HTML, so we're here now. See how to use asp.net AJAX template to solve this problem. The example code used in the article is at the end of the article, refer to the aspnetajaxtemplatedemo.aspx inside.
Sys.UI.DataView
In order to solve the problem of display data, we need to use a new client control, that is Sys.UI.DataView, referred to as DataView. We'll use DataView to replace the hand-stitched HTML part of the previous article, which is used to iterate over the LI element in UL, so it seems to use DataView as a repeater. In fact, the DataView function is similar to ListView plus DetailsView.
If you bind an array to DataView, it will appear as a ListView. Similar to ListView's LayoutTemplate, it also defines the overall layout of the control's display and is only a small part of the iteration output. For example, write a table with THEAD, and only the TR after the thead of the iteration output. In this respect, DataView and ListView are in complete agreement. The only difference is that the client has not datapager such a control for the time being, so DataView must fully display the entire array's data at once.
If you bind a single object to a DataView, it will appear as a DetailsView. This allows you to use the two DetailsView to make the classic master-details display mode, and on the server side, respectively, with ListView and DetailsView. Of course, you can't expect DataView to be able to automatically analyze each data item and map out the corresponding HTML template as DetailsView does, so HTML templates have to be written by yourself, but at least it's written in HTML, and you can enjoy the IDE when you write. Benefits, it is easier to understand the HTML that you (or others) have written down when maintaining it.
JavaScript syntax
Next, we are going to put DataView into practical application. First, let's talk about how to instantiate a DataView with JavaScript. People who have the experience of writing ASP.net AJAX client code should not be unfamiliar with $create, because DataView inherits from Sys.UI.Control, we can still instantiate it with $create. Before we do that, however, we have to write the corresponding HTML:
<ul id="itemTemplate" class="sysTemplate">
<li>
<span class="award">{{ Award }}</span>
<span class="winner">{{ Winner }}</span>
<span class="film">{{ Film }}</span>
</li>
</ul>
Then we can create the control based on the HTML element of ItemTemplate:
$create(Sys.UI.DataView, {
dataSource: new Sys.Data.AdoDataSource(),
serviceUri: "WebDataService.svc",
query: "OscarWinners"
}, {}, {}, $get("itemTemplate"));
Now, the results of the page display are exactly the same as the previous version of our manual HTML, but we are no longer needing to maintain HTML code embedded in JavaScript.