To see how the new functionality stacks up, I decided to recreate my recent jTemplates example, using only ASP.NET AJAX and its new templating features. Eventually, I settled on using the DataView class, which offers more advanced, repeater-like functionality.
Having successfully completed the exercise, I thought it seemed like something that you might find interesting too. The solution boils down to four easy steps:
- Creating a page method to return JSON data.
- Setting up a ScriptManager to coordinate script and page method access.
- Defining the client-side template that will render the JSON data.
- Using JavaScript to render the template, using the page method’s return.
A familiar page method
The first order of business is obtaining a data source to render. To keep things simple, let’s reuse the RSS reader from my jQuery “repeater” example:[WebMethod] public static IEnumerable GetFeedburnerItems() { XDocument feedXML = XDocument.Load("http://feeds.encosia.com/Encosia"); var feeds = from feed in feedXML.Descendants("item") select new { Date = DateTime.Parse(feed.Element("pubDate").Value) .ToShortDateString(), Title = feed.Element("title").Value, Link = feed.Element("link").Value, Description = feed.Element("description").Value }; return feeds; }
Setting up the ScriptManager
The ASP.NET AJAX 4.0 templating features depend on functionality in the ASP.NET AJAX client side framework. We could include MicrosoftAjax.js manually, but since we’re also using a page method, the ScriptManager will serve our needs well:<asp:ScriptManager runat="server" EnablePageMethods="true"> <Scripts> <asp:ScriptReference Path="~/MicrosoftAjaxTemplates.js" /> <asp:ScriptReference Path="~/default.js" /> </Scripts> </asp:ScriptManager>
Creating the template
Next, we need to define a template to render the page method’s return upon. The ASP.NET AJAX 4.0 templating engine’s syntax is very straightforward:<table> <thead> <tr> <th>Date</th> <th>Title</th> <th>Excerpt</th> </tr> </thead> <tbody id="RSSItem" class="sys-template"> <tr> <td>{{ Date }}</td> <td><a href="{{ Link }}">{{ Title }}</a></td> <td>{{ Description }}</td> </tr> </tbody> </table>
You may have noticed the sys-template CSS class applied to the meat of the template. This is a convention for hiding the template until it has been rendered. The class should be defined somewhere as:
.sys-template { display: none; visibility: hidden; }
Bringing it all together
Now that we’ve got our JSON data source and have defined our template, the final step is simply to connect those dots.Sys.Application.add_init(AppInit); // Execute the page method when the page finishes loading. function AppInit(sender, args) { PageMethods.GetFeedburnerItems(OnSuccess, OnFailure); } function OnSuccess(result) { // Create an ASP.NET AJAX 4.0 DataView, targeted at our template. var dv = new Sys.Preview.UI.DataView($get('RSSItem')); // Pass the DataView our JSON result of RSS items to render. dv.set_data(result); // Render the DataView template, using the provided JSON array. dv.render(); } // Do not do this in production code! function OnFailure() { }
- A DataView object is created for our template, by passing its containing DomElement: $get(‘RSSItem’).
- The JSON result is assigned to the DataView, using its set_data method.
- The DataView’s render method is called to generate the end result.
Conclusion and a couple suggestions.
Comparing this implementation to my identical jTemplates example, I must say that I prefer the DataView solution. The syntax is impressively cleaner.I just want to reiterate how happy I am with the transparency of the ASP.NET team lately. For those of us who aren’t MVPs or ASPInsiders, it’s nice to have a chance to offer constructive feedback and generally not be left in the dark.
In that spirit, I have two suggestions to improve the templating engine.
External templates. I don’t particularly enjoy mingling my template with the rest of the page’s HTML. This is one thing that jTemplates handles very well, with its createTemplateURL method. I would love to see this functionality in the ASP.NET AJAX templating engine and/or the DataView class.
To one-up jTemplates, it would be fantastic if there were a way to pre-load the template. During the second or two that the AJAX call executes would be an excellent time to get the template ready, so that there’s no HTTP delay after the data is ready.
Table issues. Ideally, there should be an automatic convention for hiding the entire table until the template is rendered. Maybe that’s already possible, but I wasn’t able to find a configuration that would do that while repeating only the tbody.
It would make sense if we could assign the entire table a CSS class of sys-template, which would be removed if any of its children were a template being rendered. I usually avoid special cases like this, but I expect that the table scenario will be a prevalent one. It makes sense to optimize for it.