| By Dave Ward | Article Rating: |
|
| August 21, 2008 01:00 AM EDT | Reads: |
2,948 |
Dave Ward's "Encosia" Blog
Hot on the heels of the recent ASP.NET AJAX roadmap, Bertrand and team have released a limited preview of the new AJAX functionality coming in ASP.NET 4.0.
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;
}
This page method will return a JSON array representing the latest ten items from my RSS feed. Specifically, their Date, Title, Link, and Description.
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>
EnablePageMethods will generate a JavaScript proxy for calling our page method. This is not necessarily the only or most efficient way of calling page methods, but works well enough for our purposes here.
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>
The bracketed field names correspond to the names of the properties in the anonymous type generated by the page method. It couldn’t be much easier.
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; }
During the rendering process, the templating engine will automatically attempt to remove the sys-template class, making the final result visible.
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() { }
The OnSuccess function is where all of the magic happens. When the page method completes its execution, three things happen:
- 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.
Try it for yourself
Published August 21, 2008 Reads 2,948
Copyright © 2008 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
About Dave Ward
Dave Ward wrote his first computer program in 1981, using good ‘ol Microsoft Color BASIC and cassette tapes for data storage. Over the years since then, he has had the opportunity to work on projects ranging from simple DOS applications to global telecommunications networks spanning multiple platforms.
- AJAX World RIA Conference & Expo Kicks Off in New York City
- An A to Z of Cloud Computing Companies in 2009
- What is Web 3.0?
- Java Kicks Ruby on Rails in the Butt
- Bordeaux in Your Glass
- Ulitzer’s Amazing First 30 Days in Public Beta
- 1st Annual Government IT Expo: Call for Papers Deadline July 15
- BEA Updates WebLogic SOA Portal for Web 2.0 Era
- RIAs for Web 3.0 Using the Microsoft Platform
- Will Ulitzer Dominate News Content on The Web? -Gartner
- AJAX World RIA Conference & Expo Kicks Off in New York City
- Ulitzer to Give Drupal 6.0 Its Biggest Scalability Challenge Yet
- An A to Z of Cloud Computing Companies in 2009
- What is Web 3.0?
- Java Kicks Ruby on Rails in the Butt
- Bordeaux in Your Glass
- Web 2.0 Is Dead. Long Live Web 2.0!
- Web Apps Will Be Built in the Cloud: Keynote Systems Exec
- Ulitzer’s Amazing First 30 Days in Public Beta
- Is Web 2.0 Possible with Existing Open Source Technologies?
- Who Are The All-Time Heroes of i-Technology?
- AJAX World RIA Conference & Expo Kicks Off in New York City
- Personal Branding Checklist
- i-Technology Viewpoint: Attack of the Blogs
- Web 2.0 News and Wrapping Up "Real-World AJAX" Seminar
- AJAX World RIA Interview: Appcelerator Building Out the RIA Open Source Community
- Coach Wei's "Direct From Web 2.0" Blog: The Converging Developer Community
- i-Technology Viewpoint: It's Time to Take the Quotation Marks Off "Web 2.0"
- SOA 2 Point Oh No!
- Sixteen Ways of Thinking in Web 2.0







































