<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Tales from the Evil Empire : Microsoft AJAX Library</title><link>http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx</link><description>Tags: Microsoft AJAX Library</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>JavaScript class browser: once again with jQuery</title><link>http://weblogs.asp.net/bleroy/archive/2009/10/30/javascript-class-browser-once-again-with-jquery.aspx</link><pubDate>Fri, 30 Oct 2009 17:43:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7243389</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7243389</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/10/30/javascript-class-browser-once-again-with-jquery.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/bleroy/MercuryWeb_1CBF87AD.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 10px 0px; display: inline; border-top: 0px; border-right: 0px" title="(c) 2004 Bertrand Le Roy" border="0" alt="(c) 2004 Bertrand Le Roy" align="left" src="http://weblogs.asp.net/blogs/bleroy/MercuryWeb_thumb_1373269F.jpg" width="184" height="244" /&gt;&lt;/a&gt; I’ve already posted twice about that little class browser application. The first iteration was mostly declarative and can be found here:    &lt;br /&gt;&lt;a title="http://weblogs.asp.net/bleroy/archive/2009/09/14/building-a-class-browser-with-microsoft-ajax-4-0-preview-5.aspx" href="http://weblogs.asp.net/bleroy/archive/2009/09/14/building-a-class-browser-with-microsoft-ajax-4-0-preview-5.aspx"&gt;http://weblogs.asp.net/bleroy/archive/2009/09/14/building-a-class-browser-with-microsoft-ajax-4-0-preview-5.aspx&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The second one was entirely imperative and can be found here:   &lt;br /&gt;&lt;a title="http://weblogs.asp.net/bleroy/archive/2009/10/15/entirely-unobtrusive-and-imperative-templates-with-microsoft-ajax-4-preview-6.aspx" href="http://weblogs.asp.net/bleroy/archive/2009/10/15/entirely-unobtrusive-and-imperative-templates-with-microsoft-ajax-4-preview-6.aspx"&gt;http://weblogs.asp.net/bleroy/archive/2009/10/15/entirely-unobtrusive-and-imperative-templates-with-microsoft-ajax-4-preview-6.aspx&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;This new version builds on top of the code for the imperative version and adds the jQuery dependency in an attempt to make the code leaner and simpler. I invite you to refer to the imperative code (included in the &lt;a href="http://weblogs.asp.net/blogs/bleroy/Samples/ClassBrowserWithjQuery.zip"&gt;archive for this post&lt;/a&gt;) and compare it with the jQuery version, which shows a couple of ways the Microsoft Ajax Library lights up when jQuery is present.&lt;/p&gt;  &lt;p&gt;The first thing I want to do here is convert the plain function I was using to build the browser’s namespace and class tree into a jQuery plug-in:&lt;/p&gt;  &lt;pre class="code"&gt;$.fn.classBrowserTreeView = &lt;span style="color: blue"&gt;function &lt;/span&gt;(options) {
  &lt;span style="color: blue"&gt;var &lt;/span&gt;opts = $.extend({},&lt;br /&gt;    $.fn.classBrowserTreeView.defaults,&lt;br /&gt;    options);
  &lt;span style="color: blue"&gt;return this&lt;/span&gt;;
};&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;That plug-in will have two options: the data to render (which will default to the root namespaces in the Microsoft Ajax Library), and the node template selector (which will default to “#nodeTemplate”):&lt;/p&gt;

&lt;pre class="code"&gt;$.fn.classBrowserTreeView.defaults = {
  data: Type.getRootNamespaces(),
  nodeTemplate: &lt;span style="color: maroon"&gt;&amp;quot;#nodeTemplate&amp;quot;
&lt;/span&gt;};&lt;/pre&gt;

&lt;p&gt;For the moment, as you can see, this plug-in does nothing. We want it to create a DataView control on each of the elements of the current wrapped set. We will do this by calling into the dataView plug-in.&lt;/p&gt;

&lt;p&gt;You may be wondering where this plug-in might come from. Well, that’s the first kind of lighting up that the Microsoft Ajax Library’s script loader (start.js) will do in the presence of jQuery: every control and behavior will get surfaced as a jQuery plug-in, and components will get added as methods on the jQuery object. This is similar to what I had shown a while ago in this post, only much easier:
  &lt;br /&gt;&lt;a title="http://weblogs.asp.net/bleroy/archive/2009/05/04/creating-jquery-plug-ins-from-microsoftajax-components.aspx" href="http://weblogs.asp.net/bleroy/archive/2009/05/04/creating-jquery-plug-ins-from-microsoftajax-components.aspx"&gt;http://weblogs.asp.net/bleroy/archive/2009/05/04/creating-jquery-plug-ins-from-microsoftajax-components.aspx&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For example, we can write this in our own plug-in to create DataView components over the current jQuery wrapped set:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;return this&lt;/span&gt;.dataView({
  data: opts.data,
  itemTemplate: opts.nodeTemplate,
});&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Now we can wire up the itemRendered event of the data view and start enriching the markup that the DataView control rendered for each data item. First, let’s get hold of the nodes in the rendered template and wrap them:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;elt = $(args.nodes);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Then, if the current node is representing a namespace, we want to hook up the expansion button’s click event so that it toggles visibility of the list of children, and we want to “unhide” the button itself (it has a “hidden” class in the default markup):&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;if &lt;/span&gt;(Type.isNamespace(args.dataItem)) {
  elt.find(&lt;span style="color: maroon"&gt;&amp;quot;.toggleButton&amp;quot;&lt;/span&gt;).click(&lt;span style="color: blue"&gt;function &lt;/span&gt;(e) {
    e.preventDefault();
    &lt;span style="color: blue"&gt;return &lt;/span&gt;toggleVisibility(&lt;span style="color: blue"&gt;this&lt;/span&gt;);
  }).removeClass(&lt;span style="color: maroon"&gt;&amp;quot;hidden&amp;quot;&lt;/span&gt;);
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;You can see here that we’re taking advantage of chaining.&lt;/p&gt;

&lt;p&gt;Next thing is to set-up the node link itself. We start by inhibiting the link’s default action. Then we set the text for the link, and finally we set the command that will bubble up to the DataView when the link gets clicked:&lt;/p&gt;

&lt;pre class="code"&gt;elt.find(&lt;span style="color: maroon"&gt;&amp;quot;.treeNode&amp;quot;&lt;/span&gt;).click(&lt;br /&gt;  &lt;span style="color: blue"&gt;function &lt;/span&gt;(e) {&lt;br /&gt;    e.preventDefault();&lt;br /&gt;    &lt;span style="color: blue"&gt;return false&lt;/span&gt;;&lt;br /&gt;  })
  .text(getSimpleName(args.dataItem.getName()))
  .setCommand(&lt;span style="color: maroon"&gt;&amp;quot;select&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Here, I’m using a small plug-in to set the command:&lt;/p&gt;

&lt;pre class="code"&gt;$.fn.setCommand = &lt;span style="color: blue"&gt;function &lt;/span&gt;(options) {
  &lt;span style="color: blue"&gt;var &lt;/span&gt;opts = $.extend({},&lt;br /&gt;    $.fn.setCommand.defaults, options);
  &lt;span style="color: blue"&gt;return &lt;/span&gt;$(&lt;span style="color: blue"&gt;this&lt;/span&gt;).each(&lt;span style="color: blue"&gt;function &lt;/span&gt;() {
    $.setCommand(&lt;span style="color: blue"&gt;this&lt;/span&gt;,&lt;br /&gt;      opts.commandName,&lt;br /&gt;      opts.commandArgument,&lt;br /&gt;      opts.commandTarget);
  });
}
$.fn.setCommand.defaults = {
    commandName: &lt;span style="color: maroon"&gt;&amp;quot;select&amp;quot;&lt;/span&gt;,
    commandArgument: &lt;span style="color: blue"&gt;null&lt;/span&gt;,
    commandTarget: &lt;span style="color: blue"&gt;null
&lt;/span&gt;};&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;I’m using $.setCommand here, which does get created by the framework for me, but I still need to create that small plug-in to make it work on a wrapped set instead of a static method off jQuery. I’ve sent feedback to the team that setCommand and bind should get created as plug-ins by the framework and hopefully it will happen in a future version.&lt;/p&gt;

&lt;p&gt;The last thing we need to do here is to recursively create the child branches of our tree:&lt;/p&gt;

&lt;pre class="code"&gt;elt.find(&lt;span style="color: maroon"&gt;&amp;quot;ul&amp;quot;&lt;/span&gt;).classBrowserTreeView({
    data: getChildren(args.dataItem)
});&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This just finds the child UL element of the current branch and calls our plug-in on the results with the children namespaces and classes as the data.&lt;/p&gt;

&lt;p&gt;And this is it for the tree, we can now create it with this simple call:&lt;/p&gt;

&lt;pre class="code"&gt;$(&lt;span style="color: maroon"&gt;&amp;quot;#tree&amp;quot;&lt;/span&gt;).classBrowserTreeView();&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The details view rendering will only differ in minor ways from the code we had in our previous imperative version. The only differences is the use of jQuery to traverse and manipulate the DOM instead of the mix of native DOM APIs and Sys.get that we were using before. For example,&lt;/p&gt;

&lt;pre class="code"&gt;args.get(&lt;span style="color: maroon"&gt;&amp;quot;li&amp;quot;&lt;/span&gt;).innerHTML =&lt;br /&gt;  args.dataItem.getName ?&lt;br /&gt;    args.dataItem.getName() :&lt;br /&gt;    args.dataItem.name;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;becomes:&lt;/p&gt;

&lt;pre class="code"&gt;$(args.nodes).filter("li").text(&lt;br /&gt;  args.dataItem.getName ?&lt;br /&gt;    args.dataItem.getName() :&lt;br /&gt;    args.dataItem.name);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Notice how jQuery’s text method makes things a little more secure than the innerHTML we had used before.&lt;/p&gt;

&lt;p&gt;Updating the details view with the data for the item selected in the tree is done by handling the select command of the tree from the following function:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;function &lt;/span&gt;onCommand(sender, args) {
  &lt;span style="color: blue"&gt;if &lt;/span&gt;(args.get_commandName() === &lt;span style="color: maroon"&gt;&amp;quot;select&amp;quot;&lt;/span&gt;) {
    &lt;span style="color: blue"&gt;var &lt;/span&gt;dataItem = sender.findContext(&lt;br /&gt;      args.get_commandSource()).dataItem;
    &lt;span style="color: blue"&gt;var &lt;/span&gt;isClass = Type.isClass(dataItem) &amp;amp;&amp;amp;&lt;br /&gt;                 !Type.isNamespace(dataItem);
    &lt;span style="color: blue"&gt;var &lt;/span&gt;childData =&lt;br /&gt;      (isClass ? getMembers : getChildren)(dataItem);
    &lt;span style="color: blue"&gt;var &lt;/span&gt;detailsChild =&lt;br /&gt;      Sys.Application.findComponent(&lt;span style="color: maroon"&gt;&amp;quot;detailsChild&amp;quot;&lt;/span&gt;);
    detailsChild.onItemRendering =&lt;br /&gt;      isClass ?&lt;br /&gt;        onClassMemberRendering :&lt;br /&gt;        onNamespaceChildRendering;
    detailsChild.onItemRendered =&lt;br /&gt;      onDetailsChildRendered;
    detailsChild.set_data(childData);
    $(&lt;span style="color: maroon"&gt;&amp;quot;#detailsTitle&amp;quot;&lt;/span&gt;).text(dataItem.getName());
    $(&lt;span style="color: maroon"&gt;&amp;quot;.namespace&amp;quot;&lt;/span&gt;).css(&lt;br /&gt;      &lt;span style="color: maroon"&gt;&amp;quot;display&amp;quot;&lt;/span&gt;,&lt;br /&gt;      isClass ? &lt;span style="color: maroon"&gt;&amp;quot;none&amp;quot; &lt;/span&gt;: &lt;span style="color: maroon"&gt;&amp;quot;block&amp;quot;&lt;/span&gt;);
    $(&lt;span style="color: maroon"&gt;&amp;quot;.class&amp;quot;&lt;/span&gt;).css(&lt;br /&gt;      &lt;span style="color: maroon"&gt;&amp;quot;display&amp;quot;&lt;/span&gt;,&lt;br /&gt;      isClass ? &lt;span style="color: maroon"&gt;&amp;quot;block&amp;quot; &lt;/span&gt;: &lt;span style="color: maroon"&gt;&amp;quot;none&amp;quot;&lt;/span&gt;);
    $(&lt;span style="color: maroon"&gt;&amp;quot;#details&amp;quot;&lt;/span&gt;).css(&lt;span style="color: maroon"&gt;&amp;quot;display&amp;quot;&lt;/span&gt;, &lt;span style="color: maroon"&gt;&amp;quot;block&amp;quot;&lt;/span&gt;);
  }
}&lt;/pre&gt;

&lt;p&gt;Not much change here from the previous version, again, except for the use of jQuery and some chaining.&lt;/p&gt;

&lt;p&gt;And that is pretty much it. I’ve made other changes in the script to make use of the new script loader in the Microsoft Ajax Library but that will be the subject of a future post.&lt;/p&gt;

&lt;p&gt;Hopefully, this has shown you how the Microsoft Ajax Library can light up with jQuery. The automatic creation of plug-ins feels very much like native jQuery plug-ins and brings all the power of client templates to jQuery. Once we have bind and setCommand plug-ins as well, the Microsoft Ajax Library may become a very useful tool to jQuery programmers just as much as jQuery itself is a very useful tool to Microsoft Ajax programmers.&lt;/p&gt;

&lt;p&gt;The code can be found here:
  &lt;br /&gt;&lt;a title="http://weblogs.asp.net/blogs/bleroy/Samples/ClassBrowserWithjQueryUpdated.zip" href="http://weblogs.asp.net/blogs/bleroy/Samples/ClassBrowserWithjQueryUpdated.zip"&gt;http://weblogs.asp.net/blogs/bleroy/Samples/ClassBrowserWithjQueryUpdated.zip&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Update:&lt;/b&gt; fixed a problem in Firefox &amp; Chrome.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7243389" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/HTML/default.aspx">HTML</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/jQuery/default.aspx">jQuery</category></item><item><title>How to render the same template on the server and client with minimal redundancy</title><link>http://weblogs.asp.net/bleroy/archive/2009/10/19/how-to-render-the-same-template-on-the-server-and-client-with-minimal-redundancy.aspx</link><pubDate>Mon, 19 Oct 2009 08:50:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7232857</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>14</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7232857</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/10/19/how-to-render-the-same-template-on-the-server-and-client-with-minimal-redundancy.aspx#comments</comments><description>&lt;h4&gt;&lt;img style="border-right-width: 0px; margin: 0px 0px 10px 10px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="(c) 2005 Bertrand Le Roy" border="0" alt="(c) 2005 Bertrand Le Roy" align="right" src="http://weblogs.asp.net/blogs/bleroy/buildingreflection_6007098A.jpg" width="180" height="260" /&gt; &lt;/h4&gt;  &lt;p&gt;Last week, I wrote &lt;a href="http://weblogs.asp.net/bleroy/archive/2009/10/15/entirely-unobtrusive-and-imperative-templates-with-microsoft-ajax-4-preview-6.aspx"&gt;a post about how the new Microsoft Ajax Library Preview 6 made it a lot easier to write unobtrusive and imperative data-driven applications&lt;/a&gt;. Because for the previous preview, I had written a cool little class browser using a declarative style, I thought it would be nice to rewrite this in a completely imperative way. The mistake I made though was to call it unobtrusive. Never mind that ‘unobtrusive’ is a perfectly well-defined word that actually existed way before JavaScript. ‘Unobtrusive JavaScript’ has a very specific meaning that people feel strongly about. To be worthy of that label, an application must basically conform to (at least) those two requirements:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;strong&gt;Markup and behavior are strictly separated.&lt;/strong&gt; That means no DOM-0 event handlers, no custom attributes or tags, and even no microformats imo. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Graceful degradation / progressive enhancement.&lt;/strong&gt; This means that the application’s script is only used to improve what would work without script, in other words that the application is entirely usable without script. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;While my little sample strictly conformed to (1), it didn’t conform to (2). Not because I’m too dumb or ignorant, mind you, but because for this application, it didn’t make any sense at all: the application’s whole point is to display client-side script objects. The server does not have the first clue about the data that needs to be rendered. Which makes it impossible for anything to render without script. Which in turn triggered some unpleasant comments on the post: this was not really unobtrusive JavaScript in the full sense of the term. Not the library’s fault though, just my own for using the wrong example.&lt;/p&gt;  &lt;h4&gt;The right example&lt;/h4&gt;  &lt;p&gt;So I thought the best thing to fix this is to provide a more relevant example, one where the server could actually be a fallback scenario.&lt;/p&gt;  &lt;p&gt;The new sample code is a fairly simple master-details view on a silly data set: jedi data. We have a WCF web service that is returning a list of jedis and their associated data, which the application can render on the server-side or on the client-side.&lt;img style="border-bottom: 0px; border-left: 0px; margin: 5px auto; display: block; float: none; border-top: 0px; border-right: 0px" title="Jedis" border="0" alt="Jedis" src="http://weblogs.asp.net/blogs/bleroy/Jedis_0779A810.png" width="413" height="176" /&gt; Once you&amp;#160; have data that is available from both the server and client sides, your best and simplest tool to achieve progressive enhancement is the plain &amp;lt;a&amp;gt; tag. You can build the links in your application so that without script, they do something meaningful:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;a &lt;/span&gt;&lt;span style="color: red"&gt;href&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;?jedi=all&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;expandButton&amp;quot;&amp;gt;&lt;/span&gt;+&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;a&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;What you see above is the + link on the top-left of the screen that will expand the list of jedis. To make it work client-side instead of the server-side, you use script to add a click handler that suppresses the default behavior of the link and replaces it with the equivalent client-side action:&lt;/p&gt;

&lt;pre class="code"&gt;Sys.UI.DomEvent.addHandler(&lt;br /&gt;  Sys.get(&lt;span style="color: maroon"&gt;&amp;quot;#expandButton&amp;quot;&lt;/span&gt;), &lt;span style="color: maroon"&gt;&amp;quot;click&amp;quot;&lt;/span&gt;, &lt;span style="color: blue"&gt;function&lt;/span&gt;(e) {
    e.preventDefault();
    jediList.fetchData();
    &lt;span style="color: blue"&gt;return false&lt;/span&gt;;
  });&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;That’s fairly easy and has been possible since, well, I’m not sure but it’s been a looong time.&lt;/p&gt;

&lt;p&gt;Now there’s the templated rendering. Rendering the same thing from the server and the client without repeating oneself too much is not as simple. The key to it is to render the server template with an empty data item and then to use the result of that as the client template.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;ul &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;jediList&amp;quot;&lt;br /&gt;&lt;/span&gt;&lt;span style="background: yellow"&gt;&amp;lt;%&lt;/span&gt;if (!isDetails) { &lt;span style="background: yellow"&gt;%&amp;gt;&lt;/span&gt; &lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;sys-template&amp;quot;&lt;/span&gt;&lt;span style="background: yellow"&gt;&amp;lt;%&lt;/span&gt; } &lt;span style="background: yellow"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;span style="background: yellow"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;jedis = !isDetails ?
    &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Jedi&lt;/span&gt;&amp;gt;() {&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Jedi&lt;/span&gt;()} :
    &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;JediService&lt;/span&gt;().GetJedis();
  &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;jedi &lt;span style="color: blue"&gt;in &lt;/span&gt;jedis) { &lt;span style="background: yellow"&gt;%&amp;gt;
&lt;/span&gt;    &lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;li&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br /&gt;      &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;a &lt;/span&gt;&lt;span style="color: red"&gt;href&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;?&lt;span style="color: red"&gt;jedi&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&lt;span style="background: yellow"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color: blue"&gt;= &lt;/span&gt;jedi.Name &lt;span style="background: yellow"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;quot;&amp;gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="background: yellow"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color: blue"&gt;= &lt;/span&gt;jedi.Name&lt;span style="background: yellow"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color: blue"&gt;&lt;br /&gt;      &amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;a&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br /&gt;    &amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;li&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;span style="background: yellow"&gt;&amp;lt;%&lt;/span&gt;} &lt;span style="background: yellow"&gt;%&amp;gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;ul&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This way, there is only one template, only one version of the markup, that we are using on both the server and client sides. When rendered from the server with the actual data set, we get the list right away, and the browser just displays it (and search engines can see the list as well, something you can’t achieve with pure script). When rendered with the dummy dataset, we get the following:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;ul &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;jediList&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;sys-template&amp;quot;&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;  &lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;li&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;a &lt;/span&gt;&lt;span style="color: red"&gt;href&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;?jedi&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&lt;span style="color: blue"&gt;all&amp;quot;&amp;gt;all&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;a&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;li&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;ul&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This markup can be used as a template on the client-side by this code:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;jediList = Sys.create.dataView(&lt;span style="color: maroon"&gt;&amp;quot;#jediList&amp;quot;&lt;/span&gt;, {
  dataProvider: &lt;span style="color: maroon"&gt;&amp;quot;JediService.svc&amp;quot;&lt;/span&gt;,
  fetchOperation: &lt;span style="color: maroon"&gt;&amp;quot;GetJedis&amp;quot;&lt;/span&gt;,
  autoFetch: &lt;span style="color: blue"&gt;false&lt;/span&gt;,
  itemRendered: &lt;span style="color: blue"&gt;function&lt;/span&gt;(sender, args) {
    &lt;span style="color: blue"&gt;var &lt;/span&gt;link = args.get(&lt;span style="color: maroon"&gt;&amp;quot;a&amp;quot;&lt;/span&gt;);
    &lt;span style="color: blue"&gt;var &lt;/span&gt;dataItem = args.dataItem;
    link.innerHTML = dataItem.Name;
    Sys.UI.DomEvent.addHandler(&lt;br /&gt;      link, &lt;span style="color: maroon"&gt;&amp;quot;click&amp;quot;&lt;/span&gt;, &lt;span style="color: blue"&gt;function&lt;/span&gt;(e) {
        e.preventDefault();
        details.set_data(dataItem);
        &lt;span style="color: blue"&gt;return false&lt;/span&gt;;
      });
  }
});&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The details view is built on pretty much the same principle. The main difference is that it does have additional markup to delimit the fields that we’ll want to set dynamically. Here’s how it renders with the dummy data:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;div &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;details&amp;quot;&lt;/span&gt; &lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;sys-template&amp;quot;&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;span &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;jediName&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;span&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &lt;/span&gt;owns a
    &lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;span &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;jediLightSaber&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;span&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &lt;/span&gt;lightsaber and is on the
    &lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;span &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;jediSide&amp;quot;&amp;gt;&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;span&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &lt;/span&gt;side.
&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;div&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;All the itemRendered handler has to do then is fill in the blanks:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;details = Sys.create.dataView(&lt;span style="color: maroon"&gt;&amp;quot;#details&amp;quot;&lt;/span&gt;, {
  itemRendered: &lt;span style="color: blue"&gt;function&lt;/span&gt;(sender, args) {
    args.get(&lt;span style="color: maroon"&gt;&amp;quot;#jediName&amp;quot;&lt;/span&gt;).innerHTML =&lt;br /&gt;      args.dataItem.Name;
    args.get(&lt;span style="color: maroon"&gt;&amp;quot;#jediLightSaber&amp;quot;&lt;/span&gt;).innerHTML =&lt;br /&gt;      args.dataItem.LightsaberColor;
    args.get(&lt;span style="color: maroon"&gt;&amp;quot;#jediSide&amp;quot;&lt;/span&gt;).innerHTML =&lt;br /&gt;      args.dataItem.DarkSide ? &lt;span style="color: maroon"&gt;&amp;quot;dark&amp;quot; &lt;/span&gt;: &lt;span style="color: maroon"&gt;&amp;quot;light&amp;quot;&lt;/span&gt;;
  }
});&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;So what do you think?&lt;/p&gt;

&lt;p&gt;Here’s the code (by the way, I’m using the Microsoft CDN in there): 
  &lt;br /&gt;&lt;a title="http://weblogs.asp.net/blogs/bleroy/Samples/ServerClientTemplates.zip" href="http://weblogs.asp.net/blogs/bleroy/Samples/ServerClientTemplates.zip"&gt;http://weblogs.asp.net/blogs/bleroy/Samples/ServerClientTemplate.zip&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7232857" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/HTML/default.aspx">HTML</category></item><item><title>Entirely unobtrusive and imperative templates with Microsoft Ajax Library Preview 6</title><link>http://weblogs.asp.net/bleroy/archive/2009/10/15/entirely-unobtrusive-and-imperative-templates-with-microsoft-ajax-4-preview-6.aspx</link><pubDate>Thu, 15 Oct 2009 09:12:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7230434</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>12</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7230434</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/10/15/entirely-unobtrusive-and-imperative-templates-with-microsoft-ajax-4-preview-6.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/bleroy/IMG_2305_0DB1EDD9.jpg"&gt;&lt;img style="border-right-width: 0px; margin: 0px 0px 10px 10px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="(c) 2009 Bertrand Le Roy" border="0" alt="(c) 2009 Bertrand Le Roy" align="right" src="http://weblogs.asp.net/blogs/bleroy/IMG_2305_thumb_138C9172.jpg" width="244" height="164" /&gt;&lt;/a&gt; Today is the release of the sixth preview of Microsoft Ajax Library. Don’t get fooled by the somewhat silly and long name: this is a major release in many ways. The scripts have been majorly refactored since preview 5. Check out the other posts out there (links at the bottom of this post) to see just some of the many new features that are in there. Some of my favorite are all the small improvements that have been made to make imperative instantiation of components and templated contents easier than ever. Many of you have told us that you preferred to do things imperatively and this release makes it a lot better.&lt;/p&gt;  &lt;p&gt;When Preview 5 came out, I built a simple class browser using the declarative syntax. The class browser shows the hierarchy of namespaces and classes in a tree view on the left side of the page, and the details of whatever’s selected in the tree on the right side of the page:&lt;a href="http://weblogs.asp.net/blogs/bleroy/ClassBrowser2_36D98617.png"&gt;&lt;img style="border-right-width: 0px; margin: 5px auto; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="The JavaScript class browser" border="0" alt="The JavaScript class browser" src="http://weblogs.asp.net/blogs/bleroy/ClassBrowser2_thumb_23B8596B.png" width="504" height="187" /&gt;&lt;/a&gt;&lt;a href="http://weblogs.asp.net/bleroy/archive/2009/09/14/building-a-class-browser-with-microsoft-ajax-4-0-preview-5.aspx"&gt;http://weblogs.asp.net/bleroy/archive/2009/09/14/building-a-class-browser-with-microsoft-ajax-4-0-preview-5.aspx&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;It still works (and an updated version is attached to this post), but I thought I would demonstrate how you can take that same sample and re-implement it in a completely imperative way. Of course, you never have to go all the way one way or another, and it’s always possible for example to use the nice declarative syntax for bindings but instantiate your components imperatively if you choose to do so. In this post, I’m deliberately going imperative all the way. Just keep in mind this is rather extreme.&lt;/p&gt;  &lt;p&gt;The first thing to notice in the new version is that the markup is perfectly clean and contains no weird extension, namespace or custom binding syntax whatsoever. It’s 100% pure HTML 5. Here is for example the complete markup for the tree view on that page:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;ul &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;tree&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;tree&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;ul&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;

&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;ul &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;nodeTemplate&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;sys-template&amp;quot;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;li&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;a &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;toggleButton&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;href&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;#&amp;quot;&amp;gt;&lt;/span&gt;+&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;a&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;a &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;treeNode&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;href&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;#&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;a&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;ul&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;ul&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;li&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;ul&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The script that builds the dynamic contents is bootstrapped by the following code:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;script &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;text/javascript&amp;quot;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: red"&gt;src&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Scripts/start.js&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;script&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;script &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
&lt;/span&gt;Sys.loadScripts([&amp;quot;Scr&lt;span style="color: maroon"&gt;ipts/Tree.js&amp;quot;], f&lt;/span&gt;unc&lt;span style="color: blue"&gt;tion() {
  &lt;/span&gt;Sys.require([Sys.components.dataView], function&lt;span style="color: blue"&gt;() {
    &lt;/span&gt;createTreeView(&amp;quot;#tree&amp;quot;,&lt;br /&gt;                   Typ&lt;span style="color: maroon"&gt;e.getRo&lt;/span&gt;otNamespaces(),&lt;br /&gt;                   &amp;quot;#nodeTempla&lt;span style="color: maroon"&gt;te&amp;quot;);
    &lt;/span&gt;Sys.create.dataView(&amp;quot;#detailsChild&amp;quot;, &lt;span style="color: maroon"&gt;{
      &lt;/span&gt;itemRendered: onDetailsChildRendered
    });
  });
});
&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;script&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;We’re making use of the new script loader here: we first include the bootstrapper file, start.js, and then we declare that we need one custom script, “tree.js” and everything necessary to instantiate a DataView. The script loader will figure out on its own the set of scripts it needs to download for that. Once those scripts have been downloaded, we call createTreeView, which is custom code that we’ll look at in a moment that creates nested DataView controls over the markup. We also create a second DataView to display the details of what’s selected in the&amp;#160; tree.&lt;/p&gt;

&lt;p&gt;Notice that we set some properties to a selector string here (for example “#nodeTemplate”). This is actually a breaking change from the previous preview, which only understood id strings. Microsoft Ajax does not include a full selector engine but it does understand the most basic of selectors (.class, tagName and #id). But where it gets really interesting is that if you had included jQuery on the page, the framework would detect it and enable you to use full selectors everywhere. Isn’t that sweet?&lt;/p&gt;

&lt;p&gt;So how does the imperative approach compare with the declarative one? Well, for instantiating components, you already have an example above, where we use Sys.create.dataView. But what about wiring up events, setting text contents and attribute values, instantiating components over the markup inside the template?&lt;/p&gt;

&lt;p&gt;All those are done by post-processing the template instances after they’ve been instantiated, by handling the itemRendered event:&lt;/p&gt;

&lt;pre class="code"&gt;itemRendered: &lt;span style="color: blue"&gt;function&lt;/span&gt;(sender, args) {
  &lt;span style="color: #006400"&gt;// do magic
&lt;/span&gt;}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Wiring up events is as simple as getting a reference to an element and calling addHandler:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;toggleButton = args.get(&lt;span style="color: maroon"&gt;&amp;quot;.toggleButton&amp;quot;&lt;/span&gt;);
Sys.UI.DomEvent.addHandler(toggleButton, &lt;span style="color: maroon"&gt;&amp;quot;click&amp;quot;&lt;/span&gt;,&lt;br /&gt;  &lt;span style="color: blue"&gt;function&lt;/span&gt;(e) {
    toggleVisibility(&lt;span style="color: blue"&gt;this&lt;/span&gt;);
  }, &lt;span style="color: blue"&gt;true&lt;/span&gt;);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The args.get function, which you will use a lot, takes a selector and returns the first element that matches it &lt;em&gt;inside the template&lt;/em&gt;. Here, we are looking for an element with class “toggleButton”, but a local id would work just as well.&lt;/p&gt;

&lt;p&gt;To set text contents and attribute values is trivial once you know how to get references to elements from local selectors (remember, jQuery also works here transparently or even explicitly when and if you need it).&lt;/p&gt;

&lt;p&gt;Finally, instantiating components is also quite easy. For example, here is the code that creates an inner DataView for the child nodes of a node in the tree, recursively:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;childView = args.get(&lt;span style="color: maroon"&gt;&amp;quot;ul&amp;quot;&lt;/span&gt;);
createTreeView(childView,&lt;br /&gt;               getChildren(args.dataItem),&lt;br /&gt;               nodeTemplate);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The args.get funtion is used once more to get a reference to the first UL element within the template, and it is then easy to do a recursive call into our tree creation function and build the new branch of the tree.&lt;/p&gt;

&lt;p&gt;The command bubbling feature that makes it so easy to wire up custom commands into a template is still usable in imperative code:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;treeNode = args.get(&lt;span style="color: maroon"&gt;&amp;quot;.treeNode&amp;quot;&lt;/span&gt;);
Sys.setCommand(treeNode, &lt;span style="color: maroon"&gt;&amp;quot;select&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Finally, there is one feature that I’m not using in that sample, but that’s immensely useful, and I’m talking of course of live bindings. Those work too, all you have to do is call the Sys.bind function and give it the target object, the name of the target property to bind, the source object and the source property name.&lt;/p&gt;

&lt;p&gt;To render the details view, I decided to not use a single item DataView like I did with the declarative version: since I’m going to use imperative code instead of declarative bindings, it is just as easy to directly manipulate the DOM that already exists, and do some hiding and showing of elements:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;function &lt;/span&gt;onCommand(sender, args) {
  &lt;span style="color: blue"&gt;if &lt;/span&gt;(args.get_commandName() === &lt;span style="color: maroon"&gt;&amp;quot;select&amp;quot;&lt;/span&gt;) {
    &lt;span style="color: blue"&gt;var &lt;/span&gt;dataItem = sender.findContext(&lt;br /&gt;      args.get_commandSource()).dataItem;
    &lt;span style="color: blue"&gt;var &lt;/span&gt;isClass = Type.isClass(dataItem) &amp;amp;&amp;amp;&lt;br /&gt;                 !Type.isNamespace(dataItem);
    &lt;span style="color: blue"&gt;var &lt;/span&gt;childData =&lt;br /&gt;      (isClass ? getMembers : getChildren)(dataItem),
      namespaceElementsDisplay = &lt;br /&gt;        isClass ? &lt;span style="color: maroon"&gt;&amp;quot;none&amp;quot; &lt;/span&gt;: &lt;span style="color: maroon"&gt;&amp;quot;block&amp;quot;&lt;/span&gt;,
      classElementsDisplay =&lt;br /&gt;        isClass ? &lt;span style="color: maroon"&gt;&amp;quot;block&amp;quot; &lt;/span&gt;: &lt;span style="color: maroon"&gt;&amp;quot;none&amp;quot;&lt;/span&gt;,
      detailsChild =&lt;br /&gt;        Sys.Application.findComponent(&lt;span style="color: maroon"&gt;&amp;quot;detailsChild&amp;quot;&lt;/span&gt;);
    detailsChild.onItemRendering =&lt;br /&gt;      isClass ?&lt;br /&gt;        onClassMemberRendering :&lt;br /&gt;        onNamespaceChildRendering;
    detailsChild.set_data(childData);
    Sys.get(&lt;span style="color: maroon"&gt;&amp;quot;#detailsTitle&amp;quot;&lt;/span&gt;).innerHTML =&lt;br /&gt;      dataItem.getName();
    Sys.get(&lt;span style="color: maroon"&gt;&amp;quot;#namespacesColumn&amp;quot;&lt;/span&gt;).style.display =
    Sys.get(&lt;span style="color: maroon"&gt;&amp;quot;#classesColumn&amp;quot;&lt;/span&gt;).style.display =&lt;br /&gt;      namespaceElementsDisplay;
    Sys.get(&lt;span style="color: maroon"&gt;&amp;quot;#propertiesColumn&amp;quot;&lt;/span&gt;).style.display =
    Sys.get(&lt;span style="color: maroon"&gt;&amp;quot;#eventsColumn&amp;quot;&lt;/span&gt;).style.display =
    Sys.get(&lt;span style="color: maroon"&gt;&amp;quot;#methodsColumn&amp;quot;&lt;/span&gt;).style.display =
    Sys.get(&lt;span style="color: maroon"&gt;&amp;quot;#staticMethodsColumn&amp;quot;&lt;/span&gt;).style.display =&lt;br /&gt;      classElementsDisplay;
    Sys.get(&lt;span style="color: maroon"&gt;&amp;quot;#details&amp;quot;&lt;/span&gt;).style.display = &lt;span style="color: maroon"&gt;&amp;quot;block&amp;quot;&lt;/span&gt;;
  }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;We do have a DataView to render the contents of the currently selected object though. The nice trick we used with the declarative version to dynamically switch the target place holder where the template gets rendered is still there, which enables a single DataView control to dispatch the data into two to four separate lists (or however much you want for that matter):&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;function &lt;/span&gt;onNamespaceChildRendering(args) {
    args.set_itemPlaceholder(
        Type.isClass(args.get_dataItem()) ?
            &lt;span style="color: maroon"&gt;&amp;quot;#classPlaceHolder&amp;quot; &lt;/span&gt;:
            &lt;span style="color: maroon"&gt;&amp;quot;#namespacePlaceHolder&amp;quot;
    &lt;/span&gt;);
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;I think all this is pretty cool and I hope the comparison between the declarative version and the imperative version of this little application gives you a sense of the flexibility that the Microsoft Ajax library now offers, and of how much you can choose your own development style and do pretty much anything with the same ease.&lt;/p&gt;

&lt;p&gt;Download the code for this post here: 
  &lt;br /&gt;&lt;a title="http://weblogs.asp.net/blogs/bleroy/Samples/Preview6ClassBrowser.zip" href="http://weblogs.asp.net/blogs/bleroy/Samples/Preview6ClassBrowser.zip"&gt;http://weblogs.asp.net/blogs/bleroy/Samples/Preview6ClassBrowser.zip&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Microsoft Ajax Library Preview 6 can be downloaded from here:
  &lt;br /&gt;&lt;a href="http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=34488"&gt;http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=34488&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here are a few links about this release:
 &lt;br/&gt;&lt;a href="http://weblogs.asp.net/scottgu/archive/2009/10/15/announcing-microsoft-ajax-library-preview-6-and-the-microsoft-ajax-minifier.aspx"&gt;http://weblogs.asp.net/scottgu/archive/2009/10/15/announcing-microsoft-ajax-library-preview-6-and-the-microsoft-ajax-minifier.aspx&lt;/a&gt;
  &lt;br /&gt;&lt;a href="http://channel9.msdn.com/posts/jsenior/Announcing-Microsoft-Ajax-Library-Preview-6/"&gt;http://channel9.msdn.com/posts/jsenior/Announcing-Microsoft-Ajax-Library-Preview-6/&lt;/a&gt;

  &lt;br /&gt;&lt;a href="http://www.jamessenior.com/post/How-the-Script-Loader-in-the-Microsoft-Ajax-Library-will-make-your-life-wonderful.aspx"&gt;http://www.jamessenior.com/post/How-the-Script-Loader-in-the-Microsoft-Ajax-Library-will-make-your-life-wonderful.aspx&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7230434" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/HTML/default.aspx">HTML</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/jQuery/default.aspx">jQuery</category></item><item><title>Ajax Control Toolkit: new controls, bug fixes</title><link>http://weblogs.asp.net/bleroy/archive/2009/09/30/ajax-control-toolkit-new-controls-bug-fixes.aspx</link><pubDate>Thu, 01 Oct 2009 00:28:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7220674</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>15</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7220674</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/09/30/ajax-control-toolkit-new-controls-bug-fixes.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px 0px 10px 10px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="(c) Bertrand Le Roy 2006" border="0" alt="(c) Bertrand Le Roy 2006" align="right" src="http://weblogs.asp.net/blogs/bleroy/101327811_c6608a9e1b_b_31770601.jpg" width="244" height="164" /&gt; And we have a new release of Ajax Control Toolkit. I didn’t work on this one but there are some nice things in there nonetheless :)&lt;/p&gt;  &lt;p&gt;First, new controls!&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;a href="http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/Seadragon/Seadragon.aspx"&gt;SeaDragon&lt;/a&gt;&lt;/strong&gt;: I’ve &lt;a href="http://weblogs.asp.net/bleroy/archive/2008/11/20/deep-zoom-without-silverlight.aspx"&gt;blogged&lt;/a&gt; &lt;a href="http://weblogs.asp.net/bleroy/archive/2009/02/13/virtualalbion-using-deep-zoom-and-seadragon-ajax.aspx"&gt;before&lt;/a&gt; about &lt;a href="http://seadragon.com/"&gt;Seadragon&lt;/a&gt;, the JavaScript-only way to do &lt;a href="http://msdn.microsoft.com/en-us/library/cc645050(VS.95).aspx"&gt;Deep Zoom&lt;/a&gt;. It became a lot easier to use a few month ago when the need for tools disappeared and you can just &lt;a href="http://seadragon.com/create/"&gt;point to any image on the web&lt;/a&gt; and immediately get the &lt;a href="http://seadragon.com/view/bqy"&gt;URL&lt;/a&gt; and script tag to put on your page:&lt;/p&gt; &lt;script src="http://seadragon.com/embed/bqy.js?width=auto&amp;amp;height=400px"&gt;&lt;/script&gt;  &lt;p&gt;Now with this release of Ajax Control Toolkit, &lt;a href="http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/Seadragon/Seadragon.aspx"&gt;including and controlling Deep Zoom from an ASP.NET page is also very easy&lt;/a&gt;:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;ajaxToolkit&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;Seadragon &lt;/span&gt;&lt;span style="color: red"&gt;ID&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Seadragon&amp;quot;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: red"&gt;CssClass&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;seadragon&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;runat&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;server&amp;quot;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: red"&gt;SourceUrl&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;sample.xml&amp;quot;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;James Senior just released a &lt;a href="http://channel9.msdn.com/posts/jsenior/Seadragon-Ajax-Control-Quick-Start-Guide/"&gt;screencast&lt;/a&gt; on how to create Deep Zoom contents for the new Seadragon control:

  &lt;br /&gt;&lt;a href="http://channel9.msdn.com/posts/jsenior/Seadragon-Ajax-Control-Quick-Start-Guide/"&gt;http://channel9.msdn.com/posts/jsenior/Seadragon-Ajax-Control-Quick-Start-Guide/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/AsyncFileUpload/AsyncFileUpload.aspx"&gt;AsyncFileUpload&lt;/a&gt;&lt;/strong&gt;: This is by far one of the most requested controls for ACT. File upload fields, while a part of HTML, do not work with Ajax/XHR requests (for security reasons, JavaScript can’t access the contents of the field). The only way to use them is to get the browser to do a real form post.&lt;/p&gt;

&lt;p&gt;This new control makes it a lot easier to handle file uploads from your Ajax applications by providing an abstraction on top of the form posting:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;ajaxToolkit&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;AsyncFileUpload
    &lt;/span&gt;&lt;span style="color: red"&gt;OnClientUploadError&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;uploadError&amp;quot;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: red"&gt;OnClientUploadComplete&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;uploadComplete&amp;quot; 
    &lt;/span&gt;&lt;span style="color: red"&gt;runat&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;server&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;ID&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;AsyncFileUpload1&amp;quot;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: red"&gt;Width&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;400px&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;UploaderStyle&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Modern&amp;quot; 
    &lt;/span&gt;&lt;span style="color: red"&gt;UploadingBackColor&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;#CCFFFF&amp;quot;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: red"&gt;ThrobberID&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;myThrobber&amp;quot;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;It works pretty much as advertised: just drop the control on the page, and you can upload files without a full postback. It looks just like Ajax and requires no plug-in of any kind.&lt;img style="border-right-width: 0px; margin: 10px auto; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="AsyncFileUpload" border="0" alt="AsyncFileUpload" src="http://weblogs.asp.net/blogs/bleroy/AsyncFileUpload_6D7321E9.png" width="519" height="262" /&gt; &lt;/p&gt;

&lt;p&gt;The control has client and server-side events that get triggered when the file has been uploaded. On the server-side, you have access to the uploaded file’s byte stream, which you can save to disk (or database, or whatever).&lt;/p&gt;

&lt;p&gt;Bug fixes: This release also has some new bug fixes (courtesy of &lt;a href="http://obout.com/"&gt;Obout&lt;/a&gt;) for some of the &lt;a title="Click on &amp;quot;Votes&amp;quot; on the right-side of the page to sort by votes." href="http://ajaxcontroltoolkit.codeplex.com/WorkItem/List.aspx"&gt;top-voted issues&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Download the new release here: 
  &lt;br /&gt;&lt;a href="http://ajaxcontroltoolkit.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=33804"&gt;http://ajaxcontroltoolkit.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=33804&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Try the live demos here: 
  &lt;br /&gt;&lt;a href="http://www.asp.net/ajax/ajaxcontroltoolkit/samples/"&gt;http://www.asp.net/ajax/ajaxcontroltoolkit/samples/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Stephen's in-depth post about this release:
&lt;br/&gt;&lt;a href="http://stephenwalther.com/blog/archive/2009/10/01/new-ajax-control-toolkit-release.aspx"&gt;http://stephenwalther.com/blog/archive/2009/10/01/new-ajax-control-toolkit-release.aspx&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7220674" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Ajax+Control+Toolkit/default.aspx">Ajax Control Toolkit</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Deep+Zoom/default.aspx">Deep Zoom</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/CodePlex/default.aspx">CodePlex</category></item><item><title>Building a class browser with Microsoft Ajax 4.0 Preview 5</title><link>http://weblogs.asp.net/bleroy/archive/2009/09/14/building-a-class-browser-with-microsoft-ajax-4-0-preview-5.aspx</link><pubDate>Mon, 14 Sep 2009 07:29:37 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7204694</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>9</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7204694</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/09/14/building-a-class-browser-with-microsoft-ajax-4-0-preview-5.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 0px 10px 10px; display: inline; border-top: 0px; border-right: 0px" title="(c) 2004 Bertrand Le Roy" border="0" alt="(c) 2004 Bertrand Le Roy" align="right" src="http://weblogs.asp.net/blogs/bleroy/DSCN3593_4CCCACF9.jpg" width="184" height="244" /&gt; The Microsoft Ajax Library 4.0 Preview 5 is the first release of Microsoft Ajax that I didn’t participate in: I left the team a few months ago. But that doesn’t mean I don’t love what’s in there, and I really do. And by the way I’ve also seen what’s in Preview 6 too and man that will seriously rock.&lt;/p&gt;  &lt;p&gt;So I thought I’d write a little something to celebrate the new preview. The new features include recursive templates, which is pretty much begging us to implement a treeview with it, and we’ll do just that in this post.&lt;/p&gt;  &lt;p&gt;There is also an intriguing capability, which enables you to dynamically set what template to render for each data item, and where to render it. At first, this doesn’t look like the most useful thing in the world, but it actually opens up some very interesting possibilities, which we’ll also show in this post.&lt;/p&gt;  &lt;p&gt;The sample code that I’m going to write for this post is a rudimentary class browser. It will render a treeview representing the hierarchical structure of namespaces and classes in Microsoft Ajax, and clicking one of the tree nodes will render a details view for it: a list of classes and subnamespaces for namespaces, and a grouped list of members for classes.&lt;/p&gt;  &lt;p&gt;Let’s start with the tree. It will be rendered as nested unordered lists by a simple recursive DataView:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;ul &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;tree&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;tree&amp;quot;
    &lt;/span&gt;&lt;span style="color: red"&gt;sys&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;attach&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;dataview&amp;quot;
    &lt;/span&gt;&lt;span style="color: red"&gt;dataview&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{{ Type.getRootNamespaces() }}&amp;quot;
    &lt;/span&gt;&lt;span style="color: red"&gt;dataview&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;itemtemplate&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;nodeTemplate&amp;quot;
    &lt;/span&gt;&lt;span style="color: red"&gt;dataview&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;oncommand&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{{ onCommand }}&amp;quot;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;ul&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;

&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;ul &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;nodeTemplate&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;sys-template&amp;quot;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;li&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;    &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;a &lt;/span&gt;&lt;span style="color: red"&gt;href&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;#&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;onclick&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;return false;&amp;quot;
       &lt;/span&gt;&lt;span style="color: red"&gt;sys&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;command&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;select&amp;quot;&amp;gt;
      &lt;/span&gt;{{ getSimpleName($dataItem.getName()) }}
    &lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;a&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;ul &lt;/span&gt;&lt;span style="color: red"&gt;sys&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;attach&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;dataview&amp;quot;
        &lt;/span&gt;&lt;span style="color: red"&gt;dataview&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{{ getChildren($dataItem) }}&amp;quot;
        &lt;/span&gt;&lt;span style="color: red"&gt;dataview&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;itemtemplate&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;nodeTemplate&amp;quot;
        &lt;/span&gt;&lt;span style="color: red"&gt;dataview&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;oncommand&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{{ onCommand }}&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;ul&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;li&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;ul&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;On the first UL, which is the outer DataView for the tree, you can see that we set the data property to Type.getRootNamespaces(), which returns the set of root namespaces currently defined.&lt;/p&gt;

&lt;p&gt;We also set the template to point to the “nodeTemplate” element, which has to be outside the DataView itself when doing recursive templates. Note that the outer node of the template, the UL, won’t actually get rendered into the target ul (tree). It is only a container.&lt;/p&gt;

&lt;p&gt;The command event of the DataView is hooked to the onCommand function, and we’ll get back to that when we couple the tree with the detail view.&lt;/p&gt;

&lt;p&gt;In the template itself, you can see we have a link with the select command so that clicking it will trigger the nearest onCommand event up the DOM.&lt;/p&gt;

&lt;p&gt;The text of that link is the results of a call to getSimpleName, which will extract the last part of the fully-qualified name of the namespace or class.&lt;/p&gt;

&lt;p&gt;After that link, we find another DataView control. The data property of that control points to an array of namespaces and classes under the current object. But the nice part here is that the template property points to “nodeTemplate”, its own parent, enabling the recursive nature of the tree.&lt;/p&gt;

&lt;p&gt;In other words, we’ve morphed a simple DataView control into a tree, with minimal effort and code.&lt;/p&gt;

&lt;p&gt;There is just one thing missing to the tree, and that is the +/- buttons that will collapse and expand the tree nodes. This is actually very easy to set-up using CSS and some simple script. First, let’s collapse the tree by default. This is done by defining the style of the tree as follows in our stylesheet:&lt;/p&gt;

&lt;pre class="code"&gt;.tree ul 
{
    padding: 0;
    display:none;
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This has the effect of collapsing all unordered list nodes under the tree.&lt;/p&gt;

&lt;p&gt;The +/- button is created by adding the following to the template, right before the existing link:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;a &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;toggleButton&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;href&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;#&amp;quot;
   &lt;/span&gt;&lt;span style="color: red"&gt;sys&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;if&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Type.isNamespace($dataItem)&amp;quot; 
   &lt;/span&gt;&lt;span style="color: red"&gt;onclick&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;return toggleVisibility(this);&amp;quot;&amp;gt;&lt;/span&gt;+&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;a&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The button is a simple link whose rendering is conditioned by whether the current data item is a namespace: only namespaces can be expanded, classes are leaf nodes.&lt;/p&gt;

&lt;p&gt;The toggling function itself is fairly simple:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;function &lt;/span&gt;toggleVisibility(element) {
    &lt;span style="color: blue"&gt;var &lt;/span&gt;childList = element.parentNode&lt;br /&gt;                     .getElementsByTagName(&lt;span style="color: maroon"&gt;&amp;quot;ul&amp;quot;&lt;/span&gt;)[0],
        isClosed = element.innerHTML === &lt;span style="color: maroon"&gt;&amp;quot;+&amp;quot;&lt;/span&gt;;
    childList.style.display =&lt;br /&gt;        isClosed ? &lt;span style="color: maroon"&gt;&amp;quot;block&amp;quot; &lt;/span&gt;: &lt;span style="color: maroon"&gt;&amp;quot;none&amp;quot;&lt;/span&gt;;
    element.innerHTML = isClosed ? &lt;span style="color: maroon"&gt;&amp;quot;-&amp;quot; &lt;/span&gt;: &lt;span style="color: maroon"&gt;&amp;quot;+&amp;quot;&lt;/span&gt;;
    &lt;span style="color: blue"&gt;return false&lt;/span&gt;;
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This just toggles the display style of the first child UL between none and block, and the text of the link between + and –.&lt;/p&gt;

&lt;p&gt;So there it is, we have built a simple tree by simply making use of the recursive capabilities of DataView and some very simple script.&lt;/p&gt;

&lt;p&gt;Before we look at the details view, let’s look at the code that gets called when the user selects a node in the tree:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;function &lt;/span&gt;onCommand(sender, args) {
    &lt;span style="color: blue"&gt;var &lt;/span&gt;dataItem = sender.findContext(&lt;br /&gt;        args.get_commandSource()).dataItem;
    $find(&lt;span style="color: maroon"&gt;&amp;quot;details&amp;quot;&lt;/span&gt;).set_data(dataItem);
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;That code gets a reference to the data item for the selected node from the template context that we can get from the sender of the event (the inner DataView that contains the selected node) using the command source as provided by the event arguments (that source is the element that triggered the command). We can then set the data of the details DataView to that data item, which will trigger that view to re-render.&lt;/p&gt;

&lt;p&gt;Now let’s build the details view. The details view will display the child namespaces and classes if a namespace is selected in the tree, and the properties, events and methods (instance and static) in the case of a class.&lt;/p&gt;

&lt;p&gt;For each case, we’ll use a different template: “namespaceTemplate” for namespaces, and “classTemplate” for classes,&amp;#160; but we’ll do so from the same DataView. This dynamic template switching is done by handling the onItemRendering event of the DataView:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;function &lt;/span&gt;onDetailsRendering(sender, args) {
    &lt;span style="color: blue"&gt;var &lt;/span&gt;dataItem = args.get_dataItem();
    args.set_itemTemplate(Type.isNamespace(dataItem) ?
        &lt;span style="color: maroon"&gt;&amp;quot;namespaceTemplate&amp;quot;&lt;/span&gt; : &lt;span style="color: maroon"&gt;&amp;quot;classTemplate&amp;quot;&lt;/span&gt;);
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This code gets the data item from the event arguments and sets the itemTemplate property depending on its type.&lt;/p&gt;

&lt;p&gt;Each of these two templates will have to display the contents of the selected object. But, and that will be the tricky part, we want all those to be neatly grouped into separated lists.&lt;/p&gt;

&lt;p&gt;One way to do that would be to have one DataView per list but where would the fun be in that? Here, we are going to enumerate only once through the data items to display and dispatch them dynamically to this or that placeholder depending on their nature.&lt;/p&gt;

&lt;p&gt;Once more, the key to doing that will be handling the onItemRendering event:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;function &lt;/span&gt;onNamespaceChildRendering(sender, args) {
    &lt;span style="color: blue"&gt;if &lt;/span&gt;(Type.isClass(args.get_dataItem())) {
        args.set_itemPlaceholder(&lt;span style="color: maroon"&gt;&amp;quot;classPlaceHolder&amp;quot;&lt;/span&gt;);
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This code is simply changing the rendering place holder for the curent item from the default (the DataView’s element) to “classPlaceHolder” if the current data item is a class (instead of a namespace). The template itself looks like this:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;div &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;namespaceTemplate&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;sys-template&amp;quot;&amp;gt;
   &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;h1&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;{{ $dataItem.getName() }}&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;h1&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
   &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;div &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;column&amp;quot;&amp;gt;
     &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;h2&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;Namespaces:&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;h2&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
     &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;ul &lt;/span&gt;&lt;span style="color: red"&gt;sys&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;namespacePlaceHolder&amp;quot;
         &lt;/span&gt;&lt;span style="color: red"&gt;sys&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;attach&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;dataview&amp;quot;
         &lt;/span&gt;&lt;span style="color: red"&gt;dataview&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;data&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{{ getChildren($dataItem) }}&amp;quot;
         &lt;/span&gt;&lt;span style="color: red"&gt;dataview&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;itemtemplate&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;namespaceChildTemplate&amp;quot;
         &lt;/span&gt;&lt;span style="color: red"&gt;dataview&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;onitemrendering&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;br /&gt;           &amp;quot;{{ onNamespaceChildRendering }}&amp;quot;&amp;gt;
     &amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;ul&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
   &amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;div&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
   &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;div &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;column&amp;quot;&amp;gt;
   &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;h2&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;Classes:&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;h2&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
     &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;ul&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;li &lt;/span&gt;&lt;span style="color: red"&gt;sys&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;classPlaceHolder&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;li&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;ul&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
   &amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;div&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;div&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;

&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;ul &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;namespaceChildTemplate&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;sys-template&amp;quot;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;li&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;{{ $dataItem.getName() }}&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;li&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon"&gt;ul&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;As you can see, there really is only one DataView in there, and thanks to the code above, it can dispatch its rendering to different places if necessary. The template for the items of that DataView happens to be the same in all cases (namespaceChildTemplate) but it could be easily different, as it was for the parent details view.&lt;/p&gt;

&lt;p&gt;The template for displaying classes is essentially the same thing, but with four placeholders instead of two.&lt;/p&gt;

&lt;p&gt;So here’s what it looks like in the end:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/bleroy/ClassBrowser_5CC62533.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" title="Class Browser" border="0" alt="Class Browser" src="http://weblogs.asp.net/blogs/bleroy/ClassBrowser_thumb_70730EC7.png" width="504" height="172" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key takeaways&lt;/strong&gt; of this post are that it’s now super-easy to render hierarchical data structures with DataView, and that you can do some interesting grouping of data on the fly by handling the item rendering event.&lt;/p&gt;

&lt;p&gt;You can play with the class browser live here:
  &lt;br /&gt;&lt;a href="http://boudin.vndv.com/AjaxPreview5Tree/default.htm"&gt;http://boudin.vndv.com/AjaxPreview5Tree/default.htm&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And you can download the code here:
  &lt;br /&gt;&lt;a title="http://weblogs.asp.net/blogs/bleroy/Samples/AjaxPreview5Tree.zip" href="http://weblogs.asp.net/blogs/bleroy/Samples/AjaxPreview5Tree.zip"&gt;http://weblogs.asp.net/blogs/bleroy/Samples/AjaxPreview5Tree.zip&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Microsoft Ajax 4.0 Preview 5:
  &lt;br /&gt;&lt;a href="http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=32770"&gt;http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=32770&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Jim and Dave’s posts on Preview 5:
  &lt;br /&gt;&lt;a href="http://weblogs.asp.net/jimwang/archive/2009/09/11/asp-net-ajax-preview-5-and-updatepanel.aspx"&gt;http://weblogs.asp.net/jimwang/archive/2009/09/11/asp-net-ajax-preview-5-and-updatepanel.aspx&lt;/a&gt;

  &lt;br /&gt;&lt;a href="http://weblogs.asp.net/infinitiesloop/archive/2009/09/10/microsoft-ajax-4-preview-5-the-dataview-control.aspx"&gt;http://weblogs.asp.net/infinitiesloop/archive/2009/09/10/microsoft-ajax-4-preview-5-the-dataview-control.aspx&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7204694" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/HTML/default.aspx">HTML</category></item><item><title>querySelectorAll on old IE versions: something that doesn’t work</title><link>http://weblogs.asp.net/bleroy/archive/2009/08/31/queryselectorall-on-old-ie-versions-something-that-doesn-t-work.aspx</link><pubDate>Tue, 01 Sep 2009 01:03:13 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7187174</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7187174</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/08/31/queryselectorall-on-old-ie-versions-something-that-doesn-t-work.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 0px 10px 10px; display: inline; border-top: 0px; border-right: 0px" title="(c) Bertrand Le Roy 2005" border="0" alt="(c) Bertrand Le Roy 2005" align="right" src="http://weblogs.asp.net/blogs/bleroy/WallDoor_7CF5A223.jpg" width="224" height="244" /&gt; In today’s post, I’m going to show an interesting technique to solve a problem and then I will tear it to pieces and explain why it is actually useless. I believe that negative results should also be published so that we can save other people from wasting time trying the same thing. So here goes…&lt;/p&gt;  &lt;p&gt;A few days ago, &lt;a href="http://ajaxian.com/archives/creating-a-queryselector-for-ie-that-runs-at-native-speed"&gt;a post on Ajaxian&lt;/a&gt; proposed a new version of a somewhat old technique to implement &lt;a href="http://www.w3.org/TR/2007/WD-selectors-api-20071221/"&gt;querySelectorAll&lt;/a&gt; on old versions of IE, using the browser’s native CSS engine. That sounds like a great idea at first, and the hack is quite clever. The idea is to dynamically add a CSS rule to the document that has the selector that you want to evaluate, and an expression that adds the matched elements to a global array.&lt;/p&gt;  &lt;p&gt;When I read this, it reminded me of a similar approach that I had tried a few years ago. At the time, we were considering implementing our own selector engine (we had not yet decided to integrate jQuery to our Ajax offerings, which in the end made the whole effort moot) so we explored a number of approaches.&lt;/p&gt;  &lt;p&gt;My idea was different in that it doesn’t use expressions at all. It does dynamically create a style rule, but instead of an expression, it just sets a non-existing “foo” style property to the equally arbitrary value of “bar”. It then scans the whole document (using the much decried and IE-specific but very fast document.all) and gets the computed style for each of the elements. We then look for the foo property on the resulting object and check whether it evaluates as “bar”. For each element that matches, we add to an array.&lt;/p&gt;  &lt;p&gt;Here’s the code:&lt;/p&gt;  &lt;pre class="code"&gt;(&lt;span style="color: blue"&gt;function&lt;/span&gt;() {
    &lt;span style="color: blue"&gt;var &lt;/span&gt;style = document.styleSheets[0] ||&lt;br /&gt;                document.createStyleSheet();
    window.select = &lt;span style="color: blue"&gt;function&lt;/span&gt;(selector) {
        style.addRule(selector, &lt;span style="color: #a31515"&gt;&amp;quot;foo:bar&amp;quot;&lt;/span&gt;);
        &lt;span style="color: blue"&gt;var &lt;/span&gt;all = document.all, resultSet = [];
        &lt;span style="color: blue"&gt;for &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;i = 0, l = all.length; i &amp;lt; l; i++) {
            &lt;span style="color: blue"&gt;if &lt;/span&gt;(all[i].currentStyle.foo === &lt;span style="color: #a31515"&gt;&amp;quot;bar&amp;quot;&lt;/span&gt;) {
                resultSet[resultSet.length] = all[i];
            }
        }
        style.removeRule(0);
        &lt;span style="color: blue"&gt;return &lt;/span&gt;resultSet;
    }
})();&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;or, in minimized form:&lt;/p&gt;

&lt;pre class="code"&gt;(&lt;span style="color: blue"&gt;function&lt;/span&gt;(){&lt;span style="color: blue"&gt;var &lt;/span&gt;d=document;&lt;span style="color: blue"&gt;var &lt;/span&gt;a=d.styleSheets[0]||&lt;br /&gt;d.createStyleSheet();window.select=&lt;span style="color: blue"&gt;function&lt;/span&gt;(e){&lt;br /&gt;a.addRule(e,&lt;span style="color: #a31515"&gt;&amp;quot;f:b&amp;quot;&lt;/span&gt;);&lt;span style="color: blue"&gt;var &lt;/span&gt;l=d.all,c=[];&lt;br /&gt;&lt;span style="color: blue"&gt;for&lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;b=0,f=l.length;b&amp;lt;f;b++)&lt;span style="color: blue"&gt;if&lt;/span&gt;(l[b].currentStyle.f)&lt;br /&gt;c[c.length]=l[b];a.removeRule(0);&lt;span style="color: blue"&gt;return &lt;/span&gt;c}})()&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;That’s 235 characters, which is not too bad (although not quite #twitcode small).&lt;/p&gt;

&lt;p&gt;The first problem with that approach though is that because it’s using the native CSS selection engine in IE, it has the same limitations and quirks. That means no fancy CSS 3 (or even 2) selectors. It also means any IE bug will surface into the result set.&lt;/p&gt;

&lt;p&gt;In other words, if you want more selectors than that, you will need to parse the selector string and branch off the code to another, more complete engine whenever something not supported is used. It also means that you need to know what is supported and what isn’t. That could be done through some dynamic discovery but doing so, we are getting into much complexity.&lt;/p&gt;

&lt;p&gt;So limited as it is, how does it perform?&lt;/p&gt;

&lt;p&gt;I ran the code in a &lt;a href="http://code.google.com/p/slickspeed/"&gt;SlickSpeed&lt;/a&gt; test (where I removed the selectors that it couldn’t handle) on IE6 and the good news is that despite the document.all scan and the current style computation, it’s more than three times faster than &lt;a href="http://ajaxian.com/archives/creating-a-queryselector-for-ie-that-runs-at-native-speed"&gt;Paul Young’s implementation that got featured on Ajaxian&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;But the bad news is that it’s also &lt;strong&gt;six times slower than jQuery:&lt;img style="border-bottom: 0px; border-left: 0px; margin: 5px auto; display: block; float: none; border-top: 0px; border-right: 0px" title="SlickSpeedResults" border="0" alt="SlickSpeedResults" src="http://weblogs.asp.net/blogs/bleroy/SlickSpeedResults_62B538FF.png" width="540" height="484" /&gt; &lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I’m afraid a hack to use the native CSS selection engine of the browser is always going to be slower than an optimized pure JavaScript implementation (to be clear, I’m not talking about native implementations of querySelectorAll, but about hacks such as this which try to surface the feature on older IE versions that don’t have querySelectorAll). Somewhat counter-intuitive, but true.&lt;/p&gt;

&lt;p&gt;End of story. Just use jQuery. :)&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7187174" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Internet+Explorer/default.aspx">Internet Explorer</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/jQuery/default.aspx">jQuery</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/CSS/default.aspx">CSS</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/TwitCode/default.aspx">TwitCode</category></item><item><title>Why is ASP.NET encoding &amp;’s in script URLs? A tale of looking at entirely the wrong place for a cause to a non-existing bug.</title><link>http://weblogs.asp.net/bleroy/archive/2009/06/05/why-is-asp-net-encoding-amp-s-in-script-urls-a-tale-of-looking-at-entirely-the-wrong-place-for-a-cause-to-a-non-existing-bug.aspx</link><pubDate>Sat, 06 Jun 2009 00:27:15 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7109236</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>7</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7109236</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/06/05/why-is-asp-net-encoding-amp-s-in-script-urls-a-tale-of-looking-at-entirely-the-wrong-place-for-a-cause-to-a-non-existing-bug.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/bleroy/Bug_50FD5922.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="(c) Bertrand Le Roy 2003" border="0" alt="(c) Bertrand Le Roy 2003" align="left" src="http://weblogs.asp.net/blogs/bleroy/Bug_thumb_774F16A0.jpg" width="254" height="184" /&gt;&lt;/a&gt; Several people have &lt;a href="http://ajaxcontroltoolkit.codeplex.com/WorkItem/View.aspx?WorkItemId=13134"&gt;reported&lt;/a&gt; seeing errors in their logs that seem to be due to requests such as this:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;/ScriptResource.axd?d=     &lt;br /&gt;[lots of junk]&lt;strong&gt;&amp;amp;amp;       &lt;br /&gt;&lt;/strong&gt;t=ffffffffee24147c&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The important part here is the HTML-encoded “&amp;amp;amp;” sequence, which stands for “&amp;amp;” of course. &lt;em&gt;If&lt;/em&gt; this exact URL is sent to the server, the server won’t know what to do with the escape sequence (URLs are not supposed to be HTML-encoded on the wire) so the parameters won’t get separated as expected, potentially resulting in a server error. This bug in the toolkit is an example of that: &lt;a href="http://ajaxcontroltoolkit.codeplex.com/WorkItem/View.aspx?WorkItemId=13134"&gt;http://ajaxcontroltoolkit.codeplex.com/WorkItem/View.aspx?WorkItemId=13134&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Of course, when people see 500 errors popping up in their server logs, they immediately assume the application is failing for some users. Or that some idiot at Microsoft did something incredibly stupid (that’s what we idiots at Microsoft do after all).&lt;/p&gt;  &lt;p&gt;Case in point, a quick peek into the source code of the application’s pages immediately reveals that the script tags generated by ScriptManager do indeed generate these URLs:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;&amp;lt;script src=&amp;quot;/ScriptResource.axd?d=[lots of junk]&amp;amp;amp;t=ffffffff8824ac28&amp;quot; type=&amp;quot;text/javascript&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;So that’s where it came from! See? &lt;strong&gt;When I copy this URL into the browser’s URL bar, I do get the same error!&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Then ensue various more or less rational reactions such as:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Correlate the user agent to the faulty requests (which correlates more or less with normal browser usage, i.e. lots of IE and then lots of Firefox, when there is a large enough sample).&lt;/li&gt;    &lt;li&gt;Blame IE6 (lots of these requests come from IE6, hence it must be responsible: IE6 sucks).&lt;/li&gt;    &lt;li&gt;“Fix” ScriptManager and remove the HTML encoding.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Well, by copying the URL from the source view into the URL bar, you did indeed reproduce the problem. A little too well. Better than you realize.&lt;/p&gt;  &lt;p&gt;This is why. &lt;strong&gt;All of the errors in your server logs come from people doing precisely what you just did&lt;/strong&gt;: copy the URL from the source view into the browser’s URL bar. They do it for various reasons: look at the source code for the scripts, understand what these weird URLs are, who knows?&lt;/p&gt;  &lt;p&gt;But the point is, you will never be able to reproduce these errors during normal use of the application. There is nothing to fix here. The value that gets sent to the server never has the “&amp;amp;amp;” sequence. You can verify it in IE6, you can verify it in any browser on any OS, it will just work.&lt;/p&gt;  &lt;p&gt;When putting a URL in an HTML attribute, you should &lt;strong&gt;*always*&lt;/strong&gt; HTML-encode it. It’s the standard, and for good reason (it enables the browser to tell between “&amp;amp;”, “&amp;amp;amp;” and “&amp;amp;amp;amp;”, it enables quotes to be embedded into attributes, etc.).&lt;/p&gt;  &lt;p&gt;A consequence of that is that if you’re going to copy the value of one of these attributes from the source view, you should do what the browser does when parsing the HTML: decode the value first (in other words, replace “&amp;amp;amp;” with “&amp;amp;”).&lt;/p&gt;  &lt;p&gt;So yes, people do fail to do that and copy the URL without decoding. Well, they are not supposed to do that, nor do they need to do it. The error is normal, it results from a bad URL having been entered manually. Nobody would be surprised to get an error when querying foo.aspx?somenumber=thisisnotanumber for example. Same thing here. Pretty much.&lt;/p&gt;  &lt;p&gt;Of course, this is not entirely trivial to figure out and I did pull my remaining hair a bit trying to understand what was going on, and you tend to trust people when they tell you there is a problem, especially when the description seems to make sense. There is some sort of confirmation bias going on there. But the more I looked at the different pieces of evidence, the more this explanation looked like the most likely, by far.&lt;/p&gt;  &lt;p&gt;But of course, I may be missing something…&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7109236" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/HTML/default.aspx">HTML</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Internet+Explorer/default.aspx">Internet Explorer</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Ajax+Control+Toolkit/default.aspx">Ajax Control Toolkit</category></item><item><title>Survey: Ajax usage among .NET developers</title><link>http://weblogs.asp.net/bleroy/archive/2009/05/22/survey-ajax-usage-among-net-developers.aspx</link><pubDate>Fri, 22 May 2009 19:13:20 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7094884</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>9</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7094884</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/05/22/survey-ajax-usage-among-net-developers.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/bleroy/Copenhagen_36DBEC47.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="(c) Bertrand Le Roy 2003" border="0" alt="(c) Bertrand Le Roy 2003" align="left" src="http://weblogs.asp.net/blogs/bleroy/Copenhagen_thumb_55B26025.jpg" width="204" height="154" /&gt;&lt;/a&gt; If you haven’t already and you are a .NET developer, please take a couple minutes and answer this survey, whether you use Ajax or not. There are a number of Ajax surveys around, but Simone’s is the only one that focuses on .NET developers.&lt;/p&gt;  &lt;p&gt;The survey:   &lt;br /&gt;&lt;a title="http://www.zoomerang.com/Survey/?p=WEB22973CYKW2H" href="http://www.zoomerang.com/Survey/?p=WEB22973CYKW2H"&gt;http://www.zoomerang.com/Survey/?p=WEB22973CYKW2H&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Simone’s post:   &lt;br /&gt;&lt;a href="http://codeclimber.net.nz/archive/2009/05/21/ajax-usage-among-.net-developers-in-2009.aspx"&gt;http://codeclimber.net.nz/archive/2009/05/21/ajax-usage-among-.net-developers-in-2009.aspx&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7094884" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Ajax+Control+Toolkit/default.aspx">Ajax Control Toolkit</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/jQuery/default.aspx">jQuery</category></item><item><title>setInterval is (moderately) evil</title><link>http://weblogs.asp.net/bleroy/archive/2009/05/14/setinterval-is-moderately-evil.aspx</link><pubDate>Fri, 15 May 2009 05:04:36 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7087574</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>10</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7087574</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/05/14/setinterval-is-moderately-evil.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px 10px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="bleroy05" border="0" alt="bleroy05" align="left" src="http://weblogs.asp.net/blogs/bleroy/bleroy05_7555BBC5.jpg" width="244" height="164" /&gt; JavaScript has two ways of delaying execution of code: &lt;a href="http://msdn.microsoft.com/en-us/library/ms536749(VS.85).aspx"&gt;setInterval&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/ms536753(VS.85).aspx"&gt;setTimeout&lt;/a&gt;. Both take a function or a string as the first parameter, and a number of milliseconds as the second parameter. The only difference is that the code provided to setInterval will run every n milliseconds whereas the code in &lt;a href="http://msdn.microsoft.com/en-us/library/ms536753(VS.85).aspx"&gt;setTimeout&lt;/a&gt; will run only once.&lt;/p&gt;  &lt;p&gt;Before I explain why I think setInterval is evil, allow me to rant on a related subject for a paragraph: you should never pass a string into any of those functions and instead always pass a function reference (unless you really, really know what you’re doing). If you pass a string, it will have to be evaluated on the fly, and eval is quite evil itself (unless you really, really know what you’re doing). It might seem tempting to generate code this way to inject dynamic parameters, but there are better ways of doing that, using &lt;a href="http://en.wikipedia.org/wiki/Currying"&gt;currying&lt;/a&gt;. In Microsoft Ajax, for example, we provide the handy &lt;a href="http://weblogs.asp.net/bleroy/archive/2007/04/10/how-to-keep-some-context-attached-to-a-javascript-event-handler.aspx"&gt;Function.createCallback and Function.createDelegate&lt;/a&gt; for exactly that usage.&lt;/p&gt;  &lt;p&gt;Anyways now let’s get back to the issue at hand: setInterval is evil. To better compare, let’s assume you have a reason to use setInterval (why else would you be reading this?). Here’s the code you might write:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;!&lt;/span&gt;&lt;span style="color: #a31515"&gt;DOCTYPE &lt;/span&gt;&lt;span style="color: red"&gt;html&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;html&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;head&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;title&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;setInterval&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;title&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;script &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
        var &lt;/span&gt;interval = setInterval(appendDateToBody, 5000);&lt;br /&gt;&lt;span style="color: blue"&gt; &lt;br /&gt;        function &lt;/span&gt;appendDateToBody() {
            document.body.appendChild(
                document.createTextNode(&lt;span style="color: blue"&gt;new &lt;/span&gt;Date() + &lt;span style="color: #a31515"&gt;&amp;quot; &amp;quot;&lt;/span&gt;));
        }

        &lt;span style="color: blue"&gt;function &lt;/span&gt;stopInterval() {
            clearInterval(interval);
        }
    &lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;script&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;head&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;body&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;input &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;button&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;value&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Stop&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;onclick&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;stopInterval();&amp;quot; /&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;body&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;html&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Both APIs have a corresponding clear API that enables the developer to cancel the interval or timeout. To identify what interval or timeout you’re cancelling, you pass into the API a token that is whatever value the call to setInterval or setTimeout returned. And that’s a first reason why setInterval is (mildly) evil: if you lose that token, there is no way you can ever stop that code from running every five seconds until you navigate away from the page. But that is a minor inconvenience, it just means you need to carefully manage your own stuff, right? Well, actually if code that you don’t know or don’t control created that interval, you’re probably in trouble even if you kept your own house real nice and tidy…&lt;/p&gt;

&lt;p&gt;But the real reason why I dislike setInterval is that it is quite hard to debug, in particular if you have more than one running simultaneously. For example, if you have two intervals, one running every 100 milliseconds, the other every five seconds, and if you want to debug the second one, the first one will constantly get triggered and will get in the way.&lt;/p&gt;

&lt;p&gt;So what, you might say, is the alternative? Well, here is code that is quasi-equivalent to the code above, but that uses the much less evil setTimeout:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;interval = setTimeout(appendDateToBody, 5000);

&lt;span style="color: blue"&gt;function &lt;/span&gt;appendDateToBody() {
    document.body.appendChild(
        document.createTextNode(&lt;span style="color: blue"&gt;new &lt;/span&gt;Date() + &lt;span style="color: #a31515"&gt;&amp;quot; &amp;quot;&lt;/span&gt;));
    interval = setTimeout(appendDateToBody, 5000);
}

&lt;span style="color: blue"&gt;function &lt;/span&gt;stopInterval() {
    clearTimeout(interval);
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Here, instead of taking a subscription for life, we’re just renewing the lease as we go. The big advantage of this code is that to stop the interval, you don’t need the token. Actually, I rarely even bother to keep hold of it. All you have to do is skip the line of code that renews the timeout for the next iteration. So no housekeeping to do, and if during a debugging session you need to get one of the interval functions out of the way, just drag your debugger’s current execution pointer to the end of the function, hit F5 and you’ll never see that function run ever again, boom, it’s out of the way and you can focus on your own debugging.&lt;/p&gt;

&lt;p&gt;So to summarize, replacing setInterval with setTimeout is easy, pain-free and removes the inconveniences of setInterval. So while the setInterval evil is about at the level of not replacing the cap of the toothpaste, the non-evil alternative is so easy that I can’t see a reason not to forget about setInterval for good.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7087574" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category></item><item><title>New release of the Ajax Control Toolkit</title><link>http://weblogs.asp.net/bleroy/archive/2009/05/13/new-release-of-the-ajax-control-toolkit.aspx</link><pubDate>Thu, 14 May 2009 05:19:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7086151</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>50</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7086151</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/05/13/new-release-of-the-ajax-control-toolkit.aspx#comments</comments><description>&lt;p&gt;A &lt;a href="http://ajaxcontroltoolkit.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=27326"&gt;new version of the AJAX Control Toolkit&lt;/a&gt; is now available for download from the CodePlex website. It contains three new controls:&lt;/p&gt;  &lt;p style="clear: both"&gt;&lt;img style="border-right-width: 0px; margin: 0px 10px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="HtmlEditor" border="0" alt="HtmlEditor" align="left" src="http://weblogs.asp.net/blogs/bleroy/HtmlEditor_1E6A1BAC.png" width="244" height="159" /&gt; &lt;strong&gt;&lt;a href="http://www.asp.net/AJAX/AjaxControlToolkit/Samples/HTMLEditor/HTMLEditor.aspx"&gt;HTMLEditor&lt;/a&gt;&lt;/strong&gt; - allows you to easily create and edit HTML content. You can edit in WYSIWYG mode or in HTML source mode. The control exists as a server-side extender but can also be instantiated purely on the client-side with a single line of code. Many thanks to &lt;a href="http://www.obout.com"&gt;Obout&lt;/a&gt; for building this.&lt;/p&gt;  &lt;p style="clear: both"&gt;&lt;br/&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px; display: inline; border-top: 0px; border-right: 0px" title="ComboBox" border="0" alt="ComboBox" align="right" src="http://weblogs.asp.net/blogs/bleroy/ComboBox_0834005A.png" width="162" height="244" /&gt; &lt;strong&gt;&lt;a href="http://www.asp.net/AJAX/AjaxControlToolkit/Samples/ComboBox/ComboBox.aspx"&gt;ComboBox&lt;/a&gt;&lt;/strong&gt; - provides a DropDownList of items, combined with TextBox. Different modes determine the interplay between the text entry and the list of items. this control behaves very much like a Windows combo. Many thanks to Dan Ludwig for building this.&lt;/p&gt;  &lt;p style="clear: both"&gt;&lt;br/&gt;&lt;img style="border-right-width: 0px; margin: 0px 10px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="ColorPicker" border="0" alt="ColorPicker" align="left" src="http://weblogs.asp.net/blogs/bleroy/ColorPicker_23002666.png" width="238" height="189" /&gt; &lt;strong&gt;&lt;a href="http://www.asp.net/AJAX/AjaxControlToolkit/Samples/ColorPicker/ColorPicker.aspx"&gt;ColorPicker&lt;/a&gt;&lt;/strong&gt; - can be attached to any ASP.NET TextBox control to provide client-side color-picking functionality from a popup. Many thanks to Alexander Turlov for building this.&lt;/p&gt;  &lt;p style="clear: both"&gt;&lt;br/&gt;The ASP.NET website has been updated with new &lt;a href="http://www.asp.net/learn/ajax-videos"&gt;videos&lt;/a&gt; and &lt;a href="http://www.asp.net/learn/ajax"&gt;tutorials&lt;/a&gt; for these controls.&lt;/p&gt;  &lt;p&gt;This new release also includes fixes for over 20 of the most voted bugs in existing AJAX Control Toolkit controls and now features minimized release versions of the script files.&lt;/p&gt;  &lt;p&gt;The release can be downloaded as a server dll or as a set of files for use with pure client-side applications.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://ajaxcontroltoolkit.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=27326"&gt;http://ajaxcontroltoolkit.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=27326&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7086151" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/HTML/default.aspx">HTML</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Ajax+Control+Toolkit/default.aspx">Ajax Control Toolkit</category></item><item><title>Creating jQuery plug-ins from MicrosoftAjax components</title><link>http://weblogs.asp.net/bleroy/archive/2009/05/04/creating-jquery-plug-ins-from-microsoftajax-components.aspx</link><pubDate>Tue, 05 May 2009 05:23:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7073206</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>16</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7073206</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/05/04/creating-jquery-plug-ins-from-microsoftajax-components.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="(c) Bertrand Le Roy" border="0" alt="(c) Bertrand Le Roy" align="left" src="http://weblogs.asp.net/blogs/bleroy/ConjonctionRetouche_3337CDB8.jpg" width="244" height="156" /&gt; We had an interesting discussion recently on the &lt;a href="http://aspinsiders.com/"&gt;ASP Insiders&lt;/a&gt; mailing list and ended up talking about what cool stuff we could build on top of jQuery. Many interesting things were mentioned and it was a very useful discussion but one suggestion in particular struck my curiosity as it was something I had investigated before and that could be improved on with very little code.&lt;/p&gt;  &lt;p&gt;I had already written &lt;a href="http://weblogs.asp.net/bleroy/archive/2008/09/28/jquery-now-officially-part-of-the-net-developer-s-toolbox.aspx"&gt;a little plugin to enable instantiation of Microsoft Ajax components on the results of a jQuery selector&lt;/a&gt;:&lt;/p&gt;  &lt;pre class="code"&gt;jQuery.fn.create = &lt;span style="color: blue"&gt;function&lt;/span&gt;(type, properties) {
&lt;span style="color: blue"&gt;    return this&lt;/span&gt;.each(&lt;span style="color: blue"&gt;function&lt;/span&gt;() {
        Sys.Component.create(type, properties, {}, {},&lt;span style="color: blue"&gt; this&lt;/span&gt;);
    });
};&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;I have another version that is a little more elaborate and takes a bag of properties and events instead of just properties (get it from the attached sample) but you get the idea. This makes it fairly easy to instantiate components based on a selector:&lt;/p&gt;

&lt;pre class="code"&gt;$(&lt;span style="color: #a31515"&gt;&amp;quot;:text.nomorethanfive&amp;quot;&lt;/span&gt;)
    .create(Bleroy.Sample.CharCount, { maxLength: 5 });&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;But if this were a native jQuery plugin instead of a Microsoft Ajax component, as &lt;a href="http://west-wind.com/weblog/"&gt;Rick Strahl&lt;/a&gt; suggested, chances are you’d do something like this instead:&lt;/p&gt;

&lt;pre class="code"&gt;$(&lt;span style="color: #a31515"&gt;&amp;quot;:text.nomorethanfive&amp;quot;&lt;/span&gt;).charCount({ maxLength: 5 });&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;We can actually get that exact result fairly easily by writing a plugin that is specific to this component, but we can’t expect every Microsoft Ajax component author to also write a jQuery plugin, can we? We can do better than that. Here is a second small piece of code that is still generic but is in the business of &lt;em&gt;creating jQuery plugins&lt;/em&gt;:&lt;/p&gt;

&lt;pre class="code"&gt;Sys.Component.exposeTojQuery = &lt;span style="color: blue"&gt;function&lt;/span&gt;(type, pluginName) {
    &lt;span style="color: blue"&gt;return &lt;/span&gt;jQuery.fn[pluginName] = &lt;span style="color: blue"&gt;function&lt;/span&gt;(properties) {
        &lt;span style="color: blue"&gt;return this&lt;/span&gt;.each(&lt;span style="color: blue"&gt;function&lt;/span&gt;() {
            Sys.Component.create(type, properties, {}, {}, &lt;span style="color: blue"&gt;this&lt;/span&gt;);
        });
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Like above, the version in the sample is better than that in that it supports events in addition to properties but again you get the idea. Thanks to this function, we can create a jQuery plugin for any existing Microsoft Ajax component with a single line of code:&lt;/p&gt;

&lt;pre class="code"&gt;Sys.Component.exposeTojQuery(Bleroy.Sample.CharCount, &lt;span style="color: #a31515"&gt;&amp;quot;charCount&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;After this call, we can write exactly the code we wrote before, but without having had to write a specific plugin. We can then use charCount like a native jQuery plugin:&lt;/p&gt;

&lt;pre class="code"&gt;$(&lt;span style="color: #a31515"&gt;&amp;quot;:text.nomorethanfive&amp;quot;&lt;/span&gt;).charCount({ maxLength: 5 });&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This is really easy to use, and I quite like it. We could for example add the following line of code to the new MicrosoftAjaxTemplates.js:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;if &lt;/span&gt;(jQuery)&lt;br /&gt;    Sys.Component.exposeTojQuery(Sys.UI.DataView, &lt;span style="color: #a31515"&gt;&amp;quot;dataView&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This would enable us to instantiate DataView controls as simply as this:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;ul &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;dv&amp;quot;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;li&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;{{ $dataItem }}&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;li&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;ul&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;script &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
&lt;/span&gt;    $(&lt;span style="color: #a31515"&gt;&amp;quot;.dv&amp;quot;&lt;/span&gt;).dataView({ data: [&lt;span style="color: #a31515"&gt;&amp;quot;foo&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;bar&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;baz&amp;quot;&lt;/span&gt;] });
&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;script&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Get the code from here (you need to include &lt;a href="http://docs.jquery.com/Downloading_jQuery"&gt;jquery-1.3.2.js&lt;/a&gt; to the script folder):

  &lt;br /&gt;&lt;a title="http://weblogs.asp.net/blogs/bleroy/Samples/jQueryCreate.zip" href="http://weblogs.asp.net/blogs/bleroy/Samples/jQueryCreate.zip"&gt;http://weblogs.asp.net/blogs/bleroy/Samples/jQueryCreate.zip&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7073206" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/jQuery/default.aspx">jQuery</category></item><item><title>A blog on Microsoft Ajax client templates and data</title><link>http://weblogs.asp.net/bleroy/archive/2009/04/21/a-blog-on-microsoft-ajax-client-templates-and-data.aspx</link><pubDate>Tue, 21 Apr 2009 21:45:01 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7054330</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=7054330</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/04/21/a-blog-on-microsoft-ajax-client-templates-and-data.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://politian.wordpress.com/"&gt;Politian&lt;/a&gt; has a &lt;a href="http://politian.wordpress.com/"&gt;great blog series&lt;/a&gt; where he goes into the details of building a data-driven Ajax application using &lt;a href="http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24645"&gt;the new 4.0 client templates and data&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Check it out!   &lt;br /&gt;&lt;a href="http://politian.wordpress.com/"&gt;http://politian.wordpress.com/&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7054330" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category></item><item><title>Some MIX talks</title><link>http://weblogs.asp.net/bleroy/archive/2009/03/22/some-mix-talks.aspx</link><pubDate>Mon, 23 Mar 2009 04:14:22 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6994071</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=6994071</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/03/22/some-mix-talks.aspx#comments</comments><description>&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 15px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="(c) 2005 Bertrand Le Roy" border="0" alt="(c) 2005 Bertrand Le Roy" align="left" src="http://weblogs.asp.net/blogs/bleroy/bleroy04_2F523812.jpg" width="244" height="164" /&gt; Stephen Walther just published links to the video, slides and sample code for his Ajax talk at MIX09:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://stephenwalther.com/blog/archive/2009/03/22/mix-slides-code-and-session-recording.aspx"&gt;http://stephenwalther.com/blog/archive/2009/03/22/mix-slides-code-and-session-recording.aspx&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;It’s pretty cool to see all the work we put into Ajax this past year or so presented at MIX. This is a really nice presentation, like Stephen’s always are.&lt;/p&gt;  &lt;p&gt;Another presentation I had lots of fun watching (not just because the speaker is making an incredible impression of me but also because I’ve been spending a good part of my time lately contributing to the application he’s showing) is Rob Conery’s. Rob is showing an interesting way to develop ASP.NET applications, aimed at ease of use and customization rather than architectural purity. Check it out, let me know what you think…&lt;/p&gt;  &lt;p&gt;&lt;a href="http://videos.visitmix.com/MIX09/T62F"&gt;http://videos.visitmix.com/MIX09/T62F&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6994071" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/MVC/default.aspx">MVC</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/ADO.NET+Data+Services/default.aspx">ADO.NET Data Services</category></item><item><title>Microsoft Ajax 4.0 Preview 4 now available</title><link>http://weblogs.asp.net/bleroy/archive/2009/03/18/microsoft-ajax-4-0-preview-4-now-available.aspx</link><pubDate>Wed, 18 Mar 2009 07:49:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6973656</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>16</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=6973656</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/03/18/microsoft-ajax-4-0-preview-4-now-available.aspx#comments</comments><description>&lt;p&gt;The Microsoft Ajax team made the &lt;a href="http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24645"&gt;fourth preview of the 4.0 version available on CodePlex&lt;/a&gt;. This is an important release because it enables the full client data story, complete with the ability to get changes back to the server automatically.&lt;/p&gt;  &lt;p&gt;Here’s a quick recap of some of the available features:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Getting a &lt;strong&gt;client representation of data&lt;/strong&gt; from an ADO.NET and REST data service.&lt;/li&gt;    &lt;li&gt;Rendering data on the client using &lt;strong&gt;templates&lt;/strong&gt;.&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Declarative instantiation&lt;/strong&gt; of client components.&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Live bindings&lt;/strong&gt;, enabling changes in the UI and in the data to be automatically propagated.&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Command bubbling&lt;/strong&gt; for codeless wiring of events in template-driven controls.&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Data identity&lt;/strong&gt; and association management for efficient and consistent client-server data exchanges.&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Sending changes back&lt;/strong&gt; to ADO.NET and REST data services.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;In a nutshell, it is probably the easiest way to build a data-driven client application. Check this out:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;div &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;peopleView&amp;quot; &lt;/span&gt;&lt;span style="color: #a31515"&gt;sys&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;attach&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;dataview&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;sys-template&amp;quot;
     &lt;/span&gt;&lt;span style="color: #a31515"&gt;dataview&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;dataprovider&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{{ $create(Sys.Data.AdoNetDataContext,
                               {serviceUri: 'PeopleIKnow.svc'})}}&amp;quot;
     &lt;/span&gt;&lt;span style="color: #a31515"&gt;dataview&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;fetchoperation&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;PeopleIKnow&amp;quot;
     &lt;/span&gt;&lt;span style="color: #a31515"&gt;dataview&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;autofetch&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;true&amp;quot;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;fieldset&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;legend&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
            &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;span&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;{binding FirstName}&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;span&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
            &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;span&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;{binding LastName}&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;span&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;legend&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;img code&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;if&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{{ Photo }}&amp;quot;
             &lt;/span&gt;&lt;span style="color: #a31515"&gt;sys&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;src&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{{ 'Images/' + Photo }}&amp;quot;
             &lt;/span&gt;&lt;span style="color: red"&gt;alt&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{{ FirstName + ' ' + LastName }}&amp;quot; /&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;br &lt;/span&gt;&lt;span style="color: blue"&gt;/&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;input &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;text&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{{ $id('firstName') }}&amp;quot;
               &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;editInPlace name&amp;quot;
               &lt;/span&gt;&lt;span style="color: red"&gt;value&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{binding FirstName}&amp;quot;
               &lt;/span&gt;&lt;span style="color: #a31515"&gt;sys&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;attach&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;inplace&amp;quot; &lt;/span&gt;&lt;span style="color: #a31515"&gt;inplace&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;cssclass&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;editing&amp;quot;/&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;input &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;text&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{{ $id('lastName') }}&amp;quot;
               &lt;/span&gt;&lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;editInPlace name&amp;quot;
               &lt;/span&gt;&lt;span style="color: red"&gt;value&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;{binding LastName}&amp;quot;
               &lt;/span&gt;&lt;span style="color: #a31515"&gt;sys&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;attach&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;inplace&amp;quot; &lt;/span&gt;&lt;span style="color: #a31515"&gt;inplace&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;cssclass&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;editing&amp;quot;/&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;fieldset&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;div&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;br &lt;/span&gt;&lt;span style="color: blue"&gt;/&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;input &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;saveButton&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;type&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;button&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;value&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Save&amp;quot; /&amp;gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This creates a DataView that queries the PeopleIKnow.svc ADO.NET data service, and repeats the markup in the div over the data. The legend contains two spans that will respond to live changes to the data ({binding FirstName and {binding LastName}). The image will only be rendered if there is a photo to show (code:if). It builds the image path using a simple JavaScript expression: {{ ‘Images/’ + Photo }}.&lt;/p&gt;

&lt;p&gt;The two input tags are augmented by a custom &lt;a href="http://weblogs.asp.net/bleroy/archive/2008/11/20/building-a-neat-edit-in-place-behavior.aspx"&gt;edit&lt;/a&gt; &lt;a href="http://weblogs.asp.net/bleroy/archive/2008/11/24/simplifying-the-edit-in-place-behavior.aspx"&gt;in place&lt;/a&gt; behavior (sys:attach=”inplace”) and are bound to the FirstName and LastName data columns so that any change to the value of the field will be propagated to everything that depends on the same data: the data itself of course but also the legend of the fieldset (see video below).&lt;/p&gt;

&lt;p&gt;The save button is hooked to the following handler:&lt;/p&gt;

&lt;pre class="code"&gt;$addHandler($get(&lt;span style="color: #a31515"&gt;&amp;quot;saveButton&amp;quot;&lt;/span&gt;), &lt;span style="color: #a31515"&gt;&amp;quot;click&amp;quot;&lt;/span&gt;, &lt;span style="color: blue"&gt;function&lt;/span&gt;() {
    $find(&lt;span style="color: #a31515"&gt;&amp;quot;peopleView&amp;quot;&lt;/span&gt;).get_dataProvider().saveChanges();
}, &lt;span style="color: blue"&gt;true&lt;/span&gt;);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This handler is super-simple as it only has to call saveChanges on the data provider of the DataView. This is enough because any changes made in the input fields have been propagated to the client data model already, which tracked all changes and can build a simple JSON object to send back to the data service. Here is an example of the kind of JSON object that travels back to the server after I’ve changed Simon’s name through the UI:&lt;/p&gt;

&lt;pre&gt;{&lt;br /&gt;  &amp;quot;__metadata&amp;quot;:{&lt;br /&gt;    &amp;quot;uri&amp;quot;:&amp;quot;http://127.0.0.1:26402/Asp.Net_Ajax_Preview_4/&lt;br /&gt;           PeopleIKnow.svc/PeopleIKnow(1)&amp;quot;,&lt;br /&gt;    &amp;quot;type&amp;quot;:&amp;quot;PeopleIKnowModel.PeopleIKnow&amp;quot;&lt;br /&gt;  },&lt;br /&gt;  &amp;quot;ID&amp;quot;:1,&lt;br /&gt;  &amp;quot;FirstName&amp;quot;:&amp;quot;Simon&amp;quot;,&lt;br /&gt;  &amp;quot;LastName&amp;quot;:&amp;quot;Calvert&amp;quot;,&lt;br /&gt;  &amp;quot;Photo&amp;quot;:&amp;quot;simoncal.jpg&amp;quot;
}&lt;/pre&gt;

&lt;p&gt;Here’s the application running:&lt;/p&gt;

&lt;embed src="http://images.video.msn.com/flash/soapbox1_1.swf" width="432" height="364" id="m6a75m3o" type="application/x-shockwave-flash" allowFullScreen="true" allowScriptAccess="always" pluginspage="http://macromedia.com/go/getflashplayer" flashvars="c=v&amp;v=140e8d2a-8d01-49ab-b96f-77e219f93e40&amp;ifs=true&amp;fr=msnvideo&amp;mkt=en-US"&gt;&lt;/embed&gt;&lt;noembed&gt;&lt;br/&gt;&lt;a href="http://video.msn.com/video.aspx?vid=140e8d2a-8d01-49ab-b96f-77e219f93e40" target="_new" title="ASP.NET Ajax 4.0 Preview 4 "&gt;Video: ASP.NET Ajax 4.0 Preview 4 &lt;/a&gt;&lt;/noembed&gt;

&lt;p&gt;The source code can be downloaded from here (contains code licensed under &lt;a href="http://opensource.org/licenses/ms-pl.html"&gt;MS-PL&lt;/a&gt;):

  &lt;br /&gt;&lt;a title="http://weblogs.asp.net/blogs/bleroy/Samples/Asp.Net_Ajax_Preview_4.zip" href="http://weblogs.asp.net/blogs/bleroy/Samples/Asp.Net_Ajax_Preview_4.zip"&gt;http://weblogs.asp.net/blogs/bleroy/Samples/Asp.Net_Ajax_Preview_4.zip&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But wait, there’s more in stock for the next preview… I’ll post more details about some of those features in the following weeks… Stay tuned…&lt;/p&gt;

&lt;p&gt;&lt;a href="http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24645"&gt;http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24645&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6973656" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/HTML/default.aspx">HTML</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JSON/default.aspx">JSON</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/ADO.NET+Data+Services/default.aspx">ADO.NET Data Services</category></item><item><title>How to choose a client template engine</title><link>http://weblogs.asp.net/bleroy/archive/2009/02/05/how-to-choose-a-client-template-engine.aspx</link><pubDate>Thu, 05 Feb 2009 08:03:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6884165</guid><dc:creator>Bertrand Le Roy</dc:creator><slash:comments>12</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/bleroy/rsscomments.aspx?PostID=6884165</wfw:commentRss><comments>http://weblogs.asp.net/bleroy/archive/2009/02/05/how-to-choose-a-client-template-engine.aspx#comments</comments><description>&lt;p&gt;&lt;strong&gt;&lt;img style="border-right-width: 0px; margin: 0px 10px 0px 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Water lenses, (c) 2007 Bertrand Le Roy" border="0" alt="Water lenses, (c) 2007 Bertrand Le Roy" align="left" src="http://weblogs.asp.net/blogs/bleroy/WaterLenses_40562071.jpg" width="244" height="164" /&gt; Disclaimer:&lt;/strong&gt; I worked on the &lt;a href="http://www.codeplex.com/aspnet/Wiki/View.aspx?title=AJAX"&gt;Microsoft Ajax 4.0&lt;/a&gt; template engine, so my criteria are of course heavily influenced by our own design.&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;Templates are a data rendering method that server-side developers have enjoyed since the old days of classic ASP and PHP. The idea was quite simple (add code blocks and dynamic expressions directly into HTML markup) but it revolutionized web development, which before that relied on the opposite method (spitting HTML from CGI code).&lt;/p&gt;  &lt;p&gt;On the client-side, the browser provides two ways to generate HTML: innerHTML and the DOM API. Template rendering is of course possible, but only using a JavaScript library. To be honest, one should mention XSLT here, which is standard and widely supported but whose somewhat unusual syntax has had limited success convincing web developers.&lt;/p&gt;  &lt;p&gt;There are literally dozens of template engines available today, using many different markup conventions and algorithms. Template engines are not equal, they have different levels of complexity, different feature sets, different security models. It can be quite hard to pick one, and it really depends on the requirements of your application and also on taste.&lt;/p&gt;  &lt;p&gt;I’ll try to go over a few things that you might want to check on a template engine. You will want to cherry-pick what’s important to you and what’s not so that you can pick the tool that’s best adapted to your problem.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;1. Expression delimiter&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;A template system is adding new semantics to an existing language. This is not completely harmless as you have to give a new meaning to sequences of characters that were understood before as plain literals. That means that an escaping mechanism must exist to express the literal contents that were given a new meaning. For example, if the expression delimiters are &amp;lt;%= and %&amp;gt;, how do you express the “&amp;lt;%=” and “%&amp;gt;” sequences of characters as literals?&lt;/p&gt;  &lt;p&gt;&amp;lt;%= and %&amp;gt; are by far the most common and familiar delimiters (they have been used by ASP, PHP, JSP, ASP.NET and many others), but they have one flaw that couldn’t be seen back in 1996: XHTML compliance. Granted, you may not care at all about XHTML. If you don’t, just skip this. But if you do, any template engine that uses &amp;lt;%= %&amp;gt; simply can’t have template code that is XHTML-compliant. Now this may still not be a big deal if you don’t care that the template code is compliant, but only care about the generated markup.&lt;/p&gt;  &lt;p&gt;But there is another reason why &amp;lt;%= %&amp;gt; is a bad choice: it’s conflicting with the server-side. A client-side engine must be able to coexist peacefully with whatever server-side technology you’re using. If your server technology of choice is using &amp;lt;%= %&amp;gt; like it very likely does, it will be conflicting with your client templates. The server-side engine will kick in when it sees these delimiters and will fail one way or another.&lt;/p&gt;  &lt;p&gt;We chose {{ }} as the expression delimiter (and also {expr } for extensible markup expressions such as {binding }), which happens to be the same choice that Django made (and Dojo, as they implement Django on&amp;#160; the client-side, and by the way I don’t know how they manage conflict with server-side Django; if you know, drop me a comment). It works fine with XHTML and it doesn’t conflict with ASP or PHP. If you want to express the “{{“ literal, we don’t provide an escape sequence per se, but you can still do it by writing {{ “{{“ }}.&lt;/p&gt;  &lt;p&gt;On a final note, there are a few template engines that do not have expression delimiters but instead rely on microformats. While this absolutely preserves the HTML semantics, it’s often clumsier to use and quite limiting. It’s a matter of taste, but microformats also can look like markup within markup.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;2. Expression language&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;In the simplest cases, you’ll want to inject a simple data field into the template, like {{ Price }}. But many times, you’ll want to go beyond those simple cases and embed a more complex expression into the markup. For example, you might want to display that price with two decimal places, like $42.00. In order to handle that case and more complex ones, it is necessary to have a full expression language.&lt;/p&gt;  &lt;p&gt;In the same way, if the template engine allows for code blocks to introduce control structures such as conditional execution or loops, it needs a full programming language.&lt;/p&gt;  &lt;p&gt;Some template engines, such as Django, introduce their own new language. Others, such as ours, just use what’s already available and familiar: JavaScript. In addition to being familiar, it eliminates the need to write a parser and interpreter, which reduces library code size and gives better performance.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;3. Protection against injection attacks&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;If you don’t know what an injection attack is… wow. Just wow. Go read &lt;a href="http://weblogs.asp.net/bleroy/archive/2004/08/18/please-please-please-learn-about-injection-attacks.aspx"&gt;this&lt;/a&gt;, now. Seriously.     &lt;br /&gt;&lt;a href="http://weblogs.asp.net/bleroy/archive/2004/08/18/please-please-please-learn-about-injection-attacks.aspx"&gt;http://weblogs.asp.net/bleroy/archive/2004/08/18/please-please-please-learn-about-injection-attacks.aspx&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The frightening thing about almost any single one of the client template engines out there is that they have no mechanism in place to protect against injection attacks, which puts the responsibility to encode and check contents in the hands of the application developer. That’s you.&lt;/p&gt;  &lt;p&gt;Most of the engines out there are using a very simple algorithm to build HTML: array joining, and then injecting the result using innerHTML. This is very similar to building SQL by concatenating strings: in a nutshell, you shouldn’t do that. In the same way that SQL is better built using parameterized queries, HTML is more securely built using the DOM API which just eliminates the need for encoding.&lt;/p&gt;  &lt;p&gt;Even when using encoding or the DOM API, there is a number of attributes that still present a fair amount of danger if user data gets injected in them. For example, if you bind the href attribute of a link to a piece of user data, no amount of encoding will protect the application against a user injecting “javascript:doSomeEvil();”.&lt;/p&gt;  &lt;p&gt;To prevent that kind of attack, we white-list the protocols in all known URL attributes to relative URLs, http and https. It is possible for the application to extend this white list as needed (available in Microsoft Ajax 4.0 Preview 4 and later).&lt;/p&gt;  &lt;p&gt;One thing to understand is that the dangerous contents here is the &lt;em&gt;data&lt;/em&gt;, not the template itself, which is trusted as application code is.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;4. Template location&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Depending on the library, the template can be embedded in comments, in script tags or just be part of the DOM. It can also be included from a separate file.&lt;/p&gt;  &lt;p&gt;Putting the template in comments or script tags easily hides its markup from initial rendering, but it makes it harder to design with existing tools and puts it out of the reach of markup validation tools.&lt;/p&gt;  &lt;p&gt;Including from a separate file adds one more request to the server, which might be a problem or not: it might also improve performance by allowing partial caching. The best is to have a choice here, so I’d reject an engine where external files is the only possibility, knowing that all other engines allow external files with minimal effort by feeding a simple XHR’s result into the system.&lt;/p&gt;  &lt;p&gt;Instead of using script tags or comments, we chose to embed the template as real HTML contents anywhere in the document and just hide it from initial rendering using CSS. This makes the code designable using any existing tool, and enables us to use the browser’s native HTML parser to understand the structure of the markup.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;5. Hooking up events and instantiating components&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;In a modern Web application, you won’t just create plain HTML, you’ll want to hook up events and instantiate components. The template engine might let you do this inline, which is obtrusive but convenient, or it might require you to enrich the DOM after template instantiation through code, or it might give you a choice between the two.&lt;/p&gt;  &lt;p&gt;One thing to look for is how much knowledge of the template markup’s shape must be injected into the code in order to enrich the DOM.&lt;/p&gt;  &lt;p&gt;For a picture of the different possibilities using our engine, read this post:    &lt;br /&gt;&lt;a href="http://weblogs.asp.net/bleroy/archive/2008/11/28/instantiating-components-on-template-markup.aspx"&gt;http://weblogs.asp.net/bleroy/archive/2008/11/28/instantiating-components-on-template-markup.aspx&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;6. Live bindings&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;One big advantage of client rendering is that the data remains under raw data form until it reaches the template engine. That means that you have a representation of the data on the client. This in turn means there is an opportunity to monitor changes to the data and to bind UI directly to it, eliminating round-trips to the server along the way.&lt;/p&gt;  &lt;p&gt;We call this ability live bindings and it makes for very responsive applications that are still easy to build.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;7. Constraints on markup shape&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;One thing you should try with any template engine is to repeat a row in a table, using IE. This may sound trivial, but innerHTML doesn’t work on IE in a number of places, which puts a constraint on the markup that you need to write to render certain shapes of HTML. There is almost always a workaround but it can be more or less painful to implement. Try and compare, see what you find reasonable.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;8. Ease of data field access&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;This one is quite simple. Some template engines force you to access the current data item’s field through some special syntax such as “$T.price”. In our engine and several others, you just write “price”.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;9. Readability&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;This one is quite subjective, but when choosing a template engine, just look at a few samples. Does the code hurt your eyes? Can you understand it right away?&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;10. Performance&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;A template engine must be able to render a reasonable number of data items with a reasonably complex template under the perception threshold. What’s “reasonable” pretty much depends on your application design, but I’d say a hundred items in a typical grid layout should be hardly noticeable. So before you choose, do a mock-up of your design and data and try it. Increase the number of items and see where rendering starts to get noticeable. Usually rendering performance is not linear so this should give you a good idea of how far you can go with each engine. Better to determine that before you invest heavily in one engine.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;11. XHTML compliance&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;As I said above, this is one you might not care about. Don’t leave yet, it actually goes both ways, as an engine, by enforcing XHTML compliance, might potentially get in the way of writing HTML the way you want.&lt;/p&gt;  &lt;p&gt;The lowest level of compliance is that the rendered output is compliant. Being template engines, most if not all engines enable you to create compliant or horribly broken markup, depending on your own style. What it must absolutely not do is &lt;em&gt;prevent&lt;/em&gt; you from generating compliant markup.&lt;/p&gt;  &lt;p&gt;The second level of compliance is that the template code itself, before data is injected, is compliant. This is much less common. Even less common is that the compliance requirement does not impose too strong a constraint on the markup you have to write.&lt;/p&gt;  &lt;p&gt;In other words, a good template engine lets you write XHTML but doesn’t force you to.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;12. Nested templates&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Rendering a template within a template might look like a whacky scenario at first if the engine understands control structures such as loops. In reality, if you include something like live bindings into the mix, you will want for example an inner template to be re-drawn when the outer data item changes. This will be much easier to achieve with nested templates.&lt;/p&gt;  &lt;p&gt;So if this is a scenario that you care about, make sure that your engine handles that case gracefully.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In conclusion&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;There are many engines to choose from, and many criteria to check. I’m quite convinced that &lt;a href="http://www.codeplex.com/aspnet/Wiki/View.aspx?title=AJAX"&gt;our own offering is pretty solid&lt;/a&gt; for all these criteria and offers a good compromise of features against simplicity and performance.&lt;/p&gt;  &lt;p&gt;There is another engine that I quite like because it’s an impressively small bit of code that remains useful, and that is John Resig’s micro-template engine, which is only 20 lines of code. Like our own engine, it “compiles” the template into a reusable function that takes the data as a parameter. But unlike ours, it has no security mechanism and has issues with some markup, which may or may not be an issue for you. It’s a different kind of compromise that puts code size and simplicity before any other consideration.&lt;/p&gt;  &lt;p&gt;I hope this helps in making a choice.&lt;/p&gt;  &lt;p&gt;A good resource on client templates:    &lt;br /&gt;&lt;a href="http://ajaxpatterns.org/Browser-Side_Templating"&gt;http://ajaxpatterns.org/Browser-Side_Templating&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;John Resig’s micro-templates:    &lt;br /&gt;&lt;a href="http://ejohn.org/blog/javascript-micro-templating/"&gt;http://ejohn.org/blog/javascript-micro-templating/&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;ASP.NET 4.0 Ajax Preview 3:    &lt;br /&gt;&lt;a title="http://www.codeplex.com/aspnet/Wiki/View.aspx?title=AJAX" href="http://www.codeplex.com/aspnet/Wiki/View.aspx?title=AJAX"&gt;http://www.codeplex.com/aspnet/Wiki/View.aspx?title=AJAX&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;UPDATE: &lt;/strong&gt;added performance and XHTML compliance to the list from Dave Reed’s feedback, and nested templates from Keith’s.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6884165" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/bleroy/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Atlas/default.aspx">Atlas</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Microsoft+AJAX+Library/default.aspx">Microsoft AJAX Library</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/HTML/default.aspx">HTML</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Internet+Explorer/default.aspx">Internet Explorer</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Security/default.aspx">Security</category><category domain="http://weblogs.asp.net/bleroy/archive/tags/Injection/default.aspx">Injection</category></item></channel></rss>