<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" gd:etag="W/&quot;AkMGRX87cCp7ImA9WhBbEUU.&quot;"><id>tag:blogger.com,1999:blog-6778277710194170777</id><updated>2013-05-10T14:20:24.108+02:00</updated><category term="Rants" /><category term="Tip" /><category term="WPF" /><category term="Announcement" /><category term="ASP.NET Webforms" /><category term="ASP.NET MVC" /><category term="Silverlight" /><title>typps</title><subtitle type="html" /><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://blog.typps.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://blog.typps.com/" /><link rel="next" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default?start-index=26&amp;max-results=25&amp;redirect=false&amp;v=2" /><author><name>Alessandro</name><uri>http://www.blogger.com/profile/08620937587114860179</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="24" src="http://4.bp.blogspot.com/-9iFBqNPDD_E/TbtHLjeMgZI/AAAAAAAAAK0/9WMh7OUK6KA/s220/alessandro.png" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>32</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/typps/axAH" /><feedburner:info uri="typps/axah" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry gd:etag="W/&quot;CEcFSX49eCp7ImA9WhNTGUk.&quot;"><id>tag:blogger.com,1999:blog-6778277710194170777.post-5969209693041938566</id><published>2012-09-29T12:35:00.002+02:00</published><updated>2012-10-22T23:40:18.060+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-10-22T23:40:18.060+02:00</app:edited><title>Backbone.js View Events not Firing?</title><content type="html">&lt;br /&gt;
In backbone, setting the el element in the view's initialize method doesn't ensure that the events are bound correctly. Apparently the events are wired only when the el element is set via the views constructor as an option, which is not always what you'd like to do.&lt;br /&gt;
&lt;br /&gt;
To workaround, if your setting the el element in the initialize method of the view, then make sure you call this._ensureElement() right after.&lt;br /&gt;
&lt;br /&gt;
_ensureElement() methods description says this :&lt;br /&gt;
&lt;br /&gt;
Ensure that the View has a DOM element to render into.&amp;nbsp;If `this.el` is a string, pass it through `$()`, take the first &amp;nbsp;matching element, and re-assign it to `el`. Otherwise, create&amp;nbsp;an element from the `id`, `className` and `tagName` properties.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; padding: 0px 5px;"&gt;
&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;
&lt;pre style="margin: 0px;"&gt;&lt;span style="color: blue; font-family: 'courier new'; font-size: 14px;"&gt;var&lt;/span&gt;&lt;span style="color: black; font-family: 'courier new'; font-size: 14px;"&gt; MyView = Backbone.View.extend({
       'events': {
                'click .next': 'next',
                'click .cancel': 'cancel'
        },
  
        'initialize': &lt;/span&gt;&lt;span style="color: blue; font-family: 'courier new'; font-size: 14px;"&gt;function&lt;/span&gt;&lt;span style="color: black; font-family: 'courier new'; font-size: 14px;"&gt;() {
            &lt;/span&gt;&lt;span style="color: blue; font-family: 'courier new'; font-size: 14px;"&gt;this&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;span style="font-family: courier new;"&gt;&lt;span style="font-size: 14px;"&gt;.el = &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 14px;"&gt;$('#step1')&lt;/span&gt;&lt;span style="font-family: courier new;"&gt;&lt;span style="font-size: 14px;"&gt;;
            &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="background-color: #85f1ff; background-position: initial initial; background-repeat: initial initial; font-family: 'courier new'; font-size: 14px;"&gt;&lt;span style="color: blue;"&gt;this&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black; font-family: 'courier new'; font-size: 14px;"&gt;&lt;span style="background: #85F1FF;"&gt;._ensureElement();&lt;/span&gt;
            _.bindAll(&lt;/span&gt;&lt;span style="color: blue; font-family: 'courier new'; font-size: 14px;"&gt;this&lt;/span&gt;&lt;span style="color: black; font-family: 'courier new'; font-size: 14px;"&gt;, 'render');
             &lt;/span&gt;&lt;span style="color: blue; font-family: 'courier new'; font-size: 14px;"&gt;this&lt;/span&gt;&lt;span style="color: black; font-family: 'courier new'; font-size: 14px;"&gt;.template = _.template($('#step1-template').html());
            &lt;/span&gt;&lt;span style="color: blue; font-family: 'courier new'; font-size: 14px;"&gt;this&lt;/span&gt;&lt;span style="color: black; font-family: 'courier new'; font-size: 14px;"&gt;.render();
        },
        
        'render': &lt;/span&gt;&lt;span style="color: blue; font-family: 'courier new'; font-size: 14px;"&gt;function&lt;/span&gt;&lt;span style="color: black; font-family: 'courier new'; font-size: 14px;"&gt;() {
            $(&lt;/span&gt;&lt;span style="color: blue; font-family: 'courier new'; font-size: 14px;"&gt;this&lt;/span&gt;&lt;span style="color: black; font-family: 'courier new'; font-size: 14px;"&gt;.el).html(&lt;/span&gt;&lt;span style="color: blue; font-family: 'courier new'; font-size: 14px;"&gt;this&lt;/span&gt;&lt;span style="color: black; font-family: 'courier new'; font-size: 14px;"&gt;.template(&lt;/span&gt;&lt;span style="color: blue; font-family: 'courier new'; font-size: 14px;"&gt;this&lt;/span&gt;&lt;span style="color: black; font-family: 'courier new'; font-size: 14px;"&gt;.model.toJSON()));
            &lt;/span&gt;&lt;span style="color: blue; font-family: 'courier new'; font-size: 14px;"&gt;return&lt;/span&gt;&lt;span style="color: black; font-family: 'courier new'; font-size: 14px;"&gt; &lt;/span&gt;&lt;span style="color: blue; font-family: 'courier new'; font-size: 14px;"&gt;this&lt;/span&gt;&lt;span style="color: black; font-family: 'courier new'; font-size: 14px;"&gt;;
        }, 

       'next': &lt;/span&gt;&lt;span style="color: blue; font-family: 'courier new'; font-size: 14px;"&gt;function&lt;/span&gt;&lt;span style="color: black; font-family: 'courier new'; font-size: 14px;"&gt;(){
           &lt;/span&gt;&lt;span style="color: blue; font-family: 'courier new'; font-size: 14px;"&gt;window&lt;/span&gt;&lt;span style="color: black; font-family: 'courier new'; font-size: 14px;"&gt;.console.log('next');
       },
  
       'cancel': &lt;/span&gt;&lt;span style="color: blue; font-family: 'courier new'; font-size: 14px;"&gt;function&lt;/span&gt;&lt;span style="color: black; font-family: 'courier new'; font-size: 14px;"&gt;(){
            &lt;/span&gt;&lt;span style="color: blue; font-family: 'courier new'; font-size: 14px;"&gt;window&lt;/span&gt;&lt;span style="color: black; font-family: 'courier new'; font-size: 14px;"&gt;.console.log('cancel');
       }
 });&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div style="clear: both; font-family: 'courier new'; font-size: 14px;"&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;img src="http://feeds.feedburner.com/~r/typps/axAH/~4/7XB4EUWNXe4" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.typps.com/feeds/5969209693041938566/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.typps.com/2012/09/backbonejs-view-events-not-firing.html#comment-form" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/5969209693041938566?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/5969209693041938566?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typps/axAH/~3/7XB4EUWNXe4/backbonejs-view-events-not-firing.html" title="Backbone.js View Events not Firing?" /><author><name>Alessandro</name><uri>http://www.blogger.com/profile/08620937587114860179</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="24" src="http://4.bp.blogspot.com/-9iFBqNPDD_E/TbtHLjeMgZI/AAAAAAAAAK0/9WMh7OUK6KA/s220/alessandro.png" /></author><thr:total>1</thr:total><feedburner:origLink>http://blog.typps.com/2012/09/backbonejs-view-events-not-firing.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DE4MRH04fSp7ImA9WhdbGE4.&quot;"><id>tag:blogger.com,1999:blog-6778277710194170777.post-6166362986796929244</id><published>2011-10-16T13:48:00.000+02:00</published><updated>2011-10-17T09:36:25.335+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-10-17T09:36:25.335+02:00</app:edited><title>An Ant process driven build for Javascript --Make your own Javascript IDE</title><content type="html">What we are going to setup is an IDE that can assemble Javascript code spread out in multiple script files into a single file (mimicking&amp;nbsp;physically structured class files found in class based languages),compile the code getting&amp;nbsp;useful warnings and errors in the process, minify the code, create documentation via jsdocs and zip+deploy . All through a build script that can be called through the push of a button F6 (Just like in visual studio).&lt;br /&gt;
&lt;br /&gt;
First a quick preview of what we are building :
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-5AekQGTzXwk/Tpvar4xiVsI/AAAAAAAAAOI/EWiK_Ch_H2Q/s1600/parametertypeerror.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="640" src="http://3.bp.blogspot.com/-5AekQGTzXwk/Tpvar4xiVsI/AAAAAAAAAOI/EWiK_Ch_H2Q/s640/parametertypeerror.jpg" width="628" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
As  you can note we used jsdocs annotations to tell the compiler what type the parameter is since javascript does not have support for declaring types like in strongly typed languages. These annotations are stripped out during minification so we don't incur the cost of verbosity in our code. We simply compile the code and dutifully get the above warning.&lt;br /&gt;
&lt;br /&gt;
Another quick example :&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-RzYFX6s_Wag/Tpmdqv35QTI/AAAAAAAAAM4/45DFww9huro/s1600/interfacenotimplemented.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="640" src="http://1.bp.blogspot.com/-RzYFX6s_Wag/Tpmdqv35QTI/AAAAAAAAAM4/45DFww9huro/s640/interfacenotimplemented.jpg" width="577" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
As you can note above, we told the compiler that the shape object is an interface, we then specified that the square object implements the shape interface. We did all this using jsdocs annotations. However in our Square object we didn't provide the implementation for the draw method. This violates the contract and we are dutifully warned. Note how we've used annotations again and our code does not depend on any third party libraries. It's at a minimum.&lt;br /&gt;
&lt;br /&gt;
These are simple examples. For a whole slew of compiler warnings provided, you can read the documentation provided by google since our build uses google closure compiler.&lt;br /&gt;
&lt;br /&gt;
&lt;div style="text-align: center;"&gt;
&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;&amp;nbsp;Getting started&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
Firstly to get such a setup in place we use Ant as our build tool. If you don't already have Ant installed, you may download it from the following location and set it up on your system : &lt;a href="http://ant.apache.org/bindownload.cgi"&gt;http://ant.apache.org/bindownload.cgi&amp;nbsp;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
You also need the java jdk, which you can download from : 
&lt;a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html"&gt;http://www.oracle.com/technetwork/java/javase/downloads/index.html&amp;nbsp;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
You may continue now after installation, but do note the location where you unzipped ant and the location of the jdk bin folder. You'll need it when setting up NppExec ( you'll find out later what this is, keep reading).&lt;br /&gt;
&lt;br /&gt;
Now that you have both the Java jdk and Ant, you have the necessary tools to build. You simply call the build file from console. However, working in this manner is quite difficult and  you'll find yourself switching too much between a console window, your favorite code editor and perhaps the windows explorer.&lt;br /&gt;
&lt;br /&gt;
What we need is a way to integrate our build into an existing code editor. For this we will use Notepad++, not only is it open source and has no costs involved but also it's in continuous development and has a good community backing it. Moreover, there are many plugins already written that will make our integration work to a minimum.&amp;nbsp;
&lt;br /&gt;
&lt;br /&gt;
You may download notepad++ from the following url :&lt;br /&gt;
&lt;a href="http://notepad-plus-plus.org/download/"&gt;http://notepad-plus-plus.org/download/&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Now that we have notepad++, lets integrate it with our Ant build script.

Our expectations are simple. What we want to do is sit in notepad++ as we edit our code and then, without leaving the editor we'd like to simply press F6 when we are ready to compile. Lastly get a status report in console within notepad++ ; In this manner we never leave our editor. In addition it would be nice to browse our files within notepad++ itself. There are quite a few nice add-ins that supply a file explorer. I use Light explorer. You'll find it in the plugin manager explained below.
&lt;br /&gt;
&lt;br /&gt;
&lt;div style="text-align: center;"&gt;
&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;Setting up Notepad++&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
First, you'll need to add a plugin capable of processing our build file in a console that is displayed inside notepad++ itself. The plugin is called NppExec and it can be added through the plugin manager :
&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-aTeUeI7g_OM/TprJu78o7gI/AAAAAAAAAOA/mWuXfZGS--s/s1600/pluginmanager2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-aTeUeI7g_OM/TprJu78o7gI/AAAAAAAAAOA/mWuXfZGS--s/s1600/pluginmanager2.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
When your plugin manager loads, it will list all available plugins. Select NppExec form this list, in addition you may also want Light Explorer and JSLint. JSLint is a good tool and will aid you in writing and debugging your js code, you will be happy to run this tool every once in a while on your *.debug.js file generated by the build.
&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-6HroFnGzvaY/Tpmd1PEWFXI/AAAAAAAAANY/T7qqIXpx4qQ/s1600/pluginmanager.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="438" src="http://3.bp.blogspot.com/-6HroFnGzvaY/Tpmd1PEWFXI/AAAAAAAAANY/T7qqIXpx4qQ/s640/pluginmanager.jpg" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
As you can note from the  plugin manager in the screenshot above, i have already installed all 3 plugins we'll need. You'll have to select them from the “Available” tab.

Once you have NppExec installed, you need to tell it about your build.xml file.&lt;br /&gt;
&lt;br /&gt;
Before we do that, try to find a way to organize your projects, because for each project you create which may not be very often, you'll end up doing the follow steps manually each time. Basically once per project. So organization is quite important.&lt;br /&gt;
&lt;br /&gt;
The following screenshot demonstrates how I organize projects, if you come up with something more clever, feel free to share it :
&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-PEHltr_gfsA/TpmdvO_P4DI/AAAAAAAAANI/mZkWUxUYMEQ/s1600/organizeprojects.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-PEHltr_gfsA/TpmdvO_P4DI/AAAAAAAAANI/mZkWUxUYMEQ/s1600/organizeprojects.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
As you see, we have Project1 and Project2, each has it's own copy of the ant build script. The version file is where you can store the current version number. All your builds will deploy to a dst folder (even to a path of your choosing) using the version number.&lt;br /&gt;
&lt;br /&gt;
The tools folder you see in the root folder contains the tools necessary that the build uses such as YUICompressor, Google closure compiler, jsdocs-toolkit and ant-contrib (we use some extensions to ant so we need this too).  It's all provided along with the build script on codeplex.&lt;br /&gt;
&lt;br /&gt;
You may later download newer versions of these tools from their respective authors and replace them as you see fit keeping everything up to date.
&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-RltAmuh1Nlg/Tpmd3mt0bHI/AAAAAAAAANo/3dZyQd_vF2o/s1600/toolsfolder.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-RltAmuh1Nlg/Tpmd3mt0bHI/AAAAAAAAANo/3dZyQd_vF2o/s1600/toolsfolder.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
Now, lets proceed to tell NppExec about Project1.&lt;br /&gt;
&lt;br /&gt;
&lt;div style="text-align: center;"&gt;
&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;Setting up NppExec plugin in Notepad++&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
Before we start, we need to configure the build file manually and tell it the name under which it needs to build the folder structure. You start with a vanilla version of build.xml, in our example from the above screenshots, for project1's build file, we'll chose “MySite”.&lt;br /&gt;
&lt;br /&gt;
Open the build file under Project1 and run a search replace for all instances of “projectname” to the actual name you'd want to use. Now save it. OK, we are ready!

After installing the plugin, we need to set it up to process our build file which we have organized in Project1 and Project2 folders as in the previous screenshots above. In notepad++, go to the menu bar, under plugins – NppExec – Execute 
&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-zvTdcuc6S4A/Tpmd2wI05AI/AAAAAAAAANg/a_JUQQSWG_s/s1600/plugins.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="361" src="http://4.bp.blogspot.com/-zvTdcuc6S4A/Tpmd2wI05AI/AAAAAAAAANg/a_JUQQSWG_s/s640/plugins.jpg" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
The execute dialog will show up :
&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-_vV8no6h3vE/TpmdfhUnMlI/AAAAAAAAAMA/j-kMNXTDzr8/s1600/exec_step1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-_vV8no6h3vE/TpmdfhUnMlI/AAAAAAAAAMA/j-kMNXTDzr8/s1600/exec_step1.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
Now we are ready to include a piece of code that will prepare the console environment for us.&lt;br /&gt;
The code to include is :&lt;br /&gt;
&amp;nbsp;ENV_SET PATH = $(NPP_DIRECTORY);C:\apache-ant-1.8.1\bin;D:\Program Files (x86)\Java\jdk1.6.0_22\bin&lt;br /&gt;
cd c:\HTML5\Project1&lt;br /&gt;
cmd&lt;br /&gt;
&lt;br /&gt;
As you can see, we have told it where to find Ant executables that will process our build file and of course, Ant requires Java so also the Java jdk executables path is included. There are many different ways to set this up, I found doing the above the most straight forward and explicit.&lt;br /&gt;
&lt;br /&gt;
You may experiment differently because for each project you will be copying and pasting this same piece of code. The part that will change is the Project name and perhaps the path. Right now we have setup the build for Project1
&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-oicjw3ZcGdU/TpmdgQDm-kI/AAAAAAAAAMI/kZSE25nf3sc/s1600/exec_step2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-oicjw3ZcGdU/TpmdgQDm-kI/AAAAAAAAAMI/kZSE25nf3sc/s1600/exec_step2.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
Next, we don't want to be doing this repeatedly everytime we open notepad++ so we'are going to save it under a name, in this case “Project1” 
&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-FJ3YQrVQt_I/Tpmdhau1f-I/AAAAAAAAAMQ/-6hstjnw1_o/s1600/exec_step3.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-FJ3YQrVQt_I/Tpmdhau1f-I/AAAAAAAAAMQ/-6hstjnw1_o/s1600/exec_step3.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
And that's it. Save it and from now on, every time you open notepad++, you will hit your F6 key to execute your project and then you will  find Project1 in the dropdownlist to select from.&amp;nbsp;The script you wrote in the execute box will run and prepare your console for you as follows:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-aGKTZNOzyuY/Tpmd5Pyl1QI/AAAAAAAAANw/PqI_2Heb8p4/s1600/weareready.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="381" src="http://1.bp.blogspot.com/-aGKTZNOzyuY/Tpmd5Pyl1QI/AAAAAAAAANw/PqI_2Heb8p4/s640/weareready.jpg" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
Now you are ready to simply build hitting your F6 key every time you want to build. You'll be doing this after you've written a piece of code, so quite often. It's important to be able to do this in a single click, F6 is awesome that way.&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;The execute dialog will look like this now :
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-0rjVH038rDw/TpmdiTcCXcI/AAAAAAAAAMY/3QAV094-Lvg/s1600/f6.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-0rjVH038rDw/TpmdiTcCXcI/AAAAAAAAAMY/3QAV094-Lvg/s1600/f6.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
In the exit message you write the commands you want to execute on your build file. Above we execute the build using Ant, this will run all tasks.&lt;br /&gt;
&lt;br /&gt;
All tasks means, it will concatenate script files, compile your js through google closure, your css files also undergo the same procedure and will be processed by YUICompressor, if you have documentation, then js docs will also be generated. If the build is successful without errors, it will be packaged in the dst folder.&lt;br /&gt;
&lt;br /&gt;
That's quite a lot of tasks the build is doing and as your script files grow you'll waste quite a bit of time just waiting for it to finish. 

So instead of calling your build using the ant command, you can call individual tasks. For instance minifyjs or minifycss etc. It's detailed out in one of the sections below, for now, let's see what happens the first time we run the ant command. 
&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-QltY6RgopCY/Tpmdks12l5I/AAAAAAAAAMo/Xh3hlaOXH8k/s1600/f6console.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-QltY6RgopCY/Tpmdks12l5I/AAAAAAAAAMo/Xh3hlaOXH8k/s1600/f6console.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
As you can note from the above screenshot, calling ant alone without any arguments will run all tasks which is fine the first time because we have nothing, What's going to happen is that it will create the folder structure for us and we end up with the following. 
&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-5mntth9iT38/TpmdjbapB1I/AAAAAAAAAMg/lzXXGj94oKM/s1600/f6build_folderstructure.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-5mntth9iT38/TpmdjbapB1I/AAAAAAAAAMg/lzXXGj94oKM/s1600/f6build_folderstructure.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
Note how everything is under com.MySite folder. This is because prior to starting, if you still remember, we ran a search and replace in the build.xml file and changed all instances of “projectname” to “MySite”. We're using a domain name convention for this but if your a little ant savy, you can change this to whatever you prefer by editing the build manually. It's all declarative code and very friendly. I love it and so you get my very biased opinion.&lt;br /&gt;
&lt;br /&gt;
However, if you are new, it won't hurt if you can get a book on Ant, there's quite a lot of good material as it enjoys a lot of popularity.

Before we proceed, let's look at what happens after we close notepad++ and try to open it again. After re-opening notepad++, hit the F6 key and you get the execute dialog again. But this time the project is saved, so you don't have to enter the commands to warm up the console.&lt;br /&gt;
&lt;br /&gt;
As  you add projects and save them, they will appear under the dropdownlist you see in the screenie below. You simply select the project from this list and click OK, then F6 again.
&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-q3B6iOtyn-Y/Tpmdlc1fIAI/AAAAAAAAAMw/YxQa_qXu-uM/s1600/f6revisit.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-q3B6iOtyn-Y/Tpmdlc1fIAI/AAAAAAAAAMw/YxQa_qXu-uM/s1600/f6revisit.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
The first time you plan to load a projects build, you may have noticed that's it's a 2 step process because the first time you have to select the project and it will prepare the console environment executing the script you see in the dialog above.&lt;br /&gt;
&lt;br /&gt;
The second time onwards you hit F6, you get the Exec dialog that will take your ant command. And this same dialog will appear every time you hit F6 from now on, unless you decided you wanted to change project. You basically have to try it 2 or 3 times to get accustomed to it. It's quite difficult to put in writing and may seem too complex but really, it's not.&lt;br /&gt;
&lt;br /&gt;
OK, we are ready. Now lets write a bit of code and then Build.&lt;br /&gt;
&lt;br /&gt;
Making your javascript or css code part of this build process is quite simple. Your code has to go in either the Scripts or Css directory the build created for you. These are the directories that will get processed by the build. We'll discuss javascript now, however keep in mind that the same procedure is available for css as well. For css you get concatenation and minification too. 

Lets pretend we want to create a script file named “MyScript”.&lt;br /&gt;
&lt;br /&gt;
We start by creating a “MyScript” folder under the Scripts folder. Within MyScript, we can create an unlimited number of subfolders and within each we can add script files. How you arrange your script files is up to you. The important thing to consider is that the build file will process the script files starting with the folder then filename in ascending order, so if order is important to you, it certainly is to me, you will find a way to arrange your files alphabetically or numerically.&lt;br /&gt;
&lt;br /&gt;
The way I do it is I start in alphabetic order so I name the first folder i want processed “a”, and the second “b” etc.

But lets do a code example. I don't like to keep my code in the global namespace so I start with a closure, this is simply going to be an anonymous outer function that will embody all the code I write eg:
&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;
&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;
&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;function&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;window&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;document&lt;/span&gt;&lt;span style="color: black;"&gt;)
{
&lt;/span&gt;    &lt;span style="color: green;"&gt;//everythign else will go in here&lt;/span&gt;&lt;span style="color: black;"&gt;
}(&lt;/span&gt;&lt;span style="color: blue;"&gt;window&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;document&lt;/span&gt;&lt;span style="color: black;"&gt;));

&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style="clear: both;"&gt;
&lt;/div&gt;
The anonymous function above is called by passing the window and document as parameters. In order to ensure that all the code is enclosed within a closure as the code above, we can arrange the scripts as in the following screenshot:
&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-zO9g1-kpA24/Tpmd69vlG1I/AAAAAAAAAN4/s0RqM2yTuK4/s1600/writingcode.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="567" src="http://2.bp.blogspot.com/-zO9g1-kpA24/Tpmd69vlG1I/AAAAAAAAAN4/s0RqM2yTuK4/s640/writingcode.jpg" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
Note the 3 folders under MyScript.&amp;nbsp;Folder “a” has a script file that contains the opening parts of the anonymous function, and folder “z” contains the closing, and since our code will be processed and assembled into a final single file in ascending order, anything in between will get embodied within. Make sense?&amp;nbsp;I hope so because if you understood that then you are ready to write your code and build it!&lt;br /&gt;
&lt;br /&gt;
The final script file is going to be created when you build and it will be deposited in the MyScript folder under the same name so, you'll end up with MyScript.debug.js and MyScript.min.js respectively. The “debug” file contains your code without minification, while the “min” file contains optimization's and minification produced by google closure compiler.

So, we end up with this after a successful build (F6) :&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-nyI1QahNcws/TpmdudXfznI/AAAAAAAAANA/ushvbOhilU8/s1600/myscript_successfulbuild1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="640" src="http://3.bp.blogspot.com/-nyI1QahNcws/TpmdudXfznI/AAAAAAAAANA/ushvbOhilU8/s640/myscript_successfulbuild1.jpg" width="484" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
Note above, that apart from the stuff that went into dst folder (your deployment folder), the build also generated 4 new files. MyScript.debug.js, MyScript.min.js, MyScript.extern.local.in and MyScript.extern.thirdparty.in. We'll get to the last 2 files that were never mentioned before soon enough, for now focus on *.debug.js and *.min.js files produced.&lt;br /&gt;
&lt;br /&gt;
The first thing to note is that the name of the files has been acquired from the root folder under Scripts. And the contents of these files is the combination of all script files within the MyScript folder.&lt;br /&gt;
&lt;br /&gt;
If we inspect the contents of MyScript.debug.js, we see :
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;
&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;
&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;function&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;window&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;document&lt;/span&gt;&lt;span style="color: black;"&gt;)
{
 &lt;/span&gt;&lt;span style="color: green;"&gt;//everythign else will go in here&lt;/span&gt;&lt;span style="color: black;"&gt;
}(&lt;/span&gt;&lt;span style="color: blue;"&gt;window&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;document&lt;/span&gt;&lt;span style="color: black;"&gt;));

&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style="clear: both;"&gt;
&lt;/div&gt;
If you remember from our earlier setup of script files, each line of the code above was in a separate file.&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;C:\HTML5\Project1\src\com\MySite\scripts\MyScript\a\a.start.js contained :
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;
&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;
&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;function&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;window&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;document&lt;/span&gt;&lt;span style="color: black;"&gt;)
{

&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style="clear: both;"&gt;
&lt;/div&gt;
C:\HTML5\Project1\src\com\MySite\scripts\MyScript\b\a.myObject.js contained :
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;
&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;
&lt;pre style="margin: 0;"&gt;&lt;span style="color: green;"&gt;//everythign else will go in here&lt;/span&gt;&lt;span style="color: black;"&gt;

&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style="clear: both;"&gt;
&lt;/div&gt;
C:\HTML5\Project1\src\com\MySite\scripts\MyScript\z\a.end.js contained:
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;
&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;
&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;}(&lt;/span&gt;&lt;span style="color: blue;"&gt;window&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: blue;"&gt;document&lt;/span&gt;&lt;span style="color: black;"&gt;));

&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style="clear: both;"&gt;
&lt;/div&gt;
Now it's all combined into a single file. Perfect! We now have everything working. 

Let's introduce a small error in our js code, for instance let's add a function in a.myObject.js and deliberately miss a closing brace. Just something simple to see if everything is working. Now lets build and see what happens:
&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-iVJl2xBw9ek/TpmdeywSZPI/AAAAAAAAAL4/VaN7FZSh0Jk/s1600/closeyourbraceserror.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="640" src="http://2.bp.blogspot.com/-iVJl2xBw9ek/TpmdeywSZPI/AAAAAAAAAL4/VaN7FZSh0Jk/s640/closeyourbraceserror.jpg" width="569" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
If you open debug.js and go to line number 5, you will see that your missing a closing brace. For a complete list of compiler warnings that you will find useful, visit the following online document :  &lt;a href="http://code.google.com/closure/compiler/docs/error-ref.html"&gt;http://code.google.com/closure/compiler/docs/error-ref.html&amp;nbsp;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style="text-align: center;"&gt;
&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;About *.extern.local.in and *.extern.thirdparty.in&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
Remember the *.extern.local.in and *.extern.thirdparty.in files that were generated when you first build the project? They are useful when compiling in Advanced mode. These are simply files where you can include methods from getting renamed when using google closure in dvanced mode. This is necessary because the closure compiler will rename everything in the project and you wont be able to access any methods in your objects using their original names you provided.&lt;br /&gt;
&lt;br /&gt;
The minification process wants to give you a very tiny hyper crunched file. So it's your job to tell it what method names to leave in tact.&lt;br /&gt;
&lt;br /&gt;
The other *.extern.thirdparty.in can contain a third party library's extern file. Most third parties will provide you with one.&amp;nbsp;For example, jquery has an extern at the following url :&lt;br /&gt;
&lt;a href="http://code.google.com/p/closure-compiler/source/browse/trunk/contrib/externs/jquery-1.3.2.externs.js?r=66"&gt;http://code.google.com/p/closure-compiler/source/browse/trunk/contrib/externs/jquery-1.3.2.externs.js?r=66&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
You just need to copy that into your third party extern file. The third party extern is useful when your calling methods from external libraries like those in jquery. The extern will contain all method names which will result in the compiler not renaming any of these methods in your code.&lt;br /&gt;
I don't want to repeat what the google documentation says about this so go ahead and have a good reading at the following url :&lt;br /&gt;
&lt;a href="http://code.google.com/closure/compiler/docs/api-tutorial3.html#externs "&gt;http://code.google.com/closure/compiler/docs/api-tutorial3.html#externs&amp;nbsp;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;Externs are only a small part of it. There are many other things to keep in mind when using google closure in advanced mode. Why is this the default? I don't know, good question, I guess because I work in the advanced mode. I encourage you to do the same.&lt;br /&gt;
Just make sure you read the following document as well :&lt;br /&gt;
&lt;a href="http://code.google.com/closure/compiler/docs/api-tutorial3.html"&gt;http://code.google.com/closure/compiler/docs/api-tutorial3.html&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Advanced mode can be turned off in the build. There are instructions on how to do this in one of the sections below.&lt;br /&gt;
&lt;br /&gt;
Don't forget about strong typing in javascript. You can achieve similar functionality using jsdocs annotations to decorate your code and tell the compiler some extra information about your types, arguments, constructors and so forth.&lt;br /&gt;
Google closure will warn you when you do something silly and in this way you can make use of strong typing in javascript. 

Read all about it at the following url:&lt;br /&gt;
&lt;a href="http://code.google.com/closure/compiler/docs/js-for-compiler.html"&gt;http://code.google.com/closure/compiler/docs/js-for-compiler.html&amp;nbsp;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
In this way you can tell the closure compiler what arguments in a function are optional, what the return type of a function is, the type of a variable and so forth. The above document is complete and a good reading. By using these annotations not only do you make use of strong typing but also the build can use this to generate your documentation. Two birds with one stone. Perfect!&lt;br /&gt;
&lt;br /&gt;
&lt;div style="text-align: center;"&gt;
&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;Selectively excluding script files from the build&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
You can add your third party code in the script folder too. But by doing so the build will process it and try to minify that code as well. Third-party code is already minified by their respective publishers so it makes sense wanting to exclude these from the final build. 
But why put third party code in the script folder? The reasons can vary, I put third party in there because I unit test within the src folder and if my code relies on third party, I'd like the code there.&lt;br /&gt;
&lt;br /&gt;
Another reason you may want script files excluded is if you have two different versions of the same js object ( in a separate class files *.js) that does a particular task but targeting different builds. So before a build you can exclude one, build, then exclude the other and produce two different versions of your code that behave a little different, perhaps one version with particular functionality against jquery and the other against dojo lib or what have you.

In order to exclude a script file, open your build.xml and look for the following property :&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;
&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;
&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;property&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"scripts.exclude"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;””&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;

&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style="clear: both;"&gt;
&lt;/div&gt;
In the value attribute, provide the name of your script file you want excluded. For instance if you wanted to exclude files foobar1.js and foobar2.js, from being assembled and processed you can specify this as follows :
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;
&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;
&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;property&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"scripts.exclude"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;”foobar1.js,&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;foobar2&lt;/span&gt;&lt;span style="color: black;"&gt;.&lt;/span&gt;&lt;span style="color: red;"&gt;js&lt;/span&gt;&lt;span style="color: black;"&gt;” &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style="clear: both;"&gt;
&lt;/div&gt;
&lt;br /&gt;
The same applies for css files, you simply exclude them by providing the filename in the css.exclude property. Look for it in the build file. 
&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;
&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;
&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;property&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"css.exclude"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;”foobar1.css,&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;foobar2&lt;/span&gt;&lt;span style="color: black;"&gt;.&lt;/span&gt;&lt;span style="color: red;"&gt;css&lt;/span&gt;&lt;span style="color: black;"&gt;” &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style="clear: both;"&gt;
&lt;/div&gt;
&lt;br /&gt;
Excluding scripts from documentation
Script files in the scripts folder after getting assembled, the *.debug.js is then processed for documentation. If you wish to not document a certain *.debug.js file, you may specify this in the build.xml manually as follows :
&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;
&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;
&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;property&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"docs.exclude"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"foobar.debug.js"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style="clear: both;"&gt;
&lt;/div&gt;
&lt;br /&gt;
Jsdoc-toolkit Templates
Note that jsdocs is very flexible and you can create your own custom templates in the templates folder. For the meat and potatoes about how it works you may reference the jsdoc-toolkit project templates section :&lt;br /&gt;
&lt;a href="http://code.google.com/p/jsdoc-toolkit/wiki/Templates"&gt;http://code.google.com/p/jsdoc-toolkit/wiki/Templates&amp;nbsp;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
As far as our build is concerned, if you want to use a specific jsdocs template, you can specify this in the following property :
  &lt;property name="js.doc.templates" value="${tools.dir}/jsdoc-toolkit/templates/jsdocs"&gt;
By default, it's using the jsdocs template as you can see from the setting in the value attribute above. You'd replace jsdocs with the name of your custom template.

Controlling compilation level :
By default the build will compile in advanced mode. If you want to go into the simple mode, you can change this in the following property :
&lt;/property&gt;&lt;br /&gt;
&lt;property name="js.doc.templates" value="${tools.dir}/jsdoc-toolkit/templates/jsdocs"&gt;&lt;br /&gt;&lt;/property&gt;&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;
&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;
&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;property&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"closureoptimizations"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"ADVANCED_OPTIMIZATIONS"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: green;"&gt;&amp;lt;!-- ADVANCED_OPTIMIZATIONS or SIMPLE_OPTIMIZATIONS--&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style="clear: both;"&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
Just change the contents of the value attribute to “SIMPLE_OPTIMIZATIONS”

Pretty Print the minification:
Sometimes when i'm debugging the minified code, I'd like it to be nicely formatted, it's so much easier, that way I can inspect the output. At some point you will appreciate this feature of the google closure compiler, so if you want the minified code to be nicely formatted, add the following line :&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;
&amp;nbsp;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;
&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;
&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;arg&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;line&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"--formatting=PRETTY_PRINT"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style="clear: both;"&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
You can add the above line right below this :&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;
&amp;nbsp; 
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;
&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;
&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;arg&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;line&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"--compilation_level=${closureoptimizations}"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt; 
&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style="clear: both;"&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
If you have other google closure compiler arguments to set, this is how you do it.

Other advanced options can be set in their respective tasks, you'll need to look at the build yourself and make sense out of it. I have tried to make it as clear and obvious as possible, but when in doubt, just follow how I coded each task and replicate, in case you want to make your own custom task.&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
In addition some tasks like minifyjs and minifycss can take additional arguments, for example in case you want to specify the compilation level for google closure compiler or formatted pretty print after minification, you can do so by adding a custom argument or modifying an existing one.&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
You'll need to look at their relevant documentation too : 
&lt;a href="http://code.google.com/closure/compiler/docs/compilation_levels.html"&gt;http://code.google.com/closure/compiler/docs/compilation_levels.html&lt;/a&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: center;"&gt;
&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;&amp;nbsp;Build Tasks&lt;/span&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
We can supply build tasks invidually in the execute dialog :&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-0rjVH038rDw/TpmdiTcCXcI/AAAAAAAAAMY/3QAV094-Lvg/s1600/f6.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-0rjVH038rDw/TpmdiTcCXcI/AAAAAAAAAMY/3QAV094-Lvg/s1600/f6.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Calling ant alone, will run all tasks.&lt;/div&gt;
&lt;div&gt;
ant&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: center;"&gt;
&lt;span class="Apple-style-span" style="font-size: large;"&gt;&amp;nbsp;Executing tasks individually&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;To build, compile and minify, supply :&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;ant minifyjs&amp;nbsp;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
To concatenate our script files only, supply :&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;ant concatjs&amp;nbsp;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
To concat and minify our css, supply :&lt;/div&gt;
&lt;div&gt;
ant&amp;nbsp;minifycss&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
To concatenate css files only, supply :&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;ant concatcss &amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
To generate documentation only, supply :&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;ant jsdocs&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div style="text-align: center;"&gt;
&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;Final Notes&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
HTML5 is all the rage now. We've started moving most of our code to the clientside and Javascript is more important now than ever. The productivity of an automated build is hard to ignore.&amp;nbsp;As you have seen, our solution involves using Ant to drive our build process. While there is quite a bit of competition in this space Ant enjoys a lot of popularity and there is plenty of adequate documentation written through the years.&lt;br /&gt;
&lt;br /&gt;
We also made use of notepad++ due to it's popularity as a light weight free code editor. One hiccup might be that notepad++ is windows only, but the build itself can be called from console so if notepad++ is not available for your platform, you can very well try to integrate it into your favorite code editor as we did in this article or simply call the build script from console.
&lt;br /&gt;
&lt;br /&gt;
Lastly, while I come from a .NET background, the urge to use MSBuild and integrate it into Visual Studio was great. One reason I never pursued this route is because I've been actually using Visual Studio .NET over the years to edit and debug my Javascript, and I'm not impressed. It's too heavy.&lt;br /&gt;
&lt;br /&gt;
I wanted to enjoy more freedom on how I write Javascript without the limitations of a single IDE, platform and extra costs. Writing an Ant build opened up a lot of possibilities.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;How good is this build?&lt;/b&gt;&lt;br /&gt;
I've wrote this build somewhere in November 2010 and I've been using it since. I must say that I've never had any real problems and its worked flawlessly for me. I release it in the hope that you too have the same experience or are capable of adapting it to your own setups and environments.&lt;br /&gt;
&lt;br /&gt;
&lt;div style="text-align: center;"&gt;
&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;Download&lt;/span&gt;&lt;/div&gt;
The project can be downloaded from&amp;nbsp;&lt;a href="http://jsbuild.codeplex.com/"&gt;http://jsbuild.codeplex.com/&lt;/a&gt;&lt;br /&gt;
accompanied with a MIT license, feel to use it in anyway you see fit.&lt;img src="http://feeds.feedburner.com/~r/typps/axAH/~4/HTRzrEUPiXs" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.typps.com/feeds/6166362986796929244/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.typps.com/2011/10/ant-process-driven-build-for-javascript.html#comment-form" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/6166362986796929244?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/6166362986796929244?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typps/axAH/~3/HTRzrEUPiXs/ant-process-driven-build-for-javascript.html" title="An Ant process driven build for Javascript --Make your own Javascript IDE" /><author><name>Alessandro</name><uri>http://www.blogger.com/profile/08620937587114860179</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="24" src="http://4.bp.blogspot.com/-9iFBqNPDD_E/TbtHLjeMgZI/AAAAAAAAAK0/9WMh7OUK6KA/s220/alessandro.png" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-5AekQGTzXwk/Tpvar4xiVsI/AAAAAAAAAOI/EWiK_Ch_H2Q/s72-c/parametertypeerror.jpg" height="72" width="72" /><thr:total>1</thr:total><feedburner:origLink>http://blog.typps.com/2011/10/ant-process-driven-build-for-javascript.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0MFQH87fCp7ImA9WhdbFk0.&quot;"><id>tag:blogger.com,1999:blog-6778277710194170777.post-6658718395596053698</id><published>2011-10-02T22:19:00.001+02:00</published><updated>2011-10-14T17:16:51.104+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-10-14T17:16:51.104+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Silverlight" /><title>Abmho --A syntax highlighter that runs online and offline</title><content type="html">&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;Introducing Abmho –the syntax highlighter wizard&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The first stable release of &lt;a href="http://www.abmho.com/"&gt;Abmho&lt;/a&gt; is out. It is by no means the first or only syntax highlighter on the web. There are quite a few out there but the vast majority aren't usable. &lt;a href="http://www.abmho.com/"&gt;Abmho&lt;/a&gt; can run on the web or as a standalone silverlight application on your desktop.&lt;br /&gt;
&lt;br /&gt;
Since I like my code colored and styled when pasting code snippets in blog posts, I decided to write an app that did only that. I hardly blog by the way, hopefully this will motivate me more.&lt;br /&gt;
&lt;br /&gt;
Now, lets look at how &lt;a href="http://www.abmho.com/"&gt;Abmho&lt;/a&gt; works and what it can do. 

There are four steps in the process.&lt;br /&gt;
&lt;br /&gt;
You start by pasting your code, as you see in the screenie below, this is done in the first step.&lt;br /&gt;
&lt;br /&gt;
&lt;div style="text-align: center;"&gt;
Step 1 - Paste your code&lt;/div&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-PTAF0u1xdaE/ToiNhAUCAaI/AAAAAAAAALk/GEnnfQtvaHs/s1600/step1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="411" src="http://4.bp.blogspot.com/-PTAF0u1xdaE/ToiNhAUCAaI/AAAAAAAAALk/GEnnfQtvaHs/s640/step1.jpg" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
Next, you need to state what language your code is in. This step also lets you set a language at the line level. What this means is that you can mix languages in a single snippet. No other syntax highlighter can do this by the way. 
&lt;br /&gt;
&lt;br /&gt;
&lt;div style="text-align: center;"&gt;
Step 2 - Compose the language&lt;/div&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-94GjzVte_Uo/ToiNjg4RzxI/AAAAAAAAALo/dBDgwHOEgq8/s1600/step2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="412" src="http://2.bp.blogspot.com/-94GjzVte_Uo/ToiNjg4RzxI/AAAAAAAAALo/dBDgwHOEgq8/s640/step2.jpg" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
Mixing languages has always been problematic for me because in asp.net you can mix code and markup in the same template file. 

This meant that I had to copy the code separately and the markup separately. Quite the work it used to be but not anymore.&lt;br /&gt;
&lt;br /&gt;
By default, the selected language will apply to all lines. If you want to mix languages, you must first “Uncheck” the checkbox and then select line/s and apply at the line level.
&lt;br /&gt;
&lt;br /&gt;
&lt;div style="text-align: center;"&gt;
Step 3 - Settings and live preview&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-gVLBeTUEj60/TooYn39LCRI/AAAAAAAAAL0/A3wdzx2ugME/s1600/step3.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="414" src="http://3.bp.blogspot.com/-gVLBeTUEj60/TooYn39LCRI/AAAAAAAAAL0/A3wdzx2ugME/s640/step3.jpg" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;blockquote&gt;
One thing to note in the above step is that when setting the background color, the code highlight will try to contrast the background automatically. This is not a&amp;nbsp;predefined&amp;nbsp;scheme and there are some edge cases where the contrasting might not be perfect. Luckily you have many colors to choose from.&lt;/blockquote&gt;
Lastly, you collect the output. You simply copy the code by either right click (select all – copy) or use the apposite copy button. The code is then embeded in your page manually. The generated output as you can see is clear css/html where you can optionally specify that classes be used instead of inline css for styling the layout. 
&lt;br /&gt;
&lt;div style="text-align: center;"&gt;
Step 4 - Copy the output and embed directly on a web page&lt;/div&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-nic-gwCqWJw/ToiNshLss6I/AAAAAAAAALw/vIDxRc_Wd5U/s1600/step4.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="410" src="http://4.bp.blogspot.com/-nic-gwCqWJw/ToiNshLss6I/AAAAAAAAALw/vIDxRc_Wd5U/s640/step4.jpg" width="640" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
The application is written in silverlight and hosted online, but you can opt to run it in&lt;b&gt; OOB&lt;/b&gt; mode (out of browser mode). In this mode, the application will run as a desktop application, meaning it will be available to you in offline mode and you don't have to navigate to our website. Instead the application gets installed locally on your desktop and you will be able to launch it from either your start menu or desktop shortcut.&lt;br /&gt;
&lt;br /&gt;
Currently I don't run ads and there is no business model to support this project.  Creating the application in silverlight allowed me to keep server costs at a minimum as I have no code running serverside (The only cost to maintain is the domain Abmho.com). The silverlight application itself is hosted on google app engine.&lt;br /&gt;
&lt;br /&gt;
Why did I make it?&lt;br /&gt;
While I find it helpful as an application for my own purposes, whether my requirement is to provide highlighted syntax in my applications or in my blog posts, having writing this application has also permitted me to experiment in silverlight/mvvm pattern/prism. Simple applications like this help me to improve my coding efforts on larger projects and make better decisions overall. So why not?&lt;br /&gt;
&lt;br /&gt;
What's next?&lt;br /&gt;
Not much really.  Next I will be porting this application to metro style on windows 8. Hopefully with minimum effort ( or I like to think so). If I feel like it, ofcourse!&lt;br /&gt;
&lt;br /&gt;
Updates&lt;br /&gt;
10/10/2011 --A new version of abmho is out. For those of you who installed the oob mode (desktop version) must first uninstall the previous version (control panel, find abmho and uninstall). And then proceed to our &lt;a href="http://www.abmho.com/#OOB"&gt;install page&lt;/a&gt; and reinstall it. &lt;strike&gt;Subsequent releases will update automatically.&lt;/strike&gt;&lt;br /&gt;
&lt;br /&gt;
Update&lt;br /&gt;
10/11/2011 --A new version is out. Abmho v1.3 Thanks to everybody reporting problems through the app itself. For OOB mode, we are unable to provide automatic updates because it requires a digital signed code trusted certificate which costs an arm and a leg.&lt;br /&gt;
&lt;br /&gt;
This is a free app, so follow the instructions in the install page. Basically simply right click on the install page, choose to "Remove the application", then proceed to re-install using the same page ( The install button will re-enable itself).&lt;br /&gt;
&lt;br /&gt;
Update&lt;br /&gt;
10/11/2011 --A new version is up. Abmho v1.4. Why so many versions in one day? Because I'm making fixes and pushing them out as they are available. This will probably the last one in a while. We now do not require elevated permissions for OOB. What a relief since we don't want to invest in a code signing digital certificate. I'm happy!&lt;br /&gt;
&lt;br /&gt;
Update&lt;br /&gt;
10/14/2011 --A new version is up. Abmho v1.6. This fixes an unexpected background worker error when using the wizard then navigating within the site and returning to the wizard again. Many of you reported this problem and you were all very kind. Thanks! We were not disposing the wizard correctly when navigating out of the home page. This is fixed now and I am happy!&lt;img src="http://feeds.feedburner.com/~r/typps/axAH/~4/hOtUN1wBxQQ" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.typps.com/feeds/6658718395596053698/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.typps.com/2011/10/abmho-syntax-highlighter-that-runs.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/6658718395596053698?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/6658718395596053698?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typps/axAH/~3/hOtUN1wBxQQ/abmho-syntax-highlighter-that-runs.html" title="Abmho --A syntax highlighter that runs online and offline" /><author><name>Alessandro</name><uri>http://www.blogger.com/profile/08620937587114860179</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="24" src="http://4.bp.blogspot.com/-9iFBqNPDD_E/TbtHLjeMgZI/AAAAAAAAAK0/9WMh7OUK6KA/s220/alessandro.png" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-PTAF0u1xdaE/ToiNhAUCAaI/AAAAAAAAALk/GEnnfQtvaHs/s72-c/step1.jpg" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://blog.typps.com/2011/10/abmho-syntax-highlighter-that-runs.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkQHRXk4eip7ImA9WhZXFUU.&quot;"><id>tag:blogger.com,1999:blog-6778277710194170777.post-1670034073380241871</id><published>2011-05-05T08:47:00.004+02:00</published><updated>2011-05-05T09:25:34.732+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-05-05T09:25:34.732+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="WPF" /><title>Binding data from the ViewModel to the Wpf Menu control</title><content type="html">Databinding the Menu Control in wpf to a viewmodel can require a little bit of effort. As a menu control can contain hierarchical data, you must think of a data model to represent your menu items upfront, so I've decided to write a little about it. But before we dive into the meat and potatoes, lets try the basics first. &lt;br /&gt;
&lt;br /&gt;
Lets see what a simple statically defined menu control looks like and then we'll transform this into a data model we can bind to from our viewmodel.&lt;br /&gt;
&lt;br /&gt;
The statically defined menu :&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Grid&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Border&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;VerticalAlignment&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Top"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Height&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"25"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;BorderThickness&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"0,0,0,1"&lt;/span&gt;&lt;span style="color: black;"&gt; 
            &lt;/span&gt;&lt;span style="color: red;"&gt;Background&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"#FFE4E2E2"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;BorderBrush&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"#FFA0A0A0"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Menu&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Height&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"25"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;VerticalAlignment&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"top"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
            &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;MenuItem&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Header&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"_File"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;MenuItem&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Header&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"New/Open project"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;MenuItem&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Header&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Save"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;MenuItem&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Header&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Exit"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
            &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;MenuItem&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
            &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;MenuItem&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Header&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"_Help"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;MenuItem&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Header&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"About us"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
            &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;MenuItem&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Menu&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Border&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Grid&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
When you run the above piece of code, this is what you will see:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-x6TEQ7t5B7c/TcJAhSekfNI/AAAAAAAAALc/GhwJeEl9e1c/s1600/staticmenu.jpg" /&gt;&lt;/div&gt;&lt;br /&gt;
Notice that each MenuItem is a HeaderedItemsControl. This is simply an ItemsControl with a Header.&lt;br /&gt;
&lt;br /&gt;
Because each MenuItem is a HeaderedItemsControl, this allows our Menu to have an unlimited number of nesting as each Item has a header ( a label, the title you see displayed in the menu) and then an items collection (the sub menu items). If we wanted to see a qiuck example, this can be easily represented declaratively as follows :&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Menu&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
      &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;MenuItem&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Header&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"_File"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
           &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;MenuItem&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Header&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"New/Open project"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
...
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;See how each MenuItem can in turn contain nested MenuItems, and a Header property is set to the title we want to display.&lt;br /&gt;
&lt;br /&gt;
To bind the Menu control from our viewmodel, we need to be able to set the bindings for each menu items header and additionally we'll need to set a command argument so that we can identify the item clicked. &lt;br /&gt;
&lt;br /&gt;
In order to achieve this we'll have to set bindings through the ItemContainerStyle. This will allow us to target the element generated for each MenuItem so that we can pass the Header value and additionally a command argument value to identify the item. By using a style element in ItemContainerStyle, we can set the TargetType to MenuItem as in the example code below, and the property setters will apply the value bindings to each MenuItem. Perfect. &lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Menu&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;ItemsSource&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"{Binding MenuItems}"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Menu&lt;/span&gt;&lt;span style="color: black;"&gt;.&lt;/span&gt;&lt;span style="color: red;"&gt;ItemContainerStyle&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Style&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;TargetType&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"MenuItem"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
            &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Setter&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Property&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Command"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Value&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"{Binding Command}"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
            &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Setter&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Property&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"CommandParameter"&lt;/span&gt;&lt;span style="color: black;"&gt; 
                       &lt;/span&gt;&lt;span style="color: red;"&gt;Value&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"{Binding CommandParameter}"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
            &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Setter&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Property&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Header"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Value&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"{Binding Header}"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
            &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Setter&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Property&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"ItemsSource"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Value&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"{Binding Items}"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Style&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Menu&lt;/span&gt;&lt;span style="color: black;"&gt;.ItemContainerStyle&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Menu&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
Let's quickly walk through the piece of xaml above. As you can note, the Menu control itself is bound to a MenuItems collection exposed in our viewmodel. &lt;br /&gt;
&lt;br /&gt;
Xaml : &lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Menu&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;ItemsSource&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"{Binding MenuItems}"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
ViewModel:&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; ObservableCollection&amp;lt;FileMenu&amp;gt; MenuItems
{
 &lt;/span&gt;&lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;
 {
  &lt;/span&gt;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&lt;span style="color: black;"&gt; (_menuItems = _menuItems ??
   &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; ObservableCollection&amp;lt;FileMenu&amp;gt;());
 }
}
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
Xaml :&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Style&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;TargetType&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"MenuItem"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
            &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Setter&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Property&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Command"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Value&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"{Binding Command}"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
            &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Setter&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Property&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"CommandParameter"&lt;/span&gt;&lt;span style="color: black;"&gt; 
                       &lt;/span&gt;&lt;span style="color: red;"&gt;Value&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"{Binding CommandParameter}"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
            &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Setter&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Property&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Header"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Value&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"{Binding Header}"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
            &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Setter&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Property&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"ItemsSource"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Value&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"{Binding Items}"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Style&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
The above xaml binds to each item in the MenuItems ObservableCollection our viewmodel exposes.&lt;br /&gt;
&lt;br /&gt;
Model:&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&lt;span style="color: black;"&gt; FileMenu : ModelBase
{
&lt;/span&gt;&lt;span style="color: black;"&gt;        ...
 &lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt; Header { &lt;/span&gt;&lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;; &lt;/span&gt;&lt;span style="color: blue;"&gt;set&lt;/span&gt;&lt;span style="color: black;"&gt;; }

 &lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt; CommandParameter { &lt;/span&gt;&lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;; &lt;/span&gt;&lt;span style="color: blue;"&gt;set&lt;/span&gt;&lt;span style="color: black;"&gt;; }

 &lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; ICommand Command
 {
  &lt;/span&gt;&lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;
  {
   &lt;/span&gt;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&lt;span style="color: black;"&gt; (_command = _command ??
    &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; DelegateCommand&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;(
     OnMenuItemClick, (x)=&amp;gt; IsEnabled));
  }
 }
...
 &lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; ObservableCollection&amp;lt;FileMenu&amp;gt; Items
 {
  &lt;/span&gt;&lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;
  {
   &lt;/span&gt;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&lt;span style="color: black;"&gt; (_items = _items ??
    &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; ObservableCollection&amp;lt;FileMenu&amp;gt;());
  }
 }
 ...
}
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
The FileMenu class above represents each item in the MenuItem's collection. As you can see, it's a very simple object with just the needed properties. Note also that each item can also contain child items, this allows us to build a hierarchical menu dynamically in code. If we go back to look at the previous xaml listing above, this is exactly what the following line does : &lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Setter&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Property&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"ItemsSource"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Value&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"{Binding Items}"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
The ItemsSource property is exposed by the MenuItem control. In this manner, if each FileMenu Item contains submenu items in turn, they will be bound. Nice!&lt;br /&gt;
&lt;br /&gt;
We now have a perfectly bindable hierarchical menu and what more, it follows the MVVM pattern with clear separation. &lt;br /&gt;
&lt;br /&gt;
Be sure to check out the sample application since code speaks for itself!&lt;br /&gt;
&lt;h1&gt;&lt;a href="https://sites.google.com/a/typps.com/home/WpfLab3.zip?attredirects=0&amp;amp;d=1"&gt;Download the sample code&lt;/a&gt;&lt;/h1&gt;&lt;br /&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://blog.typps.com/2011/05/binding-data-from-viewmodel-to-wpf-menu.html"&gt;&lt;img alt="kick it on DotNetKicks.com" border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://blog.typps.com/2011/05/binding-data-from-viewmodel-to-wpf-menu.html" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://dotnetshoutout.com/typps-Binding-data-from-the-ViewModel-to-the-Wpf-Menu-control" rev="vote-for"&gt;&lt;img alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblog.typps.com%2F2011%2F05%2Fbinding-data-from-viewmodel-to-wpf-menu.html" style="border: 0px;" /&gt;&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/typps/axAH/~4/biyMosR9Hno" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.typps.com/feeds/1670034073380241871/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.typps.com/2011/05/binding-data-from-viewmodel-to-wpf-menu.html#comment-form" title="3 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/1670034073380241871?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/1670034073380241871?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typps/axAH/~3/biyMosR9Hno/binding-data-from-viewmodel-to-wpf-menu.html" title="Binding data from the ViewModel to the Wpf Menu control" /><author><name>Alessandro</name><uri>http://www.blogger.com/profile/08620937587114860179</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="24" src="http://4.bp.blogspot.com/-9iFBqNPDD_E/TbtHLjeMgZI/AAAAAAAAAK0/9WMh7OUK6KA/s220/alessandro.png" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-x6TEQ7t5B7c/TcJAhSekfNI/AAAAAAAAALc/GhwJeEl9e1c/s72-c/staticmenu.jpg" height="72" width="72" /><thr:total>3</thr:total><feedburner:origLink>http://blog.typps.com/2011/05/binding-data-from-viewmodel-to-wpf-menu.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEENQX46eCp7ImA9WhZVE04.&quot;"><id>tag:blogger.com,1999:blog-6778277710194170777.post-9148657924914340455</id><published>2011-04-29T17:39:00.015+02:00</published><updated>2011-05-25T17:18:10.010+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-05-25T17:18:10.010+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="WPF" /><title>Getting the Most Out of the Winforms PropertyGrid Control in WPF</title><content type="html">The PropertyGrid control has been missing in WPF since it's release and sadly it's still missing! There have been various attempts by third parties to provide one and I cant speak for the commercial offerings but I find the ones in the open source space to be quite lacking and incomplete. Most projects I found on codeplex are still in beta!&lt;br /&gt;
&lt;br /&gt;
I really like the api exposed by the original PropertyGrid which I have been using for years now but it's a Winforms control. This presents some problems. The main issue for me with the Winforms version of the PropertyGrid is that it's not styleable. So it means that I'll have some inconsistency in my UI and it will stick out as shabby and odd. &lt;br /&gt;
&lt;br /&gt;
Below is a screenshot of the PropertyGrid bound to a simple “Person” object, that exposes a complex type Address and a vehicle collection property.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-fCnpTf0Guys/TbrVIzuCkFI/AAAAAAAAAKw/TIicmmZkkyo/s1600/propertygrid-shabby.jpg" /&gt;&lt;/div&gt;&lt;br /&gt;
As old as it may look, it works so well and does it job so nicely! I love this control. In the end, I've decided to use this in my Wpf application regardless of the oddity it brings. Clearly, functionality is a lot more important and since beauty lies in the eyes of the beholder, it's a subjective matter. And writing a brand new native Wpf PropertyGrid control is out of the question. &lt;br /&gt;
&lt;br /&gt;
Looks apart, think of the great things this control can do. You practically bind your objects to it and it will list them in a neat grid, categorized, with many builtin editors for color editing, navigating for images, browsing and editing collections using the builtin CollectionEditor etc. And if this is not enough you can write simple extensions with your own custom editors. Indeed this control is a gem!&lt;br /&gt;
&lt;blockquote&gt;Update 4/30/2011&lt;br /&gt;
It seems since we are referencing a Winforms library, in particular System.Windows.Forms, our client app is forced to reference the fatter .NET 4.0 version versus just the slimmer .NET 4.0 client profile which is the default.&amp;nbsp;&lt;/blockquote&gt;&lt;blockquote&gt;It's not such a big con, but most certainly something to consider. I've left some more instructions at the bottom of this post.&lt;/blockquote&gt;One thing in particular that I've done is to wrap it up in a Custom Wpf Control because in order to use the Winforms version in Wpf, we'll need to :&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;Interop via WindowsFormsHost (as easy as eating cake)&lt;/li&gt;
&lt;li&gt;We'd like to bind our ViewModel to the PropertyGrid directly from the View declaratively. This will allow us to avoid tight coupling with our ViewModel.&lt;/li&gt;
&lt;/ol&gt;Here's what the custom controls template looks like :&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;ResourceDictionary&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;xmlns&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"http://schemas.microsoft.com/winfx/2006/xaml/presentation"&lt;/span&gt;&lt;span style="color: black;"&gt;
                    &lt;/span&gt;&lt;span style="color: red;"&gt;xmlns&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: red;"&gt;x&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"http://schemas.microsoft.com/winfx/2006/xaml"&lt;/span&gt;&lt;span style="color: black;"&gt;
                    &lt;/span&gt;&lt;span style="color: red;"&gt;xmlns&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: red;"&gt;local&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"clr-namespace:WpfLab2.Controls"&lt;/span&gt;&lt;span style="color: black;"&gt;
                   &lt;/span&gt;&lt;span style="color: red;"&gt;xmlns&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: red;"&gt;o&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Style&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;TargetType&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"{x:Type local:WpfPropertyGrid}"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Setter&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Property&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Template"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
            &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Setter&lt;/span&gt;&lt;span style="color: black;"&gt;.&lt;/span&gt;&lt;span style="color: red;"&gt;Value&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;ControlTemplate&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;TargetType&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"{x:Type local:WpfPropertyGrid}"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Grid&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                        &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;WindowsFormsHost&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;x&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: red;"&gt;Name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"host"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                            &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;o&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;PropertyGrid&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;x&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: red;"&gt;Name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"propertyGrid1"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                        &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;WindowsFormsHost&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Grid&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;ControlTemplate&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
            &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Setter&lt;/span&gt;&lt;span style="color: black;"&gt;.Value&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Setter&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Style&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;ResourceDictionary&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
Minus the namespaces and the common style declarations you'll likely do for any templating requirement, it's fairly simple. All we need is to nest the Winforms PropertyGrid inside a WindowsFormsHost.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Grid&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;WindowsFormsHost&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;x&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: red;"&gt;Name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"host"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;o&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;PropertyGrid&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;x&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: red;"&gt;Name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"propertyGrid1"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;WindowsFormsHost&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Grid&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
That's pretty much all of the code. Simple indeed. The Wpf Control itself consists of a single dependency property minus a small plumbing effort. Attached to this article I include a sample application containing the control, so you can get a hang of how it works. Our View itself that consumes the PropertyGrid, just comes down to a single line of code that includes the Custom control binding to a property in the ViewModel declaratively eg:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;local&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;WpfPropertyGrid&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;SelectedObject&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"{Binding PersonItem, Mode=TwoWay}"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
See! Now powered with such a fantastic control you can provide easy editing of objects in your application and maintain your MVVM pattern. I love it.&lt;br /&gt;
&lt;br /&gt;
Make sure you also read the following resource on msdn if your new to the PropertyGrid. It provides all the basics you'll need to know to get you up and running quickly. &lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://msdn.microsoft.com/en-us/library/aa302326.aspx"&gt;http://msdn.microsoft.com/en-us/library/aa302326.aspx&lt;/a&gt;&amp;nbsp;(Basics about the PropertyGrid)&lt;br /&gt;
&lt;a href="http://msdn.microsoft.com/en-us/library/ms742875.aspx"&gt;http://msdn.microsoft.com/en-us/library/ms742875.aspx&lt;/a&gt;&amp;nbsp;(About hosting winforms in wpf)&lt;br /&gt;
&lt;br /&gt;
The sample application included is a basic example using a single person object that in turn has a complex property and a collection property. Once you edit the properties and hit the Ok button, it will display the changes in the object via a messagebox. &lt;br /&gt;
&lt;br /&gt;
Nothing fancy, but you can see some simple mvvm, creation of a simple custom value converter for the complex type “Address” exposed as a property and a the creation of a simple collection editor to allow editing the “Vehicles” collection property. Just enough to get you started.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Update: 4/30/2011&lt;/b&gt;&lt;br /&gt;
&lt;blockquote&gt;A gotcha I forgot to mention is that you'll have to reference System.Windows.Forms which resides in the System.Windows.Forms assembly. The PropertyGrid control resides in this dll. When trying to reference this library from the project references dialog, you won't find it in the list of available dlls. That's because by default your project is using the .Net 4.0 client profile, so go in your project properties window and change it from .Net 4.0 client profile to .Net 4.0. After this step you can try referencing the dll again and it will be in the list.&lt;/blockquote&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-9QnV_qybKqY/TbvFvv_yJAI/AAAAAAAAALU/ZsgwBWPx0Hw/s1600/targetframework4.jp" /&gt;&lt;/div&gt;&lt;blockquote&gt;The Microsoft .NET Framework 4 Client Profile provides a subset of features from the .NET Framework 4. The Client Profile is designed to run client applications and to enable the fastest possible deployment for Windows Presentation Foundation (WPF) and Windows Forms technology. Application developers who require features that are not included in the Client Profile should target the full .NET Framework 4 instead of the Client Profile.&lt;/blockquote&gt;&lt;b&gt;Update: 5/25/2011&lt;/b&gt;&lt;br /&gt;
&lt;blockquote&gt;I was able to change the target framework back to the default Client profile after referencing&amp;nbsp;System.Windows.Forms ; It seems this is already in the Client profile. What will throw you off is if your referencing System.Design.dll which will require the Full version of .NET. One typical requirement will arise for you when developing custom TypeEditors because most of the existing type editors are in the System.Design dll. Still thankfully, my needs for custom type editors was pretty basic and I got away developing one from scratch ( inheriting TypeEditor).&lt;/blockquote&gt;&lt;br /&gt;
I found that not setting height and width explicitly for the control makes its load time slow and at most awkward even. The fix is to be explicit with the height and width. This must be due to layout differences between WPF and Windows Forms. The following article on msdn has the meat and potatoes. &lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://msdn.microsoft.com/en-us/library/ms744952.aspx"&gt;http://msdn.microsoft.com/en-us/library/ms744952.aspx&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
So, in case its not clear, when using the control, this is what you want to attempt : &lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;my&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;WpfPropertyGrid&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;SelectedObject&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"{Binding PersonItem}"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Width&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"290"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Height&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"350"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
            &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;my&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;WpfPropertyGrid&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
Notice the explicit Width and Height above. Now the speed should be super fast! Keep reading the article on msdn I link to above, it has some pretty good information.&lt;br /&gt;
&lt;br /&gt;
And a small correction to the article, it's not entirely true that you cannot style the property grid. You can do some basic styling of the Winforms PropertyGrid. What you cannot do is enjoy a complete designer experience like you can currently with WPF controls. So, it's not as traggic as I made it sound.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;&lt;a href="https://sites.google.com/a/typps.com/home/WpfLab2.zip?attredirects=0&amp;amp;d=1"&gt;Download the sample application&lt;/a&gt;&lt;/h3&gt;&lt;br /&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://blog.typps.com/2011/04/getting-most-out-of-winforms.html"&gt;&lt;img alt="kick it on DotNetKicks.com" border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://blog.typps.com/2011/04/getting-most-out-of-winforms.html" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://dotnetshoutout.com/typps-Getting-the-Most-Out-of-the-Winforms-PropertyGrid-Control-in-WPF" rev="vote-for"&gt;&lt;img alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblog.typps.com%2F2011%2F04%2Fgetting-most-out-of-winforms.html" style="border: 0px;" /&gt;&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/typps/axAH/~4/Bhw7bqms8to" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.typps.com/feeds/9148657924914340455/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.typps.com/2011/04/getting-most-out-of-winforms.html#comment-form" title="8 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/9148657924914340455?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/9148657924914340455?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typps/axAH/~3/Bhw7bqms8to/getting-most-out-of-winforms.html" title="Getting the Most Out of the Winforms PropertyGrid Control in WPF" /><author><name>Alessandro</name><uri>http://www.blogger.com/profile/08620937587114860179</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="24" src="http://4.bp.blogspot.com/-9iFBqNPDD_E/TbtHLjeMgZI/AAAAAAAAAK0/9WMh7OUK6KA/s220/alessandro.png" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/-fCnpTf0Guys/TbrVIzuCkFI/AAAAAAAAAKw/TIicmmZkkyo/s72-c/propertygrid-shabby.jpg" height="72" width="72" /><thr:total>8</thr:total><feedburner:origLink>http://blog.typps.com/2011/04/getting-most-out-of-winforms.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0AARXs4cSp7ImA9WhZQGEU.&quot;"><id>tag:blogger.com,1999:blog-6778277710194170777.post-3893273893240346462</id><published>2011-04-26T09:11:00.000+02:00</published><updated>2011-04-27T09:35:44.539+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-04-27T09:35:44.539+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="WPF" /><title>Custom popup and windows in WPF the MVVM way</title><content type="html">In WPF, using the regular window control to launch child windows makes MVVM and separating a View concern from the ViewModel very difficult.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;The problem:&lt;/b&gt;&lt;br /&gt;
The window control works nicely as a shell for your application, however, launching child windows is problematic because the window control cannot be defined in xaml unless as the root element. &lt;br /&gt;
So, if you wanted to launch child windows inside a parent window, you lose the ability to declare these in xaml. This loss means you will not be able to bind to properties in your ViewModel from the view for the purpose of opening/closing a window. Instead you end up doing this imperatively in code which means more code, more thought, more work. &lt;br /&gt;
&lt;br /&gt;
Following is what you end up doing in the most minimalistic cases.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0px 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color: black;"&gt;MyWindow window = &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; MyWindow();
window.ShowDialog();
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
Adding code such as the above means you are forced to make your ViewModel create instances of your Window and launch them when needed. Clearly something you will not appreciate during testing and since this will provide a tight coupling to a Window control in your ViewModel, it is useless to your tests and eventually breaks your pattern.&lt;br /&gt;
&lt;br /&gt;
What would have been nice instead is if we could do the following :&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0px 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;my&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;ModalDialogPopup&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;IsOpen&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"{Binding FirstPopupIsOpen, Mode=TwoWay}"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
If we could define our window declaratively in Xam as above, then we use the databinding capabilities in WPF and bind to an FirstPopupIsOpen property in our ViewModel, which is what we are after but sadly not currently possible.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;The solution 1:&lt;/b&gt; A custom Control that behaved like a Window&lt;br /&gt;
Writing a custom control was pretty simple as we make use of the existing Popup control in Wpf. The popup control is pretty wild and requires taming but solves this problem.&lt;br /&gt;
&lt;br /&gt;
Some reasons to design our solution around the existing Popup control:&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;The popup control is designed  to stay always ontop, which has it's pitfalls but it solves more  problems than it brings. More specifically we will want the ability  to hide the content under the popup while the popup is in view.   &lt;/li&gt;
&lt;li&gt;The popup control can be positioned in so many ways,  however what we are after is the ability to define a set of Left,  Top coordinates and this is supported out of the box.   &lt;/li&gt;
&lt;li&gt;The popup control supports a few animations out of the  box. This means we can apply a nice sliding or a fade effect with  zero effort.&lt;/li&gt;
&lt;li&gt;The popup control can contain child controls  obviously. This is great and serves our purpose very well.   &lt;/li&gt;
&lt;li&gt;Enjoy the beauty of an adorner masking the background  beneath the ModalDialogPopup.&lt;br /&gt;
&lt;/li&gt;
&lt;/ol&gt;The solution we end up with is a control like the following :&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0px 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;my&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;ModalDialogPopup&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;IsOpen&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"{Binding FirstPopupIsOpen, Mode=TwoWay}"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
            &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;my&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;ModalDialogPopup&lt;/span&gt;&lt;span style="color: black;"&gt;.&lt;/span&gt;&lt;span style="color: red;"&gt;HostedContent&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;ContentControl&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Grid&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Height&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"200"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Width&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"300"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                        &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;TextBlock&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;VerticalAlignment&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Center"&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;                             HorizontalAlignment&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Center"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;FontSize&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"20"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                            This is the first modal popup
                        &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;TextBlock&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Grid&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;ContentControl&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
            &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;my&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;ModalDialogPopup&lt;/span&gt;&lt;span style="color: black;"&gt;.HostedContent&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;my&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;ModalDialogPopup&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
One inherent problem I had not considered while writing this control is that just like the Window control, even with a custom popup control, the limitation to not being able to nest a popup in another popup existed. That's because when showing the child, we are forced to hide the parent for technical reasons that exist in the popup control only(the popup control will always be the top most control).  &lt;br /&gt;
&lt;br /&gt;
That means if the parent is larger than the child in dimention, portions of controls in the parent will show and interaction with those pieces becomes possible. When a child popup is launched, we want it to behave as a modal window, so it shouldn't be able to interact with controls in popups beneat it. Sadly the adorner cannot help us here as it cannot cover the “Always ontop” popup.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ofcourse, the solution to deal with this limitation is to hide the parent when the child is in view, which doesn't help the nesting because if the parent is hidden, then the child will be hidden too!  I guess there is a good reason why the Window control in WPF does not allow nesting! Too bad I had to discover this at my own expense.&lt;br /&gt;
&lt;br /&gt;
Even with this shortcomings, in most cases, you can workaround the nesting limitation by designing your solutions with this drawback in mind.&lt;br /&gt;
&lt;br /&gt;
As you can note from the piece of xaml code in the previous code listing above, we have a control that can be defined in our view declaratively that takes content via the HostedContent template. We can also set a Title, and content in it's HostedContent template. In the previous code listing, everytime the property FirstPopupIsOpen evaluates to “true” in our ViewModel, the popup will open.&lt;br /&gt;
&lt;br /&gt;
By default the custom popup provides an OK and Cancel button whose caption/visibility you can set. If you need more customizations you can very well customize its template by providing a custom style. Attemping to supply custom styling is quite simple because the default markup we use is plain and this is intentional since styling is subjective and a trival matter. This enables you to provide custom styling of your own without fighting your way through heavy use of xaml.&lt;br /&gt;
&lt;br /&gt;
If you take a look at the code listing below, all we have is a 3 row grid, one holding the title, the second holding the content you define in the HostedContent template and the last row to hold the OK and Cancel buttons.&amp;nbsp;Simply put, you are in control of the styling and the default style template is prive of any bloated styling markup to distract you.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0px 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;ResourceDictionary&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;xmlns&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"http://schemas.microsoft.com/winfx/2006/xaml/presentation"&lt;/span&gt;&lt;span style="color: black;"&gt;
                    &lt;/span&gt;&lt;span style="color: red;"&gt;xmlns&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: red;"&gt;x&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"http://schemas.microsoft.com/winfx/2006/xaml"&lt;/span&gt;&lt;span style="color: black;"&gt;
                    &lt;/span&gt;&lt;span style="color: red;"&gt;xmlns&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: red;"&gt;local&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"clr-namespace:WpfPopup.Controls"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Style&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;TargetType&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"{x:Type local:ModalDialogPopup}"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Setter&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Property&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Template"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
            &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Setter&lt;/span&gt;&lt;span style="color: black;"&gt;.&lt;/span&gt;&lt;span style="color: red;"&gt;Value&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;ControlTemplate&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;                     TargetType&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"{x:Type local:ModalDialogPopup}"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Popup&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;x&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: red;"&gt;Name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"dialog"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;AllowsTransparency&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"True"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                        &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Grid&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;x&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: red;"&gt;Name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"content"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                            &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Grid&lt;/span&gt;&lt;span style="color: black;"&gt;.&lt;/span&gt;&lt;span style="color: red;"&gt;RowDefinitions&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                                &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;RowDefinition&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Height&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"20"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                                &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;RowDefinition&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Height&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"*"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                                &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;RowDefinition&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Height&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"20"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                            &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Grid&lt;/span&gt;&lt;span style="color: black;"&gt;.RowDefinitions&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                            &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;TextBlock&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Grid&lt;/span&gt;&lt;span style="color: black;"&gt;.&lt;/span&gt;&lt;span style="color: red;"&gt;Row&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"0"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;x&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: red;"&gt;Name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"title"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
    &lt;/span&gt;&lt;span style="color: green;"&gt;&amp;lt;!-- the hosted content --&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                            &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;ContentPresenter&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;x&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: red;"&gt;Name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"contentHost"&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;                                Grid&lt;/span&gt;&lt;span style="color: black;"&gt;.&lt;/span&gt;&lt;span style="color: red;"&gt;Row&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"1"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Margin&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"5"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                            &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;StackPanel&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Grid&lt;/span&gt;&lt;span style="color: black;"&gt;.&lt;/span&gt;&lt;span style="color: red;"&gt;Row&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"2"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Orientation&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Horizontal"&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;                                HorizontalAlignment&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Right"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                                &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Button&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Content&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Ok"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;x&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: red;"&gt;Name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"buttonOK"&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;                                  MinWidth&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"100"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Margin&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"0,0,5,0"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                                &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Button&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Content&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Cancel"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;x&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: red;"&gt;Name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"buttonCancel"&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;                                   MinWidth&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"100"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Margin&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"0,0,5,0"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                            &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;StackPanel&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                        &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Grid&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Popup&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
                &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;ControlTemplate&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
            &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Setter&lt;/span&gt;&lt;span style="color: black;"&gt;.Value&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Setter&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Style&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;ResourceDictionary&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
And here it is when popped open in the example solution attached to this post :&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-rqQ4q49UEhw/TbZoII59ECI/AAAAAAAAAJ8/kFjlqJx1Two/s1600/modalpopup.jpg" /&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
It's actually quite a minimal control without any bells and whistles and leave the responsibility of styling it in your hands, any way you want. Notice the adorner masking the background and the convenient Ok/Cancel buttons. It's so much simpler to use and easy to test. The amount of work need to set this up compared to a window control (solution 2 below) is minimal.&lt;br /&gt;
&lt;br /&gt;
Surely there is some value for such a control but it is most definitely not a resonable replacement to the Window control entirely, as far as launching child windows is concerned.&lt;br /&gt;
&lt;br /&gt;
Fortunately, the needed work to decouple the existing Window control from the ViewModel could very well be done with a simple interface contract, which brings us to the next solution.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Solution 2 :&lt;/b&gt; Decoupling the Window control from the ViewModel&lt;br /&gt;
This solution relies on dependency injection to provide proper decoupling of the Window control from the viewmodel. So, we'll need a dependency injection container. You may use any at this point, I'll be using  Microsofts Unity. This will also make it a breeze to inject a stub in place of the Window control when Unit testing.&lt;br /&gt;
&lt;br /&gt;
First, let's look at what we're going to construct. It's going to be a series of dialogs. The main Window, and 2 child windows.&lt;br /&gt;
&lt;br /&gt;
The main window :&lt;br /&gt;
&lt;div style="margin-bottom: 0in; margin-top: 0in;"&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-GUAtOv-vVP4/TbZo3duTmSI/AAAAAAAAAKA/mQat0x2CBxg/s1600/mainwindow.jpg" /&gt;&lt;/div&gt;&lt;br /&gt;
A child window launched from the main window above :&lt;br /&gt;
&lt;div style="margin-bottom: 0in; margin-top: 0in;"&gt;&lt;br /&gt;
&lt;/div&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-9I-uvDyq580/TbZo7jOOa4I/AAAAAAAAAKE/DEuWCYAzDyU/s1600/childwindow.jpg" /&gt;&lt;/div&gt;And finally another child window launched from the child window above :&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-F8m2otRnQuE/TbZo9QK949I/AAAAAAAAAKI/prP1-v16J6c/s1600/childwindownested.jpg" /&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;
Ok, now that we know what we are building, lets start. The procedure is simple. We'll start by defining the common functionality we'd normally use from the Window Control in our ViewModel by throwing it into an interface.  &lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0px 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;interface&lt;/span&gt;&lt;span style="color: black;"&gt; IChildWindow
{
 &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; Close();
 &lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;? ShowDialog();
 &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; SetOwner(&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt; window);
 &lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;? DialogResult { &lt;/span&gt;&lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;; &lt;/span&gt;&lt;span style="color: blue;"&gt;set&lt;/span&gt;&lt;span style="color: black;"&gt;; }
}
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
We do this for each View that is a window control. The reason is simple, in order to decouple the window from the viewmodel, what we are going to do is use constructor injection. By injecting this dependency in the constructor we can ensure easy substitution with fake stubs during testing and everything will just work.&lt;br /&gt;
&lt;br /&gt;
Let's also decouple the window we plan to launch by adding yet another interface :&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0px 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;interface&lt;/span&gt;&lt;span style="color: black;"&gt; IChildWindowNested
{
 &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; Close();
 &lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;? ShowDialog();
 &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; SetOwner(&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt; window);
 &lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;? DialogResult { &lt;/span&gt;&lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;; &lt;/span&gt;&lt;span style="color: blue;"&gt;set&lt;/span&gt;&lt;span style="color: black;"&gt;; }
}
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
And finally, the viewmodel:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0px 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&lt;span style="color: black;"&gt; ChildWindowViewModel
{
 &lt;/span&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;readonly&lt;/span&gt;&lt;span style="color: black;"&gt; IChildWindow _childWindow;
 &lt;/span&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;readonly&lt;/span&gt;&lt;span style="color: black;"&gt; IChildWindowNested _childWindowNested;
 &lt;/span&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;&lt;span style="color: black;"&gt; ICommand _okCommand;

 &lt;/span&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;&lt;span style="color: black;"&gt; ICommand _openCommand;

 &lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; ChildWindowViewModel(IChildWindow childWindow,
    IChildWindowNested childWindowNested)
 {
  _childWindow = childWindow;
  _childWindowNested = childWindowNested;
 }

 &lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; ICommand OpenCommand
 {
  &lt;/span&gt;&lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;
  {
   &lt;/span&gt;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&lt;span style="color: black;"&gt; _openCommand ?? (_openCommand =
    &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; DelegateCommand(OpenClick));
  }
 }

 &lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; ICommand OkCommand
 {
  &lt;/span&gt;&lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;
  {
   &lt;/span&gt;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&lt;span style="color: black;"&gt; _okCommand ?? (_okCommand =
    &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; DelegateCommand(OkClick));
  }
 }

 &lt;/span&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; OpenClick()
 {
  _childWindowNested.SetOwner(_childWindow);
  _childWindowNested.ShowDialog();
 }

 &lt;/span&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; OkClick()
 {
  _childWindow.DialogResult = &lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;;
  _childWindow.Close();
 }
}
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
As  you can notice in the above class, we are passing 2 interfaces in the constructor. The first is the window using this viewmodel : ChildWindow.xaml and the second is the window it will launch : ChildWindowNested.xaml &lt;br /&gt;
&lt;br /&gt;
The relationship between these two windows is a typical parent child relationship, ChildWindow.xaml is the parent, while ChildWindowNested.xaml is the child being launched.&lt;br /&gt;
&lt;br /&gt;
The reason we pass IChildWindow, the parent window in this case is because :&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;We need to tell the child Window we are launching who it's owner is. That way we can nicely position the child window relative to it's parent.&lt;/li&gt;
&lt;li&gt;As this is the viewmodel of ChildWindow.xaml, we will also want to handle closing this parent window when it's Ok and Cancel buttons are clicked.&lt;/li&gt;
&lt;/ol&gt;The reason we pass IChildWindowNested, the child window we are launching is because:&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;We want to launch this dialog based on an action in ChildWindow.xaml&lt;/li&gt;
&lt;li&gt;To decouple the window from the viewmodel, because we will be using dependency injection to set up this dependency on the window control.&lt;/li&gt;
&lt;/ol&gt;&lt;br /&gt;
Now the codebehind for ChildWindow.xaml :&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0px 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;partial&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&lt;span style="color: black;"&gt; ChildWindow : Window, IChildWindow
{
 &lt;/span&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;readonly&lt;/span&gt;&lt;span style="color: black;"&gt; IUnityContainer _container;

 &lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; ChildWindow()
 {
  InitializeComponent();
  _container = UnityContainerResolver.Container;
  &lt;/span&gt;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&lt;span style="color: black;"&gt; childWindowNested =&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color: black;"&gt;                             _container.Resolve&amp;lt;IChildWindowNested&amp;gt;();

  DataContext = &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; ChildWindowViewModel(&lt;/span&gt;&lt;span style="color: blue;"&gt;this&lt;/span&gt;&lt;span style="color: black;"&gt;,&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color: black;"&gt;                                                childWindowNested);
  Closing += ChildWindowClosing;
 }

 #region IChildWindow Members

 &lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; SetOwner(&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt; window)
 {
  Owner = window &lt;/span&gt;&lt;span style="color: blue;"&gt;as&lt;/span&gt;&lt;span style="color: black;"&gt; Window;
 }

 #endregion

 &lt;/span&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; ChildWindowClosing(&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt; sender, CancelEventArgs e)
 {
  e.Cancel = &lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;;
  Visibility = Visibility.Hidden;
 }
}
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
We can setup the viewmodel binding to the DataContext in several ways however doing this in the codebehind of the view as in the sample code above is the most flexible of all solutions especially if your moving beyond the typical blog post samples. &lt;br /&gt;
&lt;br /&gt;
At the end of the day, it's only a matter of opinion and what's important is that there is no dependency impeding you from testing your application. And ofcourse that it does not break your pattern. In our case, it's both convenient as we want to pass a reference of the current window to the viewmodel and at the same time, we want to do it in a very decoupled way to help us test our viewmodel.&lt;br /&gt;
&lt;br /&gt;
Also note that we are hooking into the closing handler and cancelling the default close behavior of the Window control. That's because once a window is closed, we cant reopen it and we'll need to create a new instance of the window. &lt;br /&gt;
Certainly this depends on your use case. For me, my requirements are such that I need to reuse the window being closed. This works nicely. &lt;br /&gt;
&lt;br /&gt;
What about dependency injection and how is our Unity container setup ? It's quite simple in this case. For this sample code, I just created a singleton that registers all dependencies with the container : &lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0px 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&lt;span style="color: black;"&gt; UnityContainerResolver
{
 &lt;/span&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;static&lt;/span&gt;&lt;span style="color: black;"&gt; IUnityContainer _container;

 &lt;/span&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;&lt;span style="color: black;"&gt; UnityContainerResolver()
 {
 }

 &lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;static&lt;/span&gt;&lt;span style="color: black;"&gt; IUnityContainer Container
 {
  &lt;/span&gt;&lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;
  {
   &lt;/span&gt;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&lt;span style="color: black;"&gt; (_container == &lt;/span&gt;&lt;span style="color: blue;"&gt;null&lt;/span&gt;&lt;span style="color: black;"&gt;)
   {
    _container = &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; UnityContainer();
    RegisterTypes();
   }
   &lt;/span&gt;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&lt;span style="color: black;"&gt; _container;
  }
 }

 &lt;/span&gt;&lt;span style="color: blue;"&gt;static&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; RegisterTypes()
 {
  _container.RegisterType&amp;lt;IMainWindow, MainWindow&amp;gt;();
  _container.RegisterType&amp;lt;IChildWindow, ChildWindow&amp;gt;();
  _container.RegisterType&amp;lt;IChildWindowNested,&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color: black;"&gt;                                                    ChildWindowNested&amp;gt;();
 }
}
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
That's it. By registering dependencies with a dependency container, it becomes so easy to inject fakes in our viewmodel in place of the real object. In our case, when testing, we can  make a different registration by mapping to our fake mock objects and this is relatively simple when using a dependency container :&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0px 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;static&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; RegisterTypes()
{
 _container.RegisterType&amp;lt;IMainWindow, MockMainWindow&amp;gt;();
 _container.RegisterType&amp;lt;IChildWindow, MockChildWindow&amp;gt;();
 _container.RegisterType&amp;lt;IChildWindowNested,&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color: black;"&gt;                                  MockChildWindowNested&amp;gt;();
}
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
And MockChildWindow having enough code to satisfy the contract of IChildWindow. &lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0px 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&lt;span style="color: black;"&gt; MockChildWindow : IChildWindow
{
 &lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt; Owner { &lt;/span&gt;&lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;; &lt;/span&gt;&lt;span style="color: blue;"&gt;set&lt;/span&gt;&lt;span style="color: black;"&gt;; }
 &lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; Close()
 {
  &lt;/span&gt;&lt;span style="color: green;"&gt;//&lt;/span&gt;&lt;span style="color: black;"&gt;
 }

 &lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;? ShowDialog()
 {
  &lt;/span&gt;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;;
 }

 &lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; SetOwner(&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt; window)
 {
  Owner = window;
 }

 &lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt;? DialogResult
 {
  &lt;/span&gt;&lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;;
  &lt;/span&gt;&lt;span style="color: blue;"&gt;set&lt;/span&gt;&lt;span style="color: black;"&gt;;
 }
}
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
The viewmodel itself only uses methods exposed by the contract so we are safe to use a fake object as above for testing.&lt;br /&gt;
&lt;br /&gt;
I have added a test app containing the custom popup and all code discussed here. Be sure to check it out!&lt;br /&gt;
&lt;a href="https://sites.google.com/a/typps.com/home/WpfLab.zip?attredirects=0&amp;amp;d=1"&gt;Download sample application&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://blog.typps.com/2011/04/custom-popup-and-windows-in-wpf-mvvm.html"&gt;&lt;img alt="kick it on DotNetKicks.com" border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblog.typps.com%2f2011%2f04%2fcustom-popup-and-windows-in-wpf-mvvm.html" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://dotnetshoutout.com/typps-Custom-popup-and-windows-in-WPF-the-MVVM-way" rev="vote-for"&gt;&lt;img alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblog.typps.com%2F2011%2F04%2Fcustom-popup-and-windows-in-wpf-mvvm.html" style="border: 0px currentColor;" /&gt;&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/typps/axAH/~4/aq_rrNZIvJI" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.typps.com/feeds/3893273893240346462/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.typps.com/2011/04/custom-popup-and-windows-in-wpf-mvvm.html#comment-form" title="19 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/3893273893240346462?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/3893273893240346462?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typps/axAH/~3/aq_rrNZIvJI/custom-popup-and-windows-in-wpf-mvvm.html" title="Custom popup and windows in WPF the MVVM way" /><author><name>Alessandro</name><uri>http://www.blogger.com/profile/08620937587114860179</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="24" src="http://4.bp.blogspot.com/-9iFBqNPDD_E/TbtHLjeMgZI/AAAAAAAAAK0/9WMh7OUK6KA/s220/alessandro.png" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-rqQ4q49UEhw/TbZoII59ECI/AAAAAAAAAJ8/kFjlqJx1Two/s72-c/modalpopup.jpg" height="72" width="72" /><thr:total>19</thr:total><feedburner:origLink>http://blog.typps.com/2011/04/custom-popup-and-windows-in-wpf-mvvm.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0QFSXY4cSp7ImA9WhZQFEw.&quot;"><id>tag:blogger.com,1999:blog-6778277710194170777.post-6389047413216674090</id><published>2011-04-21T19:11:00.000+02:00</published><updated>2011-04-21T21:48:38.839+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-04-21T21:48:38.839+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET MVC" /><title>Display and Editor Templated View Helpers in ASP.NET MVC</title><content type="html">Templated view helpers simplify your work greatly by allowing you to specify that rendering is required for display or input via Display and Editor templated Helpers respectively without needing to explicitly specify what Html element to map the properties to, in our model.&lt;br /&gt;
&lt;br /&gt;
For example, given the following controller that is passing a model to the view : &lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: green;"&gt;// Get: /Home/Edit/1&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; ActionResult Edit(&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; id)
{
 &lt;/span&gt;&lt;span style="color: blue;"&gt;var&lt;/span&gt;&lt;span style="color: black;"&gt; p = GetPerson(id);
 &lt;/span&gt;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&lt;span style="color: black;"&gt; View(p);
}
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
Now, without resorting to Templated View Helpers, in order to display our model for edit, in the view we can attempt to offload the task of rendering an appropriate Html element for a property in our model to a View Helper :&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;@model MvcLab.Models.Person

@{
    ViewBag.Title = "Edit a person";
}

@using (Html.BeginForm()) {
    @Html.TextBox(Model.FirstName);
}
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
in the above example, Html.TextBox will output :&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;input&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;id&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"FirstName"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"FirstName"&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;       type&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"text"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Alessandro"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;


&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
By using the Html.TextBox helper, we were able to specify explicitly what input element we wanted mapped to a property in our model. &lt;br /&gt;
&lt;br /&gt;
This is nice but we've had to explicitly state that we wanted a TextBox mappped to the FirstName field. &lt;br /&gt;
&lt;br /&gt;
The same can be achieved by using Templated View Helpers which are yet a more convenient way to associate an Html element to a property in our model :&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;@using (Html.BeginForm()) {
    @Html.EditorFor(x =&amp;gt; x.FirstName);
}
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
The above piece of code will also render an Html input element :&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;input&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;class&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"text-box single-line"&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;      id&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"FirstName"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"FirstName"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;type&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"text"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Alessandro"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
The output is pretty much the same, but notice that we didn't explicitly state what element to associate to the FirstName property. How this works is that it bases its assumption on the data type of the property and whatever model&amp;nbsp;attribute meta data decorations set on it.&lt;br /&gt;
&lt;br /&gt;
In ASP.NET MVC 2 onwards, there are 3 Editor Helpers that do the same thing with slight differences in usage. @Html.Editor, @Html.EditorFor and @Html.EditorForModel, the first can take a string containing our property name and the second a strongly typed model to&amp;nbsp;property mapping expression and the third will just use the strongly typed model passed to the view by default.&lt;br /&gt;
&lt;br /&gt;
They each provide some flexibility how the Editor Template View Helper is used. We'll be using @Html.EditorFor and&amp;nbsp;@Html.EditorForModel for the remainder of this post.&lt;br /&gt;
&lt;br /&gt;
The following piece of code in the view uses EditorForModel.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;@model MvcLab.Models.Person

@{
    ViewBag.Title = "A person";
}

@using (Html.BeginForm()) {
    @Html.EditorForModel();
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;p&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;input&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;type&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"submit"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Save"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;p&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
}

&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;p&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
    @Html.ActionLink("Back to List", "Index")
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;p&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
Something as simple as @Html.EditorForModel(); will output an input element for each property in our model. Notice how we didn't loop nor needed to pass the model. Since we're passing the Model in the view, using the default no arguments&amp;nbsp;overloads works just nicely.  The above piece of code in the view will output to screen :&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-5U7esawBNvI/TbBcqHVd8CI/AAAAAAAAAIg/IXwm5vRnzhg/s1600/EditorForModel1.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-5U7esawBNvI/TbBcqHVd8CI/AAAAAAAAAIg/IXwm5vRnzhg/s1600/EditorForModel1.gif" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
While this renders our model, we can see that the output needs a bit more tweaking, for example the field labels are using the property name, this can be tweaked on the model itself using attribute decorations such as :&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;[DisplayName("&lt;/span&gt;&lt;span style="color: darkred;"&gt;First name&lt;/span&gt;&lt;span style="color: black;"&gt;")]
&lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt; FirstName { &lt;/span&gt;&lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;; &lt;/span&gt;&lt;span style="color: blue;"&gt;set&lt;/span&gt;&lt;span style="color: black;"&gt;; }
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
Next, the id field is displaying. We'd rather this was not displayed, as this too can be compensated for by providing another attribute decoration on the property such as :&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;[HiddenInput(DisplayValue = &lt;/span&gt;&lt;span style="color: blue;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;)]
&lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; PersonId { &lt;/span&gt;&lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;; &lt;/span&gt;&lt;span style="color: blue;"&gt;set&lt;/span&gt;&lt;span style="color: black;"&gt;; }
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
Completed person class after the noted changes :&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&lt;span style="color: black;"&gt; Person
{
 [HiddenInput(DisplayValue = &lt;/span&gt;&lt;span style="color: blue;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;)]
 &lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; PersonId { &lt;/span&gt;&lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;; &lt;/span&gt;&lt;span style="color: blue;"&gt;set&lt;/span&gt;&lt;span style="color: black;"&gt;; }
 [DisplayName("&lt;/span&gt;&lt;span style="color: darkred;"&gt;First name&lt;/span&gt;&lt;span style="color: black;"&gt;")]
 &lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt; FirstName { &lt;/span&gt;&lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;; &lt;/span&gt;&lt;span style="color: blue;"&gt;set&lt;/span&gt;&lt;span style="color: black;"&gt;; }
 [DisplayName("&lt;/span&gt;&lt;span style="color: darkred;"&gt;Last name&lt;/span&gt;&lt;span style="color: black;"&gt;")]
 &lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt; LastName { &lt;/span&gt;&lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;; &lt;/span&gt;&lt;span style="color: blue;"&gt;set&lt;/span&gt;&lt;span style="color: black;"&gt;; }
 &lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; Address Residence { &lt;/span&gt;&lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt;; &lt;/span&gt;&lt;span style="color: blue;"&gt;set&lt;/span&gt;&lt;span style="color: black;"&gt;; }
}
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
This renders as below :&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-LIAI4nFQNj0/TbBcqlPUgrI/AAAAAAAAAIk/n4LFnxMkfdo/s1600/EditorForModel2.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-LIAI4nFQNj0/TbBcqlPUgrI/AAAAAAAAAIk/n4LFnxMkfdo/s1600/EditorForModel2.gif" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
That's much better, yet now we can note that our complex property Residence, which is a class with its own set of properties, did not render. In order to fix this we can try the following : &lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;@using (Html.BeginForm()) {
    @Html.EditorForModel();
 @Html.EditorFor(x =&amp;gt; x.Residence);
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;p&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;input&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;type&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"submit"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Save"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;p&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
}

&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;p&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
    @Html.ActionLink("Back to List", "Index")
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;p&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
While this is all great and works by convention, we do eventually lose fine grained control over how each field is rendered in the end. One option is to go back to rendering each field individually eg: @Html.EditorFor(x =&amp;gt; x.FirstName)&amp;nbsp;directly in the view, but then if we need to reuse this model in another view, we keep repeating each field over and over again. The extra code in each view also begins to weigh on us and becomes unmaintainable sooner than later.&lt;br /&gt;
&lt;br /&gt;
One way to solve this is to move the logic into a Partial view which can then be reused in a single line of code on other views where we'd want to render a Person model. An even nicer approach is to use convention, again designating&amp;nbsp;Editor and Display templates for our Templated View Helpers. Using this approach will result in providing us with fine grained control over the output. Lets try that now.&lt;br /&gt;
&lt;br /&gt;
Instead of creating the rendering for our model in the view directly, lets move it to a partial view. Following convention for Templated View Helpers, the partial view (template) needs to be stored in the following locations :&lt;br /&gt;
/Views/Shared/DisplayTemplates/TemplateName.cshtml and /Views/Shared/EditorTemplates/TemplateName.cshtml.&lt;br /&gt;
&lt;br /&gt;
And since our model is a Person object, we will name our template Person.cshtml for both Display and Editor templates. This is to follow convention as that is the template it will look for based on the type name. There is still more flexibility in defining a template by using meta data attributes &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.uihintattribute.aspx"&gt;UIHint&lt;/a&gt; and passing a template name there or by passing a template explicitly in the Templated View Helper itself.&lt;br /&gt;
&lt;br /&gt;
To keep the post simple we are going to use only default convention and setup custom EditorTemplates as the procedure is the same for DisplayTemplates. Along with the article I've included a sample application that shows usage for both Display and Editor templates.&lt;br /&gt;
&lt;br /&gt;
/Views/Shared/EditorTemplates/Person.cshtml :&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;@model MvcLab.Models.Person

@Html.ValidationSummary(true)
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;fieldset&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;legend&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;Person&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;legend&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;

    @Html.HiddenFor(model =&amp;gt; model.PersonId)

    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;div&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;class&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"editor-label"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
        @Html.LabelFor(model =&amp;gt; model.FirstName)
    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;div&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;div&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;class&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"editor-field"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
        @Html.EditorFor(model =&amp;gt; model.FirstName)
        @Html.ValidationMessageFor(model =&amp;gt; model.FirstName)
    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;div&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;

    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;div&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;class&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"editor-label"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
        @Html.LabelFor(model =&amp;gt; model.LastName)
    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;div&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;div&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;class&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"editor-field"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
        @Html.EditorFor(model =&amp;gt; model.LastName)
        @Html.ValidationMessageFor(model =&amp;gt; model.LastName)
    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;div&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;fieldset&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;

&lt;/span&gt;&lt;span style="color: green;"&gt;&amp;lt;!-- Note that the Residence property is a complex type for whom
 we've defined a template too in : Views\EditorTemplates\Address.cshtml --&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
 @Html.EditorFor(model =&amp;gt; model.Residence)
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
and now a Template for Address, though we could have done it in the Person object itself, as this gives us the flexibility of reusing the Address models rendering in any view.&lt;br /&gt;
&lt;br /&gt;
/Views/Shared/EditorTemplates/Address.cshtml&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;@model MvcLab.Models.Address

    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;fieldset&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;legend&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;Address&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;legend&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;

        &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;div&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;class&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"editor-label"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
            @Html.LabelFor(model =&amp;gt; model.City)
        &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;div&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;div&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;class&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"editor-field"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
            @Html.EditorFor(model =&amp;gt; model.City)
            @Html.ValidationMessageFor(model =&amp;gt; model.City)
        &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;div&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;

        &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;div&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;class&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"editor-label"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
            @Html.LabelFor(model =&amp;gt; model.Country)
        &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;div&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;div&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;class&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"editor-field"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
            @Html.EditorFor(model =&amp;gt; model.Country)
            @Html.ValidationMessageFor(model =&amp;gt; model.Country)
        &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;div&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;fieldset&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
Note that, upto this point, we have not written a single line of code. What we see so far is all auto generated scaffolding when electing to set a strongly typed class on a partial view in Visual Studio itself. This is much quicker and we're left with&amp;nbsp;remodeling&amp;nbsp;the&amp;nbsp;markup to fit our design requirements.&lt;br /&gt;
&lt;br /&gt;
Below is a screenshot of the Add View dialog that we've used to autogenerate most of the code above.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-8rvC0cCC_F8/TbBcrXvrrSI/AAAAAAAAAIs/Xu129A7oGnA/s1600/scaffolding.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-8rvC0cCC_F8/TbBcrXvrrSI/AAAAAAAAAIs/Xu129A7oGnA/s1600/scaffolding.gif" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
Next, we are finally ready to reuse these templates in our views : &lt;br /&gt;
&lt;br /&gt;
/Views/Home/edit.csHtml&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;@model MvcLab.Models.Person

@{
    ViewBag.Title = "Edit a person";
}

@using (Html.BeginForm()) {
    @Html.EditorForModel();
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;p&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;input&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;type&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"submit"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Save"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;p&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
}

&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;p&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
    @Html.ActionLink("Back to List", "Index")
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;p&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
Note that the code in our view and every other view that wants to reuse the Person model has gone down to a single method call : @Html.EditorForModel()&lt;br /&gt;
&lt;br /&gt;
and the output :&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-9Uq6LhbzodM/TbBcq7XPhCI/AAAAAAAAAIo/oOeK3sbZYT0/s1600/finalview.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="320" src="http://1.bp.blogspot.com/-9Uq6LhbzodM/TbBcq7XPhCI/AAAAAAAAAIo/oOeK3sbZYT0/s320/finalview.gif" width="201" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Final conclusions :&lt;br /&gt;
As we have seen, by using Templated View Helpers, we were able to promote reusability of our models rendering and in the process we were not limited by typical black box solutions as we were able to gain fine grained control by defining&amp;nbsp;custom templates for our models.&lt;br /&gt;
&lt;br /&gt;
Best yet, we followed convention over configuration and ended up doing little work with improved productivity and maintainability of our code. Following the same procedure here on this post, we can define&amp;nbsp;custom&amp;nbsp;templates&amp;nbsp;for Display View Helpers too in the same way we provided templates for Editors.&lt;br /&gt;
&lt;br /&gt;
The sample application provided along with this post defines templates for both Display and Editor Helpers.&lt;br /&gt;
&lt;br /&gt;
This is what it all looks like in solution explorer :&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-48ZoAvIyQNQ/TbBcrz6alFI/AAAAAAAAAIw/kZiWBOFMnmM/s1600/solutionexplorer.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="320" src="http://2.bp.blogspot.com/-48ZoAvIyQNQ/TbBcrz6alFI/AAAAAAAAAIw/kZiWBOFMnmM/s320/solutionexplorer.gif" width="228" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
Sample application : &lt;a href="https://sites.google.com/a/typps.com/home/MvcLab.zip?attredirects=0&amp;amp;d=1"&gt;download&lt;/a&gt;&lt;br /&gt;
Reference material :&lt;br /&gt;
&lt;a href="http://msdn.microsoft.com/en-us/library/ee402949.aspx"&gt;http://msdn.microsoft.com/en-us/library/ee402949.aspx&lt;/a&gt;&lt;br /&gt;
&lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.uihintattribute.aspx"&gt;http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.uihintattribute.aspx&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http://blog.typps.com/2011/04/display-and-editor-templated-view.html"&gt;&lt;img alt="kick it on DotNetKicks.com" border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblog.typps.com%2f2011%2f04%2fdisplay-and-editor-templated-view.html" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://dotnetshoutout.com/typps-Display-and-Editor-Templated-View-Helpers-in-ASPNET-MVC" rev="vote-for"&gt;&lt;img alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblog.typps.com%2F2011%2F04%2Fdisplay-and-editor-templated-view.html" style="border: 0px;" /&gt;&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/typps/axAH/~4/rY-fQmmuX7o" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.typps.com/feeds/6389047413216674090/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.typps.com/2011/04/display-and-editor-templated-view.html#comment-form" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/6389047413216674090?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/6389047413216674090?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typps/axAH/~3/rY-fQmmuX7o/display-and-editor-templated-view.html" title="Display and Editor Templated View Helpers in ASP.NET MVC" /><author><name>Alessandro</name><uri>http://www.blogger.com/profile/08620937587114860179</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="24" src="http://4.bp.blogspot.com/-9iFBqNPDD_E/TbtHLjeMgZI/AAAAAAAAAK0/9WMh7OUK6KA/s220/alessandro.png" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-5U7esawBNvI/TbBcqHVd8CI/AAAAAAAAAIg/IXwm5vRnzhg/s72-c/EditorForModel1.gif" height="72" width="72" /><thr:total>1</thr:total><feedburner:origLink>http://blog.typps.com/2011/04/display-and-editor-templated-view.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DE4ASHc6fip7ImA9WhZQFEs.&quot;"><id>tag:blogger.com,1999:blog-6778277710194170777.post-1162616031239673728</id><published>2011-04-19T12:11:00.000+02:00</published><updated>2011-04-22T12:09:09.916+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-04-22T12:09:09.916+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Tip" /><title>MARS - Multiple Active Result Sets support in SQL Server 2005 and above</title><content type="html">SQL Server 2005 onwards supports MARS - Multiple Active Result Sets. MARS enables you to reuse an existing connection to perform operations on SQL Server. This makes MARS a viable alternative to server-side cursors with significant performance boosts. As powerful that may seem it has it's drawbacks so care needs to be taken when using it.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here's an example from msdn:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;SqlCommand cmd = conn.CreateCommand();
SqlCommand cmd2 = conn.CreateCommand();
cmd.CommandText = @"&lt;/span&gt;&lt;span style="color: darkred;"&gt;select operation_id, operation_code, product_id, quantity 
      from dbo.operations where processed=0&lt;/span&gt;&lt;span style="color: black;"&gt;";
cmd2.CommandText = @"&lt;/span&gt;&lt;span style="color: darkred;"&gt;update dbo.operations set processed=1 
      where operation_id=@operation_id&lt;/span&gt;&lt;span style="color: black;"&gt;";
SqlParameter opid=cmd2.Parameters.Add("&lt;/span&gt;&lt;span style="color: darkred;"&gt;@operation_id&lt;/span&gt;&lt;span style="color: black;"&gt;", SqlDbType.Int);
reader=cmd.ExecuteReader();
&lt;/span&gt;&lt;span style="color: blue;"&gt;while&lt;/span&gt;&lt;span style="color: black;"&gt; (reader.Read())
{
   ProcessOperation();
   opid.Value=reader.GetInt32(0); &lt;/span&gt;&lt;span style="color: green;"&gt;// operation_id&lt;/span&gt;&lt;span style="color: black;"&gt;
   &lt;/span&gt;&lt;span style="color: green;"&gt;//notice how we are trying to execute an update query on the second command&lt;/span&gt;&lt;span style="color: black;"&gt;
   &lt;/span&gt;&lt;span style="color: green;"&gt;//this is going to fail miserably because&lt;/span&gt;&lt;span style="color: black;"&gt;
   &lt;/span&gt;&lt;span style="color: green;"&gt;//we did not set MutipleActiveResultSets=Yes &lt;/span&gt;&lt;span style="color: black;"&gt;
   &lt;/span&gt;&lt;span style="color: green;"&gt;//on the connectionstring&lt;/span&gt;&lt;span style="color: black;"&gt;
   cmd2.ExecuteNonQuery();
}
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
By default MARS is not enabled. So reusing the same connection as the example above will throw an exception of type :&lt;br /&gt;
&lt;br /&gt;
InvalidOperationException, There is already an open DataReader associated with this Connection which must be closed first.&lt;br /&gt;
&lt;br /&gt;
Say hello to MARS : &lt;br /&gt;
&lt;br /&gt;
Enabling usage of MARS is as simple as setting MultipleActiveResultSets=Yes on your connectionString, eg: &lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;connectionStrings&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;clear&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
      &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;add&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"TyppsDB"&lt;/span&gt;&lt;span style="color: black;"&gt; 
         &lt;/span&gt;&lt;span style="color: red;"&gt;connectionString&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Data Source=typps-pc;Initial Catalog=TyppsDB;
                 Integrated Security=True;MultipleActiveResultSets=Yes"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
 &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;connectionStrings&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
you can now issue multiple commands on the same connectionstring which can result in a performance boost since opening and closing a connection can be expensive.&lt;br /&gt;
&lt;br /&gt;
When retrieving recordsets, the client has to eager load, meaning consume the resultsets immediately as oppossed to executing the command and not reading the data. Not doing so will cause the server-side buffer(Where sql server is hosted) to hold on to the data in memory and tie up resources, locks, threads etc something you want to avoid. &lt;br /&gt;
&lt;br /&gt;
Instead you want the data to stream to the client as fast as possible as the server returns the resultset without the server holding a large recordset in memory and tying up resources. This is not specific to MARS by the way, but when used correctly with eager loading meaning you consume the data as the command is executed &amp;nbsp;you can benefit from a significant performance boost as you have the ability to execute more commands and retrieve data seamlessly without the overhead of closing and reopening a connection to Sql Server.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;myReader = myCommand.&lt;span class="Apple-style-span" style="font-family: Consolas, Courier, monospace; font-size: 13px;"&gt;ExecuteReader&lt;/span&gt;(); &lt;/span&gt;&lt;span style="color: blue;"&gt;while&lt;/span&gt;&lt;span style="color: black;"&gt;(myReader.Read())
{
 &lt;/span&gt;&lt;span style="color: green;"&gt;//consume immediately now, don't wait.&lt;/span&gt;&lt;span style="color: black;"&gt;
}
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
Any command that is a SELECT, FETCH, READTEXT, RECEIVE, BULK INSERT (or bcp interface) or Asynchronous cursor population can take full advantage of MARS. &lt;br /&gt;
&lt;br /&gt;
What this means is that with MARS enabled, each of these commands listed above are defined in terms of interleaved executions, allowing them to be processed atomically within the same connection but with the ability to interleave, this means they can resume execution from the points where they were suspended if at all suspended. &lt;br /&gt;
&lt;br /&gt;
For instance, INSERT and UPDATE cannot take advantage of MARS. Now, consider a long running INSERT or UPDATE operation followed by a SELECT statement, all executing within the same connection. Such an operation will suspend the SELECT command until the UPDATE or INSERT has completed and then resume the SELECT operation. &lt;br /&gt;
&lt;br /&gt;
Consider reversing the above example where you had a SELECT followed by an UPDATE or INSERT operation within the same connection. Even in this case, if the UPDATE or INSERT command are issued while the SELECT is executing, then the SELECT command will interleave and as such become suspended until the INSERT or UPDATE complete and only after completion of the UPDATE or INSERT operation, will it resume execution. &lt;br /&gt;
&lt;br /&gt;
This is because the SELECT command can take advantage of MARS and has the ability to resume from the point where it was suspended making it an interleaved execution.&lt;br /&gt;
&lt;br /&gt;
Lastly, note that there are some intricacies when using transactions with MARS. You will need to workaround using recommendations provided eg: by using batch-scoped transactions.&lt;br /&gt;
&lt;br /&gt;
References :&lt;br /&gt;
&lt;a href="http://msdn.microsoft.com/en-us/library/ms345109(v=sql.90).aspx"&gt;http://msdn.microsoft.com/en-us/library/ms345109(v=sql.90).aspx&lt;/a&gt;&lt;br /&gt;
&lt;a href="http://msdn.microsoft.com/en-us/library/ms174377.aspx"&gt;http://msdn.microsoft.com/en-us/library/ms174377.aspx&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblog.typps.com%2f2011%2f04%2fmars-multiple-active-result-sets.html"&gt;&lt;img alt="kick it on DotNetKicks.com" border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblog.typps.com%2f2011%2f04%2fmars-multiple-active-result-sets.html" /&gt;&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/typps/axAH/~4/hhXDNQy1nsE" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.typps.com/feeds/1162616031239673728/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.typps.com/2011/04/mars-multiple-active-result-sets.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/1162616031239673728?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/1162616031239673728?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typps/axAH/~3/hhXDNQy1nsE/mars-multiple-active-result-sets.html" title="MARS - Multiple Active Result Sets support in SQL Server 2005 and above" /><author><name>Alessandro</name><uri>http://www.blogger.com/profile/08620937587114860179</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="24" src="http://4.bp.blogspot.com/-9iFBqNPDD_E/TbtHLjeMgZI/AAAAAAAAAK0/9WMh7OUK6KA/s220/alessandro.png" /></author><thr:total>0</thr:total><feedburner:origLink>http://blog.typps.com/2011/04/mars-multiple-active-result-sets.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0YAR30_cCp7ImA9WhZQEUg.&quot;"><id>tag:blogger.com,1999:blog-6778277710194170777.post-6775620359242367302</id><published>2011-04-18T11:25:00.000+02:00</published><updated>2011-04-18T21:32:26.348+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-04-18T21:32:26.348+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Tip" /><title>.NET exceptions, error handling for the exceptional case</title><content type="html">The tip of the day consists of, in making sure to use a try/catch block to trap individual exception types such as SqlException which is specific whereas Exception being more general. &lt;br /&gt;
&lt;br /&gt;
For instance, imagine you are connecting to a database, it's quite common to experience a connection failure that you might not expect. So it makes sense to try to handle any unexpected connection failures. &lt;br /&gt;
&lt;br /&gt;
First, try and discover the &lt;a href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.open(v=VS.71).asp"&gt;SqlConnection members&lt;/a&gt; class on msdn and drill down to the &lt;a href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.open(v=VS.71).aspx"&gt;SqlConnection.Open method&lt;/a&gt;, there's a section including the exceptions that the open method may throw and we find two : &lt;br /&gt;
&lt;br /&gt;
InvalidOperationException and SqlException with the specifics on what the conditions are when these exceptions are thrown.&lt;br /&gt;
&lt;br /&gt;
Similarly, we can write code likewise : &lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;try&lt;/span&gt;&lt;span style="color: black;"&gt;
{

 &lt;/span&gt;&lt;span style="color: green;"&gt;//connect to db and do something useful&lt;/span&gt;&lt;span style="color: black;"&gt;
}
&lt;/span&gt;&lt;span style="color: blue;"&gt;catch&lt;/span&gt;&lt;span style="color: black;"&gt;(InvalidOperationException invalidException)
{
 &lt;/span&gt;&lt;span style="color: green;"&gt;// specific exception&lt;/span&gt;&lt;span style="color: black;"&gt;
 &lt;/span&gt;&lt;span style="color: green;"&gt;// usually occurs when trying to open a conenction&lt;/span&gt;&lt;span style="color: black;"&gt;
}
&lt;/span&gt;&lt;span style="color: blue;"&gt;catch&lt;/span&gt;&lt;span style="color: black;"&gt; (SqlException sqlexception)
{
 &lt;/span&gt;&lt;span style="color: green;"&gt;// specific exception&lt;/span&gt;&lt;span style="color: black;"&gt;
 &lt;/span&gt;&lt;span style="color: green;"&gt;// will occur when opening a connection&lt;/span&gt;&lt;span style="color: black;"&gt;
}
&lt;/span&gt;&lt;span style="color: blue;"&gt;catch&lt;/span&gt;&lt;span style="color: black;"&gt; (Exception exception)
{
   &lt;/span&gt;&lt;span style="color: green;"&gt;// lastly the general exceptional case will kick in&lt;/span&gt;&lt;span style="color: black;"&gt;
   &lt;/span&gt;&lt;span style="color: green;"&gt;// could be anything that the first two exceptional cases didn't catch.&lt;/span&gt;&lt;span style="color: black;"&gt;
}
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
Note how Exception ( the mother of all exceptions) is handled last and instead we branch out by starting with more specific exceptions, InvalidOperationException and SqlException. This allows us to catch more specific exceptions and then move on to the more general exceptions. This is a &lt;a href="http://msdn.microsoft.com/en-us/library/seyhszts.aspx"&gt;good practice&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
Lastly, when logging your exceptions, use the default implementation of &lt;a href="http://msdn.microsoft.com/en-us/library/system.exception.tostring.aspx"&gt;ToString&lt;/a&gt; of the exception thrown to obtain the name of the class that threw the current exception, the message, the result of calling ToString on the inner exception, and the result of calling Environment.StackTrace with line number etc.&lt;br /&gt;
&lt;br /&gt;
Final notes: &lt;br /&gt;
If an exception can be handled programmatically without resorting to try/catch then investigate that route first. Handle the exceptions that are relevant to your code, throw back everything else. Read &lt;a href="http://msdn.microsoft.com/en-us/library/seyhszts.aspx"&gt;Best Practices for Handling Exceptions&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
References :&lt;br /&gt;
&lt;a href="http://msdn.microsoft.com/en-us/library/seyhszts.aspx"&gt;http://msdn.microsoft.com/en-us/library/seyhszts.aspx&lt;/a&gt;&lt;br /&gt;
&lt;a href="http://msdn.microsoft.com/en-us/library/system.exception.tostring.aspx"&gt;http://msdn.microsoft.com/en-us/library/system.exception.tostring.aspx&lt;/a&gt;&lt;br /&gt;
&lt;a href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection_methods(v=VS.71).aspx"&gt;http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection_methods(v=VS.71).aspx&lt;/a&gt;&lt;br /&gt;
&lt;a href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.open(v=VS.71).aspx"&gt;http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.open(v=VS.71).aspx&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblog.typps.com%2f2011%2f04%2fnet-exceptions-error-handling-for.html"&gt;&lt;img alt="kick it on DotNetKicks.com" border="0" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblog.typps.com%2f2011%2f04%2fnet-exceptions-error-handling-for.html" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;a href="http://dotnetshoutout.com/typps-NET-exceptions-error-handling-for-the-exceptional-case" rev="vote-for"&gt;&lt;img alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblog.typps.com%2F2011%2F04%2Fnet-exceptions-error-handling-for.html" style="border: 0px;" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/typps/axAH/~4/Jdrnl4KMXQg" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.typps.com/feeds/6775620359242367302/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.typps.com/2011/04/net-exceptions-error-handling-for.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/6775620359242367302?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/6775620359242367302?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typps/axAH/~3/Jdrnl4KMXQg/net-exceptions-error-handling-for.html" title=".NET exceptions, error handling for the exceptional case" /><author><name>Alessandro</name><uri>http://www.blogger.com/profile/08620937587114860179</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="24" src="http://4.bp.blogspot.com/-9iFBqNPDD_E/TbtHLjeMgZI/AAAAAAAAAK0/9WMh7OUK6KA/s220/alessandro.png" /></author><thr:total>0</thr:total><feedburner:origLink>http://blog.typps.com/2011/04/net-exceptions-error-handling-for.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkYDRHg5cCp7ImA9WhZQEEs.&quot;"><id>tag:blogger.com,1999:blog-6778277710194170777.post-692886189080891631</id><published>2011-04-15T10:43:00.000+02:00</published><updated>2011-04-17T21:22:55.628+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-04-17T21:22:55.628+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Rants" /><title>Silverlight or Html5 - from A .NET developer perspective.</title><content type="html">Html5 vs Silverlight for the .NET developer :&lt;br /&gt;
&lt;br /&gt;
If you are already invested in .NET, then you have been honing a particular set of skills for the past decade. This puts you at a position where it is difficult to favor Html5 simply because you lose some agility and sometimes this may make all the difference. &lt;br /&gt;
&lt;br /&gt;
Since MIX11, there is much debate with regard to Html5 vs silverlight. And developers continue to ask, what should I use. When I try to make a decision on the technology to use and power my applications the following facts helps me consolidate my ideas and beliefs further and this is also my current state of mind.&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;Silverlight is cross browser / platform&lt;/li&gt;
&lt;/ul&gt;&lt;br /&gt;
Note: This is great news if we are comparing silverlight to wpf but when compared to html5, we can agree on one thing. Html5 will be available on many more devices so it's not actually a pro for silverlight however, it's good to note that silverlight runs on those devices where it counts :&lt;br /&gt;
&lt;br /&gt;
All major browsers on both the mac and windows are supported. *nix support through Novell mono moonlight, which currently lags a bit behind but don't let this be a show stopper for you. You can easily develop a secondary slim version that works for &lt;a href="http://www.mono-project.com/Release_Notes_Moonlight4_Preview"&gt;moonlight&lt;/a&gt; which currently fully supports version 2.0 of silverlight and one that supports 3.0 is almost complete.&lt;br /&gt;
&lt;br /&gt;
Depending on the features your using, you might even be able to provide support for your application in it's full glory or perhaps with a minor workarounds if this audience is very important to you. Finally you also get to target the mobile platform wp7(Currently html5 support is lagging on wp7).&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;Strongly typed languages support (c#, vb.net). Unlike losely typed languages, you will benefit greatly from compile time error checking, and many language specific features.&lt;/li&gt;
&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;Easy deployment --your compiled silverlight assembly includes the compiled code and xaml files for all your pages in your application. These files are embedded  as resources and are compressed. While this is the default, it's also very flexible, meaning you can split your code in modules that can be dynamically downloaded by your silverlight application upon request. Tip: &lt;a href="http://compositewpf.codeplex.com/releases/view/55576"&gt;Prism&lt;/a&gt; is a great library if you want to benefit from modularity among other things in your silverlight apps.&lt;/li&gt;
&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;First class IDE support. Not only are you benefiting from superb IDE support for both designing your applications in expression blend while doing the development  in visual studio, but your way of working changes very little if you are already a developer in the microsoft space. Note that we will soon see some pretty good IDE support for Html5 as well, so this particular con for html5 won't hold true for very long.&lt;/li&gt;
&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;Reuse existing known code patterns. Patterns and practices have been gathered over the years and are a way for us to reuse "experience" in our development. Some are our own, some are industry known patterns and practices. On the other hand, Javascript too has been around for a very long time and they too have adopted their own set of patterns and practices, but clearly as a .NET developer, with silverlight, you don't have to relearn anything. I do heavy development in both c# and javascript, so this point is pretty moot for me, however, it's an important decision maker when chosing the right technology.&lt;/li&gt;
&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;Reusing existing code  --You already have existing code written for the .net framework, be it code that simply executes on the server or perhaps a WPF desktop application. Porting this code to run on the client instead in silverlight is much easier than rewriting it from scratch in javascript.&lt;/li&gt;
&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;As a web developer, you write client-side code for Silverlight in the same language that you use for server-side code (such as C# and VB), similarly you will be able to leverage the same objects you use on the server, so : generics, LINQ, Lambda, collections and many more even though only a subset of the .NET framework classes.&lt;/li&gt;
&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;IE8, 7 and 6 support is important to you because you still have a lot of clients on these browsers.  This is a given in silverlight, yet these browsers currently do not support the html5 featureset. Using another third party plugin such as google frame to target content to this user group ( also currently constituting the majority) is out of the question. Why should the largest portion of your userbase be served in downlevel?&lt;/li&gt;
&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;Install and upgrade --fully managed install and upgrade mechanism in place for Silverlight. This means as  new versions of silverlight are made available, we can start using them in our applications right away and based on the runtime version of silverlight your applications are built against, that version will be used or the user will prompted to download and install it. This is fully managed by Microsoft and you do nothing in this department other than some minimal configurations such as MinRuntimeVersion and so forth.&lt;/li&gt;
&lt;/ul&gt;Counter this with Html5, where features that constitute Html5 are supported at the discretion of the browser vendor. This means each browser vendor will pick up the feature they think is useful or important and will implement it. This makes it quite easy to find a feature that works in one browser but doesn't work in another or simply a feature that works in the latest versions of the browser while your left working around to compensate for the feature loss in older versions.&lt;br /&gt;
&lt;br /&gt;
In fact not too long ago &lt;a href="http://blog.whatwg.org/html-is-the-new-html5"&gt;whatwg&lt;/a&gt; has officially moved to a non-versioned model where Html5 is simply known as Html. This move by whatwg makes sense to me also because no browser currently supports the entire featureset of Html5 and therefore the name is misleading. Infact no browser ever supported html4 completely. Silverlight, being a plugin does not suffer from this lack of feature uniformity across browsers/devices.&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-size: large;"&gt;Final notes :&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
One thing is sure, Html5 has been a hot topic for a while now and with the release of ie9 it has gotten even hotter. This is a fresh area with room for plenty of innovation and new business opportunities to tap into. Like all new things, there's a gold rush. Everybody is pushing out web applications that uses shiny new Html5 features, because that is the current trend and there's lots of room for investment. This is a large market segment with a wider audience.&lt;br /&gt;
&lt;br /&gt;
As a .NET developer, if you are selling development tools, you are no longer confined to Microsoft developers, instead suddenly the web is your oyster. Lastly, it makes the headlines to invest in Html5 at this early stage, which can quickly translate to free marketing for you. That's someting to consider as well. &lt;br /&gt;
&lt;br /&gt;
So, in the end, which one is the better? Today I'm confident about my silverlight investment because it made sense for my application and it's needs. However on a different project, I may sing a different tune all together. Sadly, there is no better choice. You know your application, only you know what it needs. &lt;br /&gt;
&lt;br /&gt;
For instance I developed &lt;a href="http://www.abmho.com/"&gt;Abmho&lt;/a&gt; based on need. I couldn't find a proper syntax highlighter on the web that simply worked, that wasn't riddled with adverts and one that didn't require me to make a request to the server everytime. Soon this became irritating and so I decided to make one myself. &lt;br /&gt;
&lt;br /&gt;
It was important that my syntax highlighter executed in the browser as an application and was fast and responsive. I also wanted to complete development fast and I have a .NET background. I also already had a syntax highlighter library in c#. Rewriting this library in javascript would have simply taken forever. It was certainly not worth the effort.&lt;br /&gt;
&lt;br /&gt;
This move allows me to ship a wpf version (currently in the works) that requires subtle minimal changes to get working as a full fledged desktop application that I will be making available soon since Silverlight is a subset of Wpf. All this wouldn't be possible if I had gone the Html5 route.&lt;br /&gt;
&lt;br /&gt;
&lt;div&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblog.typps.com%2f2011%2f04%2fsilverlight-or-html5-from-net-developer.html"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblog.typps.com%2f2011%2f04%2fsilverlight-or-html5-from-net-developer.html" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;/div&gt;&lt;br /&gt;
&lt;div&gt;&lt;a rev="vote-for" href="http://dotnetshoutout.com/Silverlight-or-Html5-from-A-NET-developer-perspective"&gt;&lt;img alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fblog.typps.com%2F2011%2F04%2Fsilverlight-or-html5-from-net-developer.html" style="border:0px"/&gt;&lt;/a&gt;&lt;br /&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/typps/axAH/~4/aqBaIuwu12U" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.typps.com/feeds/692886189080891631/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.typps.com/2011/04/silverlight-or-html5-from-net-developer.html#comment-form" title="3 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/692886189080891631?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/692886189080891631?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typps/axAH/~3/aqBaIuwu12U/silverlight-or-html5-from-net-developer.html" title="Silverlight or Html5 - from A .NET developer perspective." /><author><name>Alessandro</name><uri>http://www.blogger.com/profile/08620937587114860179</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="24" src="http://4.bp.blogspot.com/-9iFBqNPDD_E/TbtHLjeMgZI/AAAAAAAAAK0/9WMh7OUK6KA/s220/alessandro.png" /></author><thr:total>3</thr:total><feedburner:origLink>http://blog.typps.com/2011/04/silverlight-or-html5-from-net-developer.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0QCRnk5eip7ImA9WhZRGEg.&quot;"><id>tag:blogger.com,1999:blog-6778277710194170777.post-6941494317794722112</id><published>2011-04-15T10:16:00.000+02:00</published><updated>2011-04-15T10:16:07.722+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-04-15T10:16:07.722+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Tip" /><title>Setting up a ConnectionStrings in .NET</title><content type="html">ConnectionStringBuilder : &lt;br /&gt;
&lt;br /&gt;
Tip for today is the ConnectionStringBuilder class. This is quite an unknown class but can result very useful in building your connectionstring. Normally, you'd take a connection string and pass it directly to your connection objects such as SqlConnection or DbConnection classes directly.&lt;br /&gt;
&lt;br /&gt;
Quick example from msdn : &lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;static&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; CreateCommand(&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt; queryString,
    &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt; connectionString)
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt;&lt;span style="color: black;"&gt; (SqlConnection connection = &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; SqlConnection(
               connectionString))
    {
        SqlCommand command = &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; SqlCommand(queryString, connection);
        command.Connection.Open();
        command.ExecuteNonQuery();
    }
}
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;br style="clear:both" /&gt;&lt;br /&gt;
As you can note from this classical example above, the connectionstring is passed directly to the SqlConnection object's constructor. While this is fine, any error in the connection and it's quite easy to make a mistake, won't be known until the connection is opened. &lt;br /&gt;
&lt;br /&gt;
Further more, if you want to pass some additional values, or modify existing values in the connectionstring it can be time consuming and again error prone and more effort than you want to invest.&lt;br /&gt;
&lt;br /&gt;
Say hello to the ConnectionStringBuilder. What this class does for you is that it facilitates parsing a connection string and also provides named properties you can simply set in code or whose values you want retrieved. A classic code example from msdn itself :&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt;&lt;span style="color: black;"&gt; System.Data;
&lt;/span&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt;&lt;span style="color: black;"&gt; System.Data.SqlClient;

&lt;/span&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&lt;span style="color: black;"&gt; Program
{
    &lt;/span&gt;&lt;span style="color: blue;"&gt;static&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; Main()
    {
        &lt;/span&gt;&lt;span style="color: green;"&gt;// Create a new SqlConnectionStringBuilder and&lt;/span&gt;&lt;span style="color: black;"&gt;
        &lt;/span&gt;&lt;span style="color: green;"&gt;// initialize it with a few name/value pairs.&lt;/span&gt;&lt;span style="color: black;"&gt;
        SqlConnectionStringBuilder builder =
            &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; SqlConnectionStringBuilder(GetConnectionString());

        &lt;/span&gt;&lt;span style="color: green;"&gt;// The input connection string used the &lt;/span&gt;&lt;span style="color: black;"&gt;
        &lt;/span&gt;&lt;span style="color: green;"&gt;// Server key, but the new connection string uses&lt;/span&gt;&lt;span style="color: black;"&gt;
        &lt;/span&gt;&lt;span style="color: green;"&gt;// the well-known Data Source key instead.&lt;/span&gt;&lt;span style="color: black;"&gt;
        Console.WriteLine(builder.ConnectionString);

        &lt;/span&gt;&lt;span style="color: green;"&gt;// Pass the SqlConnectionStringBuilder an existing &lt;/span&gt;&lt;span style="color: black;"&gt;
        &lt;/span&gt;&lt;span style="color: green;"&gt;// connection string, and you can retrieve and&lt;/span&gt;&lt;span style="color: black;"&gt;
        &lt;/span&gt;&lt;span style="color: green;"&gt;// modify any of the elements.&lt;/span&gt;&lt;span style="color: black;"&gt;
        builder.ConnectionString = "&lt;/span&gt;&lt;span style="color: darkred;"&gt;server=(local);user id=ab;&lt;/span&gt;&lt;span style="color: black;"&gt;" +
            "&lt;/span&gt;&lt;span style="color: darkred;"&gt;password= a!Pass113;initial catalog=AdventureWorks&lt;/span&gt;&lt;span style="color: black;"&gt;";

        &lt;/span&gt;&lt;span style="color: green;"&gt;// Now that the connection string has been parsed,&lt;/span&gt;&lt;span style="color: black;"&gt;
        &lt;/span&gt;&lt;span style="color: green;"&gt;// you can work with individual items.&lt;/span&gt;&lt;span style="color: black;"&gt;
        Console.WriteLine(builder.Password);
        builder.Password = "&lt;/span&gt;&lt;span style="color: darkred;"&gt;new@1Password&lt;/span&gt;&lt;span style="color: black;"&gt;";
        builder.AsynchronousProcessing = &lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;;

        &lt;/span&gt;&lt;span style="color: green;"&gt;// You can refer to connection keys using strings, &lt;/span&gt;&lt;span style="color: black;"&gt;
        &lt;/span&gt;&lt;span style="color: green;"&gt;// as well. When you use this technique (the default&lt;/span&gt;&lt;span style="color: black;"&gt;
        &lt;/span&gt;&lt;span style="color: green;"&gt;// Item property in Visual Basic, or the indexer in C#),&lt;/span&gt;&lt;span style="color: black;"&gt;
        &lt;/span&gt;&lt;span style="color: green;"&gt;// you can specify any synonym for the connection string key&lt;/span&gt;&lt;span style="color: black;"&gt;
        &lt;/span&gt;&lt;span style="color: green;"&gt;// name.&lt;/span&gt;&lt;span style="color: black;"&gt;
        builder["&lt;/span&gt;&lt;span style="color: darkred;"&gt;Server&lt;/span&gt;&lt;span style="color: black;"&gt;"] = "&lt;/span&gt;&lt;span style="color: darkred;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;";
        builder["&lt;/span&gt;&lt;span style="color: darkred;"&gt;Connect Timeout&lt;/span&gt;&lt;span style="color: black;"&gt;"] = 1000;
        builder["&lt;/span&gt;&lt;span style="color: darkred;"&gt;Trusted_Connection&lt;/span&gt;&lt;span style="color: black;"&gt;"] = &lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;;
        Console.WriteLine(builder.ConnectionString);

        Console.WriteLine("&lt;/span&gt;&lt;span style="color: darkred;"&gt;Press Enter to finish.&lt;/span&gt;&lt;span style="color: black;"&gt;");
        Console.ReadLine();
    }

    &lt;/span&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;static&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt; GetConnectionString()
    {
        &lt;/span&gt;&lt;span style="color: green;"&gt;// To avoid storing the connection string in your code,&lt;/span&gt;&lt;span style="color: black;"&gt;
        &lt;/span&gt;&lt;span style="color: green;"&gt;// you can retrieve it from a configuration file. &lt;/span&gt;&lt;span style="color: black;"&gt;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&lt;span style="color: black;"&gt; "&lt;/span&gt;&lt;span style="color: darkred;"&gt;Server=(local);Integrated Security=SSPI;&lt;/span&gt;&lt;span style="color: black;"&gt;" +
            "&lt;/span&gt;&lt;span style="color: darkred;"&gt;Initial Catalog=AdventureWorks&lt;/span&gt;&lt;span style="color: black;"&gt;";
    }
}

&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;br style="clear:both"/&gt;&lt;br /&gt;
Note: the same applies for the generic non SqlClient class DbConnection and you'd use the apposite DbConnectionStringBuilder class.&lt;br /&gt;
&lt;br /&gt;
Lastly, retrieving the connectionString itself can be facilitated by&amp;nbsp;using Server explorer, after having made the connection to the database visually in VS.NET you need to select your database in solution explorer and right click - Properties. From the property grid you can see the connectionstring VS.NET is using. That is the connectionstring you want.&lt;br /&gt;
&lt;br /&gt;
Alternatively you can use create a DataLink and copy the autogenerated connectionstring created by the DataLink. Follow the url in the references section below.&lt;br /&gt;
&lt;br /&gt;
Reference material :&lt;br /&gt;
&lt;a href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder.aspx"&gt;http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder.aspx&lt;/a&gt;&lt;br /&gt;
&lt;a href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.aspx"&gt;http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.aspx&lt;/a&gt;&lt;br /&gt;
&lt;a href="http://msdn.microsoft.com/en-us/library/ms718102(v=vs.85).aspx"&gt;http://msdn.microsoft.com/en-us/library/ms718102(v=vs.85).aspx&lt;/a&gt;&amp;nbsp;( DataLink )&lt;br /&gt;
&lt;a href="http://msdn.microsoft.com/en-us/library/33wwc2yw(v=VS.80).aspx"&gt;http://msdn.microsoft.com/en-us/library/33wwc2yw(v=VS.80).aspx&lt;/a&gt;&amp;nbsp;(Server Explorer)&lt;img src="http://feeds.feedburner.com/~r/typps/axAH/~4/TvDuaVDyHCQ" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.typps.com/feeds/6941494317794722112/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.typps.com/2011/04/setting-up-connectionstrings-in-net.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/6941494317794722112?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/6941494317794722112?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typps/axAH/~3/TvDuaVDyHCQ/setting-up-connectionstrings-in-net.html" title="Setting up a ConnectionStrings in .NET" /><author><name>Alessandro</name><uri>http://www.blogger.com/profile/08620937587114860179</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="24" src="http://4.bp.blogspot.com/-9iFBqNPDD_E/TbtHLjeMgZI/AAAAAAAAAK0/9WMh7OUK6KA/s220/alessandro.png" /></author><thr:total>0</thr:total><feedburner:origLink>http://blog.typps.com/2011/04/setting-up-connectionstrings-in-net.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D08EQX4-cCp7ImA9WhZRGEg.&quot;"><id>tag:blogger.com,1999:blog-6778277710194170777.post-2465776627487553547</id><published>2011-04-10T18:00:00.000+02:00</published><updated>2011-04-15T10:23:20.058+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-04-15T10:23:20.058+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Announcement" /><title>Typps 3.1 released</title><content type="html">A new 3.1 stable release is available for download.&lt;img src="http://feeds.feedburner.com/~r/typps/axAH/~4/XbGG7iBF0Q8" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.typps.com/feeds/2465776627487553547/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.typps.com/2011/04/typps-31-released.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/2465776627487553547?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/2465776627487553547?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typps/axAH/~3/XbGG7iBF0Q8/typps-31-released.html" title="Typps 3.1 released" /><author><name>Alessandro</name><uri>http://www.blogger.com/profile/08620937587114860179</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="24" src="http://4.bp.blogspot.com/-9iFBqNPDD_E/TbtHLjeMgZI/AAAAAAAAAK0/9WMh7OUK6KA/s220/alessandro.png" /></author><thr:total>0</thr:total><feedburner:origLink>http://blog.typps.com/2011/04/typps-31-released.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0EHRn87eCp7ImA9WhZRGEg.&quot;"><id>tag:blogger.com,1999:blog-6778277710194170777.post-6874490623802569515</id><published>2011-03-29T08:59:00.000+02:00</published><updated>2011-04-15T10:20:37.100+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-04-15T10:20:37.100+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Announcement" /><title>Typps 3.0 released</title><content type="html">&lt;div&gt;We haven't been blogging much. We'll try to improve in that too. In the mean time know that&amp;nbsp;&lt;/div&gt;3.0 is now released. This version fixes many problems among which issues in IE9. &lt;br /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/typps/axAH/~4/AMeMl2fcHFI" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.typps.com/feeds/6874490623802569515/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.typps.com/2011/03/typps-30-released.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/6874490623802569515?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/6874490623802569515?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typps/axAH/~3/AMeMl2fcHFI/typps-30-released.html" title="Typps 3.0 released" /><author><name>Alessandro</name><uri>http://www.blogger.com/profile/08620937587114860179</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="24" src="http://4.bp.blogspot.com/-9iFBqNPDD_E/TbtHLjeMgZI/AAAAAAAAAK0/9WMh7OUK6KA/s220/alessandro.png" /></author><thr:total>0</thr:total><feedburner:origLink>http://blog.typps.com/2011/03/typps-30-released.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0INSHs_fCp7ImA9WhZRGEg.&quot;"><id>tag:blogger.com,1999:blog-6778277710194170777.post-7373462502837282895</id><published>2010-11-12T22:47:00.000+01:00</published><updated>2011-04-15T10:19:59.544+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-04-15T10:19:59.544+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Announcement" /><title>Typps Version 2.7 released</title><content type="html">We have a new release that fixes an important bug in when running the editor in a website that targets .NET 4.0 and Ajax control Toolkit. This release does not break any features so it should be a fairly easy upgrade.&lt;br /&gt;
&lt;br /&gt;
Currently we are restructuring code in preparation for new features. So unless there is a major bug to fix, we will be skipping a few iterations.&lt;br /&gt;
&lt;br /&gt;
One of the things we don't currently do is check in our code on codeplex. Instead we make releases and put out packages in the download section. Hopefully from the next release onwards we will start doing that too.&lt;img src="http://feeds.feedburner.com/~r/typps/axAH/~4/lHbgTX1kWsw" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.typps.com/feeds/7373462502837282895/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.typps.com/2010/11/typps-version-27-released.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/7373462502837282895?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/7373462502837282895?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typps/axAH/~3/lHbgTX1kWsw/typps-version-27-released.html" title="Typps Version 2.7 released" /><author><name>Alessandro</name><uri>http://www.blogger.com/profile/08620937587114860179</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="24" src="http://4.bp.blogspot.com/-9iFBqNPDD_E/TbtHLjeMgZI/AAAAAAAAAK0/9WMh7OUK6KA/s220/alessandro.png" /></author><thr:total>0</thr:total><feedburner:origLink>http://blog.typps.com/2010/11/typps-version-27-released.html</feedburner:origLink></entry><entry gd:etag="W/&quot;Ak4MQXs9eyp7ImA9WhZQEk0.&quot;"><id>tag:blogger.com,1999:blog-6778277710194170777.post-1432045573847976403</id><published>2010-11-11T02:19:00.000+01:00</published><updated>2011-04-19T12:29:40.563+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-04-19T12:29:40.563+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Rants" /><title>Silverlight is all that and a bag of chips</title><content type="html">Recently there's been a lot of FUD around silverlight and how microsoft is making a shift in strategy, which by the way is a good thing. &lt;a href="http://weblogs.asp.net/scottgu/archive/2010/11/04/silverlight-questions.aspx"&gt;Silverlight questions&lt;/a&gt;&amp;nbsp;for more.&lt;br /&gt;
&lt;br /&gt;
While I like standards and HTML5, I want it all today. I'm currently invested in silverlight, I love the DEV tools, great community that Microsoft managed to build around it during the past 3 years and currently it enjoys a good 60% penetration rate.&lt;br /&gt;
&lt;br /&gt;
60% is kind of OK for me, afterall, it's a free plugin with a small footprint. It costs nothing to the end user to install. If they want to use my application, they will eventually get it. Best yet, I don't have to worry about ie6,ie7 and ie8 not supporting it.&lt;br /&gt;
&lt;br /&gt;
People right now are still confused on which way to swing. Silverlight vs Html5 because IE9 is releasing soon with support for HTML5.&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;My reasons to continue to stick to silverlight is simple.&lt;/span&gt;&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;I am porting existing c# code I used to run serverside, to now run on the client.&lt;/li&gt;
&lt;li&gt;IE6,IE7,IE8 support. Even with IE9 out there, we still have to worry about these currently dominating browsers. I simply refuse to use yet another plugin ( google frame ) to provide HTML5 support.&lt;/li&gt;
&lt;li&gt;Silverlight has proper development tools, enviornment and a thriving community. I cannot say the same about HTML5 and SVG.&lt;/li&gt;
&lt;/ol&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;What are others worried about ?&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;div style="text-align: center;"&gt;&lt;span class="Apple-style-span"&gt;&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;Silverlight&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/_tn86ovsmIy4/TNtBYjDKp4I/AAAAAAAAAGc/V5fUiJeK7pQ/s1600/silverlight-is.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="240" src="http://4.bp.blogspot.com/_tn86ovsmIy4/TNtBYjDKp4I/AAAAAAAAAGc/V5fUiJeK7pQ/s320/silverlight-is.jpg" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;div style="text-align: center;"&gt;&lt;span class="Apple-style-span"&gt;&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;Flash&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/_tn86ovsmIy4/TNtBWz8VjMI/AAAAAAAAAGU/calGL2FF5P8/s1600/flash-is.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="239" src="http://4.bp.blogspot.com/_tn86ovsmIy4/TNtBWz8VjMI/AAAAAAAAAGU/calGL2FF5P8/s320/flash-is.jpg" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;div style="text-align: center;"&gt;&lt;span class="Apple-style-span"&gt;&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;HTML5&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/_tn86ovsmIy4/TNtBX-piBuI/AAAAAAAAAGY/rhHxYMyfNZ4/s1600/html5-is.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="233" src="http://1.bp.blogspot.com/_tn86ovsmIy4/TNtBX-piBuI/AAAAAAAAAGY/rhHxYMyfNZ4/s320/html5-is.jpg" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
whoops, I might be in a little trouble &amp;lt;:o) Just kidding. I understand the fear. But nothing is going to change, reality is what it is. Silverlight is all that and a bag of chips. I shall continue developing my application in silverlight. I will update this post when the release is up.&lt;br /&gt;
&lt;br /&gt;
Update 4/19/2011 : The release is up and has been up for almost a month. I just forgot to update this post. &lt;br /&gt;
Your all welcome to check it out.  &lt;a href="http://www.abmho.com"&gt;abmho.com&lt;/a&gt; &lt;br /&gt;
&lt;br /&gt;
It's still in beta.&lt;img src="http://feeds.feedburner.com/~r/typps/axAH/~4/ANCfcJ2kph0" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.typps.com/feeds/1432045573847976403/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.typps.com/2010/11/silverlight-is-all-that-and-bag-of.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/1432045573847976403?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/1432045573847976403?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typps/axAH/~3/ANCfcJ2kph0/silverlight-is-all-that-and-bag-of.html" title="Silverlight is all that and a bag of chips" /><author><name>Alessandro</name><uri>http://www.blogger.com/profile/08620937587114860179</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="24" src="http://4.bp.blogspot.com/-9iFBqNPDD_E/TbtHLjeMgZI/AAAAAAAAAK0/9WMh7OUK6KA/s220/alessandro.png" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/_tn86ovsmIy4/TNtBYjDKp4I/AAAAAAAAAGc/V5fUiJeK7pQ/s72-c/silverlight-is.jpg" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://blog.typps.com/2010/11/silverlight-is-all-that-and-bag-of.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0IDRH08cCp7ImA9WhZRGEg.&quot;"><id>tag:blogger.com,1999:blog-6778277710194170777.post-3284614749541861732</id><published>2010-08-21T18:36:00.000+02:00</published><updated>2011-04-15T10:19:35.378+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-04-15T10:19:35.378+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Announcement" /><title>Typps Version 2.2 released</title><content type="html">After 8 months of heavy development, Typps is back with a vengence. This new version marks a milestone because the code base has gone a partial rewrite and includes numerous bug fixes and new features that we shall be documenting soon.&lt;br /&gt;
&lt;br /&gt;
Infact we shall soon provide a backlog so people know what feature is being worked on and where we are headed. In this release we have stalled many times but this is due to the numerous problems we were trying to tackle in Typps. To make it worse, we under went a complete rebranding phase and we are now officially called Typps. Along with the new name we have revamped our site, moved our discussions to codeplex ( much more consistent and less maintenance work for us). &lt;br /&gt;
&lt;br /&gt;
We are however still lacking in documentation and demo pages to showcase on &lt;a href="http://demo.typps.com/"&gt;http://demo.typps.com/&lt;/a&gt; ; the demo pages are also downloadable on codeplex. This allows you quick access to seeing how everything works. There are a lot of plans for typps and where we are headed, eg : more documentation, video tutorials, a faster release sprint (while adopting agile methodologies), supply more unit tests ( our current unit tests are quite scarce and broken). Moving forward, we also want to provide MVC support and silverlight. &lt;br /&gt;
&lt;br /&gt;
MVC and silverlight are important, in this manner we can reuse the same components across various project types without resorting to alternatives etc.&lt;br /&gt;
&lt;br /&gt;
This new release does not offer an upgrade, so don't expect to upgrade your existing apps by simply copying the dll. There are a lot of breaking features to take this to the next level. If you encounter issues, report them on codeplex.&lt;img src="http://feeds.feedburner.com/~r/typps/axAH/~4/ORbp5DBZ5AA" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.typps.com/feeds/3284614749541861732/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.typps.com/2010/08/typps-version-22-released.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/3284614749541861732?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/3284614749541861732?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typps/axAH/~3/ORbp5DBZ5AA/typps-version-22-released.html" title="Typps Version 2.2 released" /><author><name>Alessandro</name><uri>http://www.blogger.com/profile/08620937587114860179</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="24" src="http://4.bp.blogspot.com/-9iFBqNPDD_E/TbtHLjeMgZI/AAAAAAAAAK0/9WMh7OUK6KA/s220/alessandro.png" /></author><thr:total>0</thr:total><feedburner:origLink>http://blog.typps.com/2010/08/typps-version-22-released.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0IBRHg-fip7ImA9WhZRGEg.&quot;"><id>tag:blogger.com,1999:blog-6778277710194170777.post-970402087486310910</id><published>2010-08-01T05:32:00.000+02:00</published><updated>2011-04-15T10:19:15.656+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-04-15T10:19:15.656+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Announcement" /><title>We have rebranded to Typps (Formerly jiffycms)</title><content type="html">Lots to do and so little time. With a little bit of patience it will all be done and we will put up the latest release on codeplex.&lt;br /&gt;
This is&amp;nbsp;the&amp;nbsp;very first post on our new blog! Finally. In the my next post entry&amp;nbsp;I'll write a little on why we rebranded and the reason to move into the cloud. Why google apps and blogger and what happened to our cms? ಠ_ಠ&lt;br /&gt;
But now it's time to put up some documentation and&amp;nbsp;wrap up this release.&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote&gt;Note that previously we maintained our own forums too. We are now using the forum on codeplex, so use that to report any issues or ask for help&amp;nbsp;&lt;a href="http://typps.codeplex.com/Thread/List.aspx"&gt;http://typps.codeplex.com/Thread/List.aspx&lt;/a&gt;&lt;/blockquote&gt;&lt;img src="http://feeds.feedburner.com/~r/typps/axAH/~4/Vk0fydfcD4Y" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.typps.com/feeds/970402087486310910/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.typps.com/2010/07/we-have-rebranded-to-typps-formerly.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/970402087486310910?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/970402087486310910?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typps/axAH/~3/Vk0fydfcD4Y/we-have-rebranded-to-typps-formerly.html" title="We have rebranded to Typps (Formerly jiffycms)" /><author><name>Alessandro</name><uri>http://www.blogger.com/profile/08620937587114860179</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="24" src="http://4.bp.blogspot.com/-9iFBqNPDD_E/TbtHLjeMgZI/AAAAAAAAAK0/9WMh7OUK6KA/s220/alessandro.png" /></author><thr:total>0</thr:total><feedburner:origLink>http://blog.typps.com/2010/07/we-have-rebranded-to-typps-formerly.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkEBSHYzcSp7ImA9WhZXEUo.&quot;"><id>tag:blogger.com,1999:blog-6778277710194170777.post-8256997074223109947</id><published>2008-09-23T01:22:00.003+02:00</published><updated>2011-04-30T15:37:39.889+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-04-30T15:37:39.889+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET Webforms" /><title>Conserving Resources When Writing BLOB Values to SQL Server and Streaming BLOB Values back to the Client</title><content type="html">&amp;nbsp;Reading and writing blob data to/from your database can be a resource hog, because it normally involves holding the entire stream in memory. Holding 10kb, 20, 100kb in memory might not be an issue, however as you start allowing larger file uploads, your application starts to feel the stress and easily starts to run out of resources. &lt;br /&gt;
&lt;br /&gt;
Particularly, say you had a relatively small file (2mb) your allowing users to upload from your pages. User A uploads, it's only 2mb, you have not exceeded your memory allocation yet. However, as more users try to upload simultaneously, now your allocating more memory slots at a time, eg. if they were 5 simultaneous users, that's 2*5 = 10mb, 100 users, then it's 2*100. &lt;br /&gt;
&lt;br /&gt;
This is all too stressful on your webserver, or atleast was until ASP.NET 2.0. Since, things have changed and now asp.net supports setting a diskThreshold that allows you to specify the amount of data buffered in server memory for the request. So, say you wanted to keep the default diskThreshold of 256 bytes, when the file is larger than 256bytes, asp.net begins to write the filestream to disk Versus keeping it in memory. This is just great because i remember back in the day, this was quite annoying. Glad it's fixed. This means we do nothing as far as the upload is concerned from the client to your webserver.&lt;br /&gt;
&lt;br /&gt;
Still, if we are storing the stream in SQL Server, now we have a whole new scenario, and the question remains, how do we stream this data to sql server while conserving memory ? Should we stream the entire data as is in one lump ? Wont that allocate memory on our database server before writing to the database field ? This is what i want to address. Basically, what we want to do is save our file stream to sqlserver in smaller chunks, that way our database server will allocate only enough memory for that little chunk before writing it to the database field, in this way conserving memory at the cost of write speed(no free lunch, i'm afraid). &lt;br /&gt;
&lt;br /&gt;
However, the price to pay in latency for streaming chunks of data to sql server might be well worth, because the alternative is to tax your database server when memory is available and to crash it (I assume it may crash when it runs out of memory. I have not tried personally. You may try that yourself if your feeling curious. Do not forget to share your findings if you do :P)&lt;br /&gt;
&lt;br /&gt;
Take, SQL SERVER Express edition as an example which has the imposed limit of 1gb memory. That's not a whole lot is it ? Streaming the data to sql server in chunks is actually not as taxing as it may seem. True, the write performance is degraded but you can regulate it by increasing/decreasing the size of your chunk to find a compromise.&lt;br /&gt;
&lt;br /&gt;
So, how do we do this ? SQL Server 2005 has a new feature that enables it to update a field with .WRITE ; as you stream your chunks it just appends it to the existing field. You can read about it here : &lt;a href="http://msdn.microsoft.com/en-us/library/ms177523.aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms177523.aspx"&gt;http://msdn.microsoft.com/en-us/library/ms177523.aspx&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
To Quote the documentation from the above link : &lt;br /&gt;
Because the .WRITE clause cannot be used to modify a NULL column, the column is first populated with temporary data. This data is then replaced with the correct data by using the .WRITE clause&lt;br /&gt;
&lt;br /&gt;
Now, that's not cool! We can work around that nicely though. Ineffect, it does not even feel like were working around anything. I've basically followed microsofts own documentation located at : &lt;a href="http://msdn.microsoft.com/en-us/library/aa719771.aspx" mce_href="http://msdn.microsoft.com/en-us/library/aa719771.aspx"&gt;http://msdn.microsoft.com/en-us/library/aa719771.aspx&lt;/a&gt;, which had the proper code, however, instead of .Write, it uses an older method that is not compatible with&amp;nbsp; the newer varbinary,varchar(max),nvarchar(max) datatypes supported by SQL Server 2005&amp;nbsp; as opposed to the older not recommeded types -&amp;gt; image, text, and ntext. &lt;br /&gt;
&lt;br /&gt;
Following is an extract of the code i use to stream chunks of data to sql server using the .Write method. The purpose of the sample code is to display how you can go about writing your own ofcourse. I have just ripped the piece of code out of my DAL with a few minor adjustments, so just running a copy/paste of the code below will or may not yield much depending on your level of expertise. I am just way too lazy to write a completely contained example so bear with me. As it is, this article has gotten longer than I want it to be.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; InsertFile(&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt; applicationName, Stream data,
  &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt; mimeType, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt; fileName)
 {
  &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; fileId = -1;
  &lt;/span&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt;&lt;span style="color: black;"&gt; (SqlConnection myConnection = GetSqlConnection())
  {
   myConnection.Open();
   SqlCommand cmd = &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; SqlCommand(DbOwner +
    "&lt;/span&gt;&lt;span style="color: darkred;"&gt;.DataFile_Insert&lt;/span&gt;&lt;span style="color: black;"&gt;", myConnection);
   cmd.CommandType = CommandType.StoredProcedure;
   cmd.Parameters.Add("&lt;/span&gt;&lt;span style="color: darkred;"&gt;@applicationName&lt;/span&gt;&lt;span style="color: black;"&gt;",
    SqlDbType.NVarChar, 256).Value = applicationName;
   cmd.Parameters.Add("&lt;/span&gt;&lt;span style="color: darkred;"&gt;@mimeType&lt;/span&gt;&lt;span style="color: black;"&gt;",
    SqlDbType.NVarChar, 50).Value = mimeType;
   cmd.Parameters.Add("&lt;/span&gt;&lt;span style="color: darkred;"&gt;@length&lt;/span&gt;&lt;span style="color: black;"&gt;",
    SqlDbType.Int, 4).Value = data.Length;
   cmd.Parameters.Add("&lt;/span&gt;&lt;span style="color: darkred;"&gt;@fileName&lt;/span&gt;&lt;span style="color: black;"&gt;",
    SqlDbType.NVarChar, 256).Value = fileName;
   SqlParameter fileIdParam =
    cmd.Parameters.Add("&lt;/span&gt;&lt;span style="color: darkred;"&gt;@fileId&lt;/span&gt;&lt;span style="color: black;"&gt;", SqlDbType.Int, 4);
   fileIdParam.Direction = ParameterDirection.Output;
   cmd.ExecuteNonQuery();
   &lt;/span&gt;&lt;span style="color: green;"&gt;// now insert in chunks&lt;/span&gt;&lt;span style="color: black;"&gt;
   fileId = (&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;)fileIdParam.Value;
  }
  &lt;/span&gt;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&lt;span style="color: black;"&gt; (fileId &amp;gt; -1)
  {
   &lt;/span&gt;&lt;span style="color: green;"&gt;//The size of the "chunks" &lt;/span&gt;&lt;span style="color: black;"&gt;
   &lt;/span&gt;&lt;span style="color: green;"&gt;// 128 bytes, regulate at will&lt;/span&gt;&lt;span style="color: black;"&gt;
   InsertFileByChunks(fileId, data, 128);
  }
 }
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
&amp;nbsp;note how we are calling InsertFileByChunks from our insert method above ? &lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; InsertFileByChunks(&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; fileId, Stream data, &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; bufferLen)
{
&lt;/span&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;   &lt;/span&gt;using&lt;/span&gt;&lt;span style="color: black;"&gt; (SqlConnection myConnection = GetSqlConnection())
   {
  &amp;nbsp;&lt;/span&gt;   myConnection.Open();&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;   &lt;span style="color: black;"&gt;SqlCommand cmd = &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; SqlCommand(DbOwner +&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;  &amp;nbsp;   &lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: darkred;"&gt;.DataFile_InsertChunk&lt;/span&gt;&lt;span style="color: black;"&gt;", myConnection);&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;   cmd.CommandType = CommandType.StoredProcedure;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;   &lt;span style="color: black;"&gt;cmd.Parameters.Add("&lt;/span&gt;&lt;span style="color: darkred;"&gt;@fileId&lt;/span&gt;&lt;span style="color: black;"&gt;",&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;   SqlDbType.Int, 4).Value = fileId;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;   &lt;span style="color: black;"&gt;SqlParameter paramData = cmd.Parameters.Add("&lt;/span&gt;&lt;span style="color: darkred;"&gt;@data&lt;/span&gt;&lt;span style="color: black;"&gt;",&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;   SqlDbType.VarBinary, 128);&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;   &lt;span style="color: black;"&gt;SqlParameter paramOffset = cmd.Parameters.Add("&lt;/span&gt;&lt;span style="color: darkred;"&gt;@offset&lt;/span&gt;&lt;span style="color: black;"&gt;",&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;  &amp;nbsp;  &amp;nbsp;   SqlDbType.BigInt);&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;   &lt;span style="color: black;"&gt;cmd.Parameters.Add("&lt;/span&gt;&lt;span style="color: darkred;"&gt;@length&lt;/span&gt;&lt;span style="color: black;"&gt;",&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;  &amp;nbsp;   SqlDbType.Int, 4).Value = bufferLen;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;&lt;/span&gt;   &lt;span style="color: blue;"&gt;using&lt;/span&gt;&lt;span style="color: black;"&gt; (BinaryReader br = &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; BinaryReader(data))&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&amp;nbsp;   {&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;&lt;/span&gt;  &amp;nbsp;   &lt;span style="color: blue;"&gt;byte&lt;/span&gt;&lt;span style="color: black;"&gt;[] buffer = br.ReadBytes(bufferLen);&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;&lt;/span&gt;  &amp;nbsp;   &lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; offset = 0;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;&lt;/span&gt;  &amp;nbsp;   &lt;span style="color: blue;"&gt;while&lt;/span&gt;&lt;span style="color: black;"&gt; (buffer.Length &amp;gt; 0)&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&amp;nbsp;  &amp;nbsp;   {&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&amp;nbsp;  &amp;nbsp;  &amp;nbsp;   paramData.Value = buffer;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&amp;nbsp;  &amp;nbsp;  &amp;nbsp;   paramOffset.Value = offset;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&amp;nbsp;  &amp;nbsp;  &amp;nbsp;   cmd.ExecuteNonQuery();&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&amp;nbsp;  &amp;nbsp;  &amp;nbsp;   offset += bufferLen;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&amp;nbsp;  &amp;nbsp;  &amp;nbsp;   buffer = br.ReadBytes(bufferLen);&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&amp;nbsp;  &amp;nbsp;   }&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&amp;nbsp;   }&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&amp;nbsp;   data.Close();&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;}
}
&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
&amp;nbsp;As you can see the above method just writes chunks of data at a time(the accompanying stored procedure DataFile_InsertChunk uses .Write to update the field with the new chunks as they come in).&lt;br /&gt;
&lt;br /&gt;
And now the accompanying stored procedures DataFile_Insert and DataFile_InsertChunk : &lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;CREATE&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;PROCEDURE&lt;/span&gt;&lt;span style="color: black;"&gt; [dbo].[DataFile_Insert]
@applicationName &lt;/span&gt;&lt;span style="color: blue;"&gt;nvarchar&lt;/span&gt;&lt;span style="color: black;"&gt;(256),
@mimeType &lt;/span&gt;&lt;span style="color: blue;"&gt;nvarchar&lt;/span&gt;&lt;span style="color: black;"&gt;(50), 
@length &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, 
@fileName &lt;/span&gt;&lt;span style="color: blue;"&gt;nvarchar&lt;/span&gt;&lt;span style="color: black;"&gt;(256),
@fileId &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;output&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;AS&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;BEGIN&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;DECLARE&lt;/span&gt;&lt;span style="color: black;"&gt; @applicationId &lt;/span&gt;&lt;span style="color: blue;"&gt;UNIQUEIDENTIFIER&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;SELECT&lt;/span&gt;&lt;span style="color: black;"&gt;  @ApplicationId = ApplicationId &lt;/span&gt;&lt;span style="color: blue;"&gt;FROM&lt;/span&gt;&lt;span style="color: black;"&gt; dbo.aspnet_Applications 
&lt;/span&gt;&lt;span style="color: blue;"&gt;WHERE&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;LOWER&lt;/span&gt;&lt;span style="color: black;"&gt;(@ApplicationName) = LoweredApplicationName

&lt;/span&gt;&lt;span style="color: blue;"&gt;INSERT&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;INTO&lt;/span&gt;&lt;span style="color: black;"&gt; dataFile (applicationId, data, mimeType, [length],  
[fileName], dateCreated, lastDateModified)
&lt;/span&gt;&lt;span style="color: blue;"&gt;VALUES&lt;/span&gt;&lt;span style="color: black;"&gt; (@applicationId, 0x0, @mimeType, @length, @fileName,  
GETDATE(), GETDATE())
&lt;/span&gt;&lt;span style="color: blue;"&gt;SET&lt;/span&gt;&lt;span style="color: black;"&gt; @fileId = SCOPE_IDENTITY()    
&lt;/span&gt;&lt;span style="color: blue;"&gt;END&lt;/span&gt;&lt;span style="color: black;"&gt; 
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
&amp;nbsp;Note above how we introduce the value 0x0 (null) as per the article on MSDN into the data field ? "data" is the name of the field that will be holding our data stream and is a varbinary type. The reason we introduce the value 0x0 is because .Write cannot write into a NULL field.&amp;nbsp; This is the workaround i was talking about earlier and it does not effect the appended chunks in anyway.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;CREATE&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;PROCEDURE&lt;/span&gt;&lt;span style="color: black;"&gt; [dbo].[DataFile_InsertChunk]
@data &lt;/span&gt;&lt;span style="color: blue;"&gt;varbinary&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;MAX&lt;/span&gt;&lt;span style="color: black;"&gt;), 
@length &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;, 
@fileId &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;output&lt;/span&gt;&lt;span style="color: black;"&gt;,
@offset &lt;/span&gt;&lt;span style="color: blue;"&gt;bigint&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;AS&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;BEGIN&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;UPDATE&lt;/span&gt;&lt;span style="color: black;"&gt; dataFile &lt;/span&gt;&lt;span style="color: blue;"&gt;SET&lt;/span&gt;&lt;span style="color: black;"&gt; data.&lt;/span&gt;&lt;span style="color: blue;"&gt;WRITE&lt;/span&gt;&lt;span style="color: black;"&gt;(@data, @offset, 
@length) &lt;/span&gt;&lt;span style="color: blue;"&gt;WHERE&lt;/span&gt;&lt;span style="color: black;"&gt; fileId = @fileId    
&lt;/span&gt;&lt;span style="color: blue;"&gt;END&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
&amp;nbsp;As you can see, it all boils down to this. Quite simple indeed. the .WRITE method takes as argument your chunk, the offset and the length. Not much for me to add here. Oh one thing you may also want to note is that .WRITE is called via the field to which it needs to write to. so the syntax takes the form of -&amp;gt; fieldName.WRITE(...) ; in my case the field name is "data" as you can note from the code in the stored proc above.&lt;br /&gt;
&lt;br /&gt;
We have now successfully stored our file stream in chunks. Next we want to see how to retrieve our filestream in a similar manner, that way we do not hog resources on our webserver when binary content is requested, requiring us to stream it out from the database server to the webserver where our code streams it out again to the client. Firstly, I hope you are already familiar with an HttpHandler, if not you can look it up on google. &lt;br /&gt;
&lt;br /&gt;
An HttpHandler/Module is general knowledge so i'm not going to get into it. I've mapped somefile.ashx to my HttpHandler, that way, every request that comes in will be handled by my custom handler, where i request the file stream from the database and then stream it out again in chunks to the client. My HttpHandler looks like this : &lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&lt;span style="color: black;"&gt; BinaryDataHandler : IHttpHandler
{
&lt;/span&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;   &lt;/span&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; ProcessRequest(HttpContext context)
   {
&lt;/span&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;&lt;/span&gt;   &lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; fileId;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;&lt;/span&gt;   &lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt; success = &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;.TryParse(&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&amp;nbsp;   context.Request.QueryString["&lt;span style="color: darkred;"&gt;fileId&lt;/span&gt;&lt;span style="color: black;"&gt;"],&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;&lt;/span&gt;   &lt;span style="color: blue;"&gt;out&lt;/span&gt;&lt;span style="color: black;"&gt; fileId);&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;&lt;/span&gt;   &lt;span style="color: blue;"&gt;if&lt;/span&gt;&lt;span style="color: black;"&gt; (success)&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&amp;nbsp;   {&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&amp;nbsp;  &amp;nbsp;   IDataReader r = FileManager.GetFileById(fileId);&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&amp;nbsp;  &amp;nbsp;   StreamData(r, context);&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&amp;nbsp;   }&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;}
&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;   &lt;/span&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; StreamData(IDataReader r, HttpContext context)
   {
&lt;/span&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;&lt;/span&gt;   &lt;span style="color: blue;"&gt;if&lt;/span&gt;&lt;span style="color: black;"&gt; (r.Read())&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&amp;nbsp;   {&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&amp;nbsp;  &amp;nbsp;   DataFileItem dfi = &lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; DataFileItem();&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;  &amp;nbsp;   &lt;span style="color: black;"&gt;dfi.Length = (r["&lt;/span&gt;&lt;span style="color: darkred;"&gt;length&lt;/span&gt;&lt;span style="color: black;"&gt;"] &lt;/span&gt;&lt;span style="color: blue;"&gt;is&lt;/span&gt;&lt;span style="color: black;"&gt; DBNull) ?&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;  &amp;nbsp;  &amp;nbsp;   &lt;span style="color: black;"&gt;-1 : (&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;)r["&lt;/span&gt;&lt;span style="color: darkred;"&gt;length&lt;/span&gt;&lt;span style="color: black;"&gt;"];&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;  &amp;nbsp;   &lt;span style="color: black;"&gt;dfi.MimeType = (r["&lt;/span&gt;&lt;span style="color: darkred;"&gt;mimeType&lt;/span&gt;&lt;span style="color: black;"&gt;"] &lt;/span&gt;&lt;span style="color: blue;"&gt;is&lt;/span&gt;&lt;span style="color: black;"&gt; DBNull) ?&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;&lt;/span&gt;  &amp;nbsp;   &lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;.Empty : (&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;)r["&lt;/span&gt;&lt;span style="color: darkred;"&gt;mimeType&lt;/span&gt;&lt;span style="color: black;"&gt;"];&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;&lt;/span&gt;  &amp;nbsp;   &lt;span style="color: blue;"&gt;if&lt;/span&gt;&lt;span style="color: black;"&gt; (dfi.MimeType != &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;.Empty)&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&amp;nbsp;  &amp;nbsp;  &amp;nbsp;   context.Response.ContentType = dfi.MimeType;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&amp;nbsp;  &amp;nbsp;   dfi.FileName = (r["&lt;span style="color: darkred;"&gt;fileName&lt;/span&gt;&lt;span style="color: black;"&gt;"] &lt;/span&gt;&lt;span style="color: blue;"&gt;is&lt;/span&gt;&lt;span style="color: black;"&gt; DBNull) ?&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;&lt;/span&gt;  &amp;nbsp;   &lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;.Empty : (&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;)r["&lt;/span&gt;&lt;span style="color: darkred;"&gt;fileName&lt;/span&gt;&lt;span style="color: black;"&gt;"];&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&amp;nbsp;  &amp;nbsp;   dfi.Extention = (r["&lt;span style="color: darkred;"&gt;extention&lt;/span&gt;&lt;span style="color: black;"&gt;"] &lt;/span&gt;&lt;span style="color: blue;"&gt;is&lt;/span&gt;&lt;span style="color: black;"&gt; DBNull) ?&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;&lt;/span&gt;  &amp;nbsp;   &lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;.Empty : (&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;)r["&lt;/span&gt;&lt;span style="color: darkred;"&gt;extention&lt;/span&gt;&lt;span style="color: black;"&gt;"];&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&amp;nbsp;  &amp;nbsp;   context.Response.AddHeader("&lt;span style="color: darkred;"&gt;Content-Disposition&lt;/span&gt;&lt;span style="color: black;"&gt;",&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;&lt;/span&gt;  &amp;nbsp;   &lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;.Format("&lt;/span&gt;&lt;span style="color: darkred;"&gt;inline;filename={0}{1}&lt;/span&gt;&lt;span style="color: black;"&gt;",&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&amp;nbsp;  &amp;nbsp;  &amp;nbsp;  &amp;nbsp;   dfi.FileName, dfi.Extention));&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&amp;nbsp;  &amp;nbsp;   context.Response.AddHeader("&lt;span style="color: darkred;"&gt;Content-Length&lt;/span&gt;&lt;span style="color: black;"&gt;",&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;  &amp;nbsp;   dfi.Length.ToString());&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;&lt;/span&gt;  &amp;nbsp;   &lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; dataOrdinal = r.GetOrdinal("&lt;/span&gt;&lt;span style="color: darkred;"&gt;data&lt;/span&gt;&lt;span style="color: black;"&gt;");&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;&lt;/span&gt;  &amp;nbsp;   &lt;span style="color: blue;"&gt;if&lt;/span&gt;&lt;span style="color: black;"&gt; (!(r[dataOrdinal] &lt;/span&gt;&lt;span style="color: blue;"&gt;is&lt;/span&gt;&lt;span style="color: black;"&gt; DBNull))&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&amp;nbsp;  &amp;nbsp;  &amp;nbsp;  &amp;nbsp; StreamByteArrayInChunks(r, dataOrdinal, context);&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&amp;nbsp;   }&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;}
&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;   &lt;/span&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; StreamByteArrayInChunks(IDataReader r, &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; ordinal,
  &amp;nbsp;&lt;/span&gt;  &amp;nbsp;   HttpContext context)&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;   {
&lt;/span&gt;&lt;span style="color: green;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;&lt;/span&gt;   &lt;span class="Apple-style-span" style="color: green;"&gt;//102400 is 100kb at a time buffer&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;&lt;/span&gt;   &lt;span style="color: blue;"&gt;byte&lt;/span&gt;&lt;span style="color: black;"&gt;[] buffer = &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;byte&lt;/span&gt;&lt;span style="color: black;"&gt;[102400];&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;&lt;/span&gt;   &lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; index = 0;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;&lt;/span&gt;   &lt;span style="color: blue;"&gt;while&lt;/span&gt;&lt;span style="color: black;"&gt; (&lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&amp;nbsp;   {&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;&lt;/span&gt;  &amp;nbsp;   &lt;span style="color: blue;"&gt;long&lt;/span&gt;&lt;span style="color: black;"&gt; count = r.GetBytes(ordinal,&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&amp;nbsp;  &amp;nbsp;   index, buffer, 0, buffer.Length);&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;&lt;/span&gt;  &amp;nbsp;   &lt;span style="color: blue;"&gt;if&lt;/span&gt;&lt;span style="color: black;"&gt; (count == 0)&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&amp;nbsp;  &amp;nbsp;   {&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;&lt;/span&gt;  &amp;nbsp;  &amp;nbsp;   &lt;span style="color: blue;"&gt;break&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&amp;nbsp;  &amp;nbsp;   }&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;&lt;/span&gt;  &amp;nbsp;   &lt;span class="Apple-style-span" style="color: blue;"&gt;else&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&amp;nbsp;  &amp;nbsp;   {&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&amp;nbsp;  &amp;nbsp;  &amp;nbsp;   index = index + (&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;)count;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;  &amp;nbsp;  &amp;nbsp;   context.Response.BinaryWrite(buffer);&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;  &amp;nbsp;  &amp;nbsp;   context.Response.Flush();&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;  &amp;nbsp;   }&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;   }&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: green;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;&lt;/span&gt;   &lt;span class="Apple-style-span" style="color: green;"&gt;//closes datareader + underlying db connection&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&amp;nbsp;   r.Close();&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;}
&lt;span style="color: green;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;   &lt;/span&gt;// Override the IsReusable property.&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;   &lt;/span&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;bool&lt;/span&gt;&lt;span style="color: black;"&gt; IsReusable
   {
&lt;/span&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;  &amp;nbsp;&lt;/span&gt;&lt;/span&gt;   &lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;span style="color: black;"&gt; { &lt;/span&gt;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;; }&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;}
}
&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
&amp;nbsp;A few things i will explain quickly from the above code is how we retrieve an IDataReader from our DAL instead of retrieving a stream. The reason is i want to retrieve the filestream in chunks from the DataReader and every chunk i read from the database, i want to write it out to the client, as i receive the chunks without storing them in memory on my webserver where my application resides. &lt;br /&gt;
&lt;br /&gt;
Another thing you may want to notice is the usage of the Http Header Content-Disposition and Content-Length ; basically, it was important for me to flush out the file with the original filename when the user choses to save the file and Content-Disposition helped there. Whereas Content-Length helped ensure the filesize on the receiving end (the client) since without it i was getting errors unzipping a file for example where winzip complained that the file was corrupt :-)&lt;br /&gt;
&lt;br /&gt;
So that's pretty much it. And oh, the method in my DAL that returns the IDataReader follows : &lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;override&lt;/span&gt;&lt;span style="color: black;"&gt; IDataReader GetFileById(&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; fileId)
{
   SqlConnection myConnection = GetSqlConnection();
   myConnection.Open();
   SqlCommand cmd = &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; SqlCommand(DbOwner +
  &amp;nbsp;&lt;/span&gt;   &lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: darkred;"&gt;.DataFile_GetFileById&lt;/span&gt;&lt;span style="color: black;"&gt;", myConnection);&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;   cmd.CommandType = CommandType.StoredProcedure;
   cmd.Parameters.Add("&lt;/span&gt;&lt;span style="color: darkred;"&gt;@fileId&lt;/span&gt;&lt;span style="color: black;"&gt;",
  &amp;nbsp;&lt;/span&gt;   SqlDbType.Int, 4).Value = fileId;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;   SqlDataReader r = cmd.ExecuteReader(
  &amp;nbsp;&lt;/span&gt;   CommandBehavior.CloseConnection);&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;   &lt;/span&gt;return&lt;/span&gt;&lt;span style="color: black;"&gt; r;
}
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;With that i think we have succeeded in conserving resources on both our database server and our Web server. One last thing, when you got this all working, experiment by changing the buffer size and regulate it to the write speed that is acceptable as per your standards. Basically from my tests on my dev machine, i was getting a download speed of 500KB - 517KB/sec with a buffer size of 100kb (102400 bytes). While setting the same buffer size to 10kb (10240bytes), i was getting a download speed of 50KB - 60KB/sec ; &lt;br /&gt;
&lt;br /&gt;
These tests are ofcourse based on what i see as i request a file handled through my custom httphandler above. These values are more or less the same i reckon when streaming the same data to sql server in chunks or a good approximation, so use that as a starting point to regulate the desired speed/latency required by your app based on how much resources your webserver/database server has. Normally, uploads are usually restricted to a certain amount by most ISP's so it means you can set a much smaller chunk size when streaming from your application to sql server. And increase the chunk size when streaming from your webserver to the client (since your requesting clients download speeds are significantly high). It will also depend on the bandwidth availability of your webserver and other factors ofcourse, but that's off topic.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: red; font-weight: bold;"&gt;Update 23&amp;nbsp; september&amp;nbsp; 2008 :&lt;/span&gt; &lt;br /&gt;
&lt;br /&gt;
One last thing i forgot to mention but my friend &lt;a href="http://www.filip.duyck.org/"&gt;filip duyck&lt;/a&gt; brought up is that now since we are sending the data in chunks to sql server (whilst reusing the same connection), it's still additional hits to our database server. So consider that too while you evaluate how big you want your chunks to be.&lt;img src="http://feeds.feedburner.com/~r/typps/axAH/~4/6P5KWru_qyc" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.typps.com/feeds/8256997074223109947/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.typps.com/2008/09/conserving-resources-when-writing-blob.html#comment-form" title="3 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/8256997074223109947?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/8256997074223109947?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typps/axAH/~3/6P5KWru_qyc/conserving-resources-when-writing-blob.html" title="Conserving Resources When Writing BLOB Values to SQL Server and Streaming BLOB Values back to the Client" /><author><name>Alessandro</name><uri>http://www.blogger.com/profile/08620937587114860179</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="24" src="http://4.bp.blogspot.com/-9iFBqNPDD_E/TbtHLjeMgZI/AAAAAAAAAK0/9WMh7OUK6KA/s220/alessandro.png" /></author><thr:total>3</thr:total><feedburner:origLink>http://blog.typps.com/2008/09/conserving-resources-when-writing-blob.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0QEQn8zfip7ImA9WhZXEUo.&quot;"><id>tag:blogger.com,1999:blog-6778277710194170777.post-228814235049230140</id><published>2008-03-01T17:14:00.003+01:00</published><updated>2011-04-30T15:48:23.186+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-04-30T15:48:23.186+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET Webforms" /><title>Part 2 : Building and binding hierarchical data from the database to the ASP.NET Navigation Controls</title><content type="html">I wrote about how we can make use of some of the databinding capabilities of controls, that can bind to hierarchical data like the TreeView &lt;a href="http://blog.typps.com/2008/02/building-and-binding-hierarchical-data.html"&gt;around last week&lt;/a&gt;. While I covered pretty much everything, the data i was binding to was not deeply nested. Instead the data we were consuming was just 3 levels deep and that was that.&lt;br /&gt;
&lt;br /&gt;
&lt;div&gt;Today, we will be binding to a hierarchical datasource whose nesting structure is not known and can go as deep as it wants. Here, also SQL servers&amp;nbsp;XML capabilities seem to come short since it does not support recursions. so&amp;nbsp;if our data had this kind of deep nesting, SQL Servers FOR XML queries are not helping out much and we'd need to combine XSLT to our FOR XML Query. &lt;/div&gt;&lt;br /&gt;
&lt;div&gt;Because of that, and since we end up using XSL for the transformation anyway, i'm going to be using the Dataset/XSLT approach I discussed in my previous post.&lt;/div&gt;&lt;br /&gt;
&lt;div&gt;First, lets create a "pages" table in Sql server with the following schema as you can see in the screen shot below :&lt;/div&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-VgIZ-yVIijE/TamTRJV1xBI/AAAAAAAAAIQ/lbWrfn-HVLw/s1600/table-schema.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="94" src="http://1.bp.blogspot.com/-VgIZ-yVIijE/TamTRJV1xBI/AAAAAAAAAIQ/lbWrfn-HVLw/s320/table-schema.gif" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div&gt;As you can note from above, we have a parentId field which is a self related field, related to the pageId in the same table. I personally prefer to move this field out to a junction table but each approach has it's pro's and con's, and this is simpler and serves our purpose :-)&lt;/div&gt;&lt;br /&gt;
&lt;div&gt;Now, let's fill it with sample data, as you can note in the screenshot below :&lt;/div&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-OFI6Gty406s/TamTRkKxsAI/AAAAAAAAAIY/4T3caa1asSQ/s1600/Menu-Data.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="278" src="http://2.bp.blogspot.com/-OFI6Gty406s/TamTRkKxsAI/AAAAAAAAAIY/4T3caa1asSQ/s320/Menu-Data.gif" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span style="color: red;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;ohh perfect. Now it's time to put together some c# code : &lt;/div&gt;&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;protected&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; Page_Load(&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt; sender, EventArgs e)
{
&lt;/span&gt;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&lt;span style="color: black;"&gt; (!IsPostBack)
XmlDataSource1.Data = GetHierarchicalData();
}
&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt; GetHierarchicalData()
{
&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt; queryString =
"&lt;/span&gt;&lt;span style="color: darkred;"&gt;SELECT pageId, pageDisplayName, parentId FROM pages;&lt;/span&gt;&lt;span style="color: black;"&gt;";
DataSet ds = &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; DataSet("&lt;/span&gt;&lt;span style="color: darkred;"&gt;Pages&lt;/span&gt;&lt;span style="color: black;"&gt;");
&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt; connectionString = ConfigurationManager.
ConnectionStrings["&lt;/span&gt;&lt;span style="color: darkred;"&gt;LocalSqlServer&lt;/span&gt;&lt;span style="color: black;"&gt;"].ConnectionString;
&lt;/span&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt;&lt;span style="color: black;"&gt; (SqlConnection connection = &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; SqlConnection(
connectionString))
{
SqlDataAdapter adapter = &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; SqlDataAdapter();
adapter.SelectCommand = &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; SqlCommand(
queryString, connection);
adapter.Fill(ds);

ds.Tables[0].TableName = "&lt;/span&gt;&lt;span style="color: darkred;"&gt;PageItem&lt;/span&gt;&lt;span style="color: black;"&gt;";
&lt;/span&gt;&lt;span style="color: green;"&gt;// relate our tables&lt;/span&gt;&lt;span style="color: black;"&gt;
DataRelation dr = &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; DataRelation("&lt;/span&gt;&lt;span style="color: darkred;"&gt;FK_pageId_parentId&lt;/span&gt;&lt;span style="color: black;"&gt;", 
ds.Tables["&lt;/span&gt;&lt;span style="color: darkred;"&gt;PageItem&lt;/span&gt;&lt;span style="color: black;"&gt;"].Columns["&lt;/span&gt;&lt;span style="color: darkred;"&gt;pageId&lt;/span&gt;&lt;span style="color: black;"&gt;"],
ds.Tables["&lt;/span&gt;&lt;span style="color: darkred;"&gt;PageItem&lt;/span&gt;&lt;span style="color: black;"&gt;"].Columns["&lt;/span&gt;&lt;span style="color: darkred;"&gt;parentId&lt;/span&gt;&lt;span style="color: black;"&gt;"]);
&lt;/span&gt;&lt;span style="color: green;"&gt;// we'd like the page items nested within &lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: green;"&gt;// each node for every child page.&lt;/span&gt;&lt;span style="color: black;"&gt;
dr.Nested = &lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;;
ds.Relations.Add(dr);
}
&lt;/span&gt;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&lt;span style="color: black;"&gt; ds.GetXml();
}


&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div&gt;The xml returned by our dataset looks like this : &lt;a class="" href="https://sites.google.com/a/typps.com/home/menuxml.txt?attredirects=0&amp;amp;d=1"&gt;Dataset generated XML&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div&gt;Notice how every pageId that had a corresponding parentId was nested within it's parent, forming a hiearchical set of data. This is just perfect, all thanks to the dataset's DataRelation capabilities and our setting Nested=true on the DataRelation. This kind of recusive nesting didn't seem supported by SQL Servers FOR XML queries, and if it was, then it's probably trival. I did find a few implicit hints in the documentation that recursive nesting was not supported on SQL Server FOR XML Queries, however i could be wrong (so please do your homework). &lt;/div&gt;&lt;br /&gt;
&lt;div&gt;Now our XSL file that does the recursion, but it's easier to make the transformation&amp;nbsp;now, because the data is already structured out nicely with the proper hierarchical structure. Only thing missing and why we need to perform transformation is as i explained in the previous post, we need the fields as attributes and not as xml elements. So here is our recursive xslt that does just that : &lt;/div&gt;&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color: black;"&gt;xml version="1.0" encoding="utf-8"&lt;/span&gt;&lt;span style="color: blue;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;

&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;stylesheet&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;version&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"1.0"&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: red;"&gt;xmlns&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: red;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"http://www.w3.org/1999/XSL/Transform"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;output&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;method&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"xml"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;template&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Menu"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;match&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"/"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;element&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Pages"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;for&lt;/span&gt;&lt;span style="color: black;"&gt;-&lt;/span&gt;&lt;span style="color: red;"&gt;each&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;select&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Pages/PageItem"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;variable&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"parentId"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;select&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"parentId/text()"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;

&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;element&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"PageItem"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;attribute&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"pageId"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;value&lt;/span&gt;&lt;span style="color: black;"&gt;-&lt;/span&gt;&lt;span style="color: red;"&gt;of&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;select&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"pageId/text()"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;attribute&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;attribute&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"pageDisplayName"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;value&lt;/span&gt;&lt;span style="color: black;"&gt;-&lt;/span&gt;&lt;span style="color: red;"&gt;of&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;select&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"pageDisplayName/text()"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;attribute&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;

&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;call&lt;/span&gt;&lt;span style="color: black;"&gt;-&lt;/span&gt;&lt;span style="color: red;"&gt;template&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"processChildren"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;

&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;element&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;for&lt;/span&gt;&lt;span style="color: black;"&gt;-each&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;element&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;template&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: green;"&gt;&amp;lt;!-- recursive template --&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;template&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"processChildren"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;for&lt;/span&gt;&lt;span style="color: black;"&gt;-&lt;/span&gt;&lt;span style="color: red;"&gt;each&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;select&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"PageItem"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;element&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"PageItem"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;attribute&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"pageId"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;value&lt;/span&gt;&lt;span style="color: black;"&gt;-&lt;/span&gt;&lt;span style="color: red;"&gt;of&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;select&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"pageId/text()"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;attribute&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;attribute&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"pageDisplayName"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;value&lt;/span&gt;&lt;span style="color: black;"&gt;-&lt;/span&gt;&lt;span style="color: red;"&gt;of&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;select&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"pageDisplayName/text()"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;attribute&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;call&lt;/span&gt;&lt;span style="color: black;"&gt;-&lt;/span&gt;&lt;span style="color: red;"&gt;template&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"processChildren"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;element&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;for&lt;/span&gt;&lt;span style="color: black;"&gt;-each&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;template&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;stylesheet&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;



&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div&gt;And here is the output of this miraculous transformation : &lt;a class="" href="https://sites.google.com/a/typps.com/home/menuxslt.txt?attredirects=0&amp;amp;d=1" target="_blank"&gt;XSL Transformed XML&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div&gt;Ohhh what beauty :-) and to complete, here is&amp;nbsp;the declarative code to bind our asp.net menu to the XmlDataSource control that consumes this data :&lt;/div&gt;&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;form&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;id&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"form1"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;runat&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"server"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;div&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;asp&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;Menu&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;ID&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Menu1"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;MaximumDynamicDisplayLevels&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"1000"&lt;/span&gt;&lt;span style="color: black;"&gt; 
&lt;/span&gt;&lt;span style="color: red;"&gt;Orientation&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Horizontal"&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: red;"&gt;DataSourceID&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"XmlDataSource1"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;runat&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"server"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;DataBindings&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;asp&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;MenuItemBinding&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;DataMember&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"PageItem"&lt;/span&gt;&lt;span style="color: black;"&gt; 
&lt;/span&gt;&lt;span style="color: red;"&gt;TextField&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"pageDisplayName"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;ValueField&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"pageId"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;DataBindings&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;asp&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;Menu&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;asp&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;XmlDataSource&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;ID&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"XmlDataSource1"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;XPath&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Pages/PageItem"&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: red;"&gt;TransformFile&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"~/MenuTransform.xsl"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;runat&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"server"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;asp&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;XmlDataSource&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;div&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;form&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;div&gt;One gotcha you want to&amp;nbsp;watch out for is the root node showing in the menu. Since we do not want to show the starting node, we&amp;nbsp;explain this to the XmlDataSource nicely&amp;nbsp;by setting an XPath expression : XPath="Pages/PageItem". &lt;/div&gt;&lt;br /&gt;
&lt;div&gt;And below is our menu, when previewed in the browser. Time to congratulate ourselves on a job well done :-)&lt;/div&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-U_kNOl3QfrQ/TamTRRt9bTI/AAAAAAAAAIU/tl_RGu7cPDE/s1600/Menu-Final-Product.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="143" src="http://1.bp.blogspot.com/-U_kNOl3QfrQ/TamTRRt9bTI/AAAAAAAAAIU/tl_RGu7cPDE/s320/Menu-Final-Product.gif" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div&gt;&lt;/div&gt;&lt;h1 style="color: red;"&gt;Update 21 May 2008&lt;/h1&gt;&lt;span style="color: red;"&gt;A note i forgot to mention is that the XmlDataSource control has caching turned on by default. So in case you made a change in your xslt file and didn't see the change occuring, then you know it's using a cached copy. So make sure you disable caching during development.&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: red;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;img src="http://feeds.feedburner.com/~r/typps/axAH/~4/BodN53BALhM" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.typps.com/feeds/228814235049230140/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.typps.com/2008/03/part-2-building-and-binding.html#comment-form" title="9 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/228814235049230140?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/228814235049230140?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typps/axAH/~3/BodN53BALhM/part-2-building-and-binding.html" title="Part 2 : Building and binding hierarchical data from the database to the ASP.NET Navigation Controls" /><author><name>Alessandro</name><uri>http://www.blogger.com/profile/08620937587114860179</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="24" src="http://4.bp.blogspot.com/-9iFBqNPDD_E/TbtHLjeMgZI/AAAAAAAAAK0/9WMh7OUK6KA/s220/alessandro.png" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/-VgIZ-yVIijE/TamTRJV1xBI/AAAAAAAAAIQ/lbWrfn-HVLw/s72-c/table-schema.gif" height="72" width="72" /><thr:total>9</thr:total><feedburner:origLink>http://blog.typps.com/2008/03/part-2-building-and-binding.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0QCR3s_fip7ImA9WhZXEUo.&quot;"><id>tag:blogger.com,1999:blog-6778277710194170777.post-5980663679726872117</id><published>2008-02-20T22:30:00.003+01:00</published><updated>2011-04-30T15:49:26.546+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-04-30T15:49:26.546+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET Webforms" /><title>Building and binding hierarchical data from the database to the ASP.NET Navigation Controls</title><content type="html">If we need to bind our navigations controls to hierarchical data we define manually ourselves in an xml file, this is easy as pie. However, things can get rather complicated or not so obvious when we need to generate this data from a database. First off, what can we use that is already provided to us for binding hierarchical data to our navigation controls in ASP.NET ?&lt;br /&gt;
&lt;br /&gt;
&lt;div&gt;The already out of the box approach and ideal solution is to use the XmlDataSource control. This is quite a flexible datasource control since it not only enables us to define the path to our xml file containing the structure we need but also it allows us to define xml data to it via it's "Data" property. As you may have already guessed, because our data is going to be retrieved from our database, this is the property we shall be using :-)&lt;/div&gt;&lt;br /&gt;
&lt;div&gt;But first let's look at a sample data structure we may have in our database. I'm using the classic Northwind database. Let's imagine we want to display products grouped by category. So in short, for every category node, we want to show products under it. Following is a screenshot of the categories and products table and how they relate :&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-lvKRfFhy-tk/TamKrvj02qI/AAAAAAAAAIM/rXWVCRV8lOU/s1600/categories_products.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="320" src="http://2.bp.blogspot.com/-lvKRfFhy-tk/TamKrvj02qI/AAAAAAAAAIM/rXWVCRV8lOU/s320/categories_products.gif" width="187" /&gt;&lt;/a&gt;&lt;/div&gt;The most common thing i see done is to manually loop through the records returned, create TreeNodes again manually and keep adding till you've build the TreeView or Menu, etc. Nothing wrong with this approach, since it works, however it is quite lengthy in code and time consuming too. &lt;br /&gt;
&lt;br /&gt;
But that's not the main reason why I'm writing this article. The main reason is that all these navigation controls in ASP.NET know how to consume hierarchical data. Once they have this, they know how to render themselves without you needing to do anything special. This is indeed some powerful databinding support that we miss out on when we go the manual approach. Here i am going to list two different approaches :&lt;br /&gt;
&lt;br /&gt;
&lt;div&gt;1) DataSet and XSL Transformations, which while being clean and gives a more declarative model to work with (XSLT) versus the manual c# code approach. &lt;/div&gt;&lt;br /&gt;
&lt;div&gt;2)The second approach uses SqlServer's XML generating capabilities which allows us to skip XSLT and the dataset all together.&lt;/div&gt;&lt;h1&gt;DataSet and XSL Transformation approach:&lt;/h1&gt;Note below how our select statements retrieves all categories, while the second statement retrieves all products. We then relate both these tables using a key field they have in common "CategoryID". We also use a dataset since it's providing us a lot of functionality like relating the tables after data retrieval and representing the data in xml.&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt; GetHierarchicalData()
{
&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt; queryString =
"&lt;/span&gt;&lt;span style="color: darkred;"&gt;SELECT CategoryID, CategoryName, Description FROM Categories AS Category;&lt;/span&gt;&lt;span style="color: black;"&gt;";
queryString +=
"&lt;/span&gt;&lt;span style="color: darkred;"&gt;SELECT ProductID, CategoryID, ProductName FROM Products AS Product&lt;/span&gt;&lt;span style="color: black;"&gt;";
DataSet ds = &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; DataSet("&lt;/span&gt;&lt;span style="color: darkred;"&gt;TreeView&lt;/span&gt;&lt;span style="color: black;"&gt;");
&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt; connectionString = ConfigurationManager
.ConnectionStrings["&lt;/span&gt;&lt;span style="color: darkred;"&gt;LocalSqlServer&lt;/span&gt;&lt;span style="color: black;"&gt;"].ConnectionString;
&lt;/span&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt;&lt;span style="color: black;"&gt; (SqlConnection connection = &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; SqlConnection(
connectionString))
{
SqlDataAdapter adapter = &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; SqlDataAdapter();
adapter.SelectCommand = &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; SqlCommand(
queryString, connection);
adapter.Fill(ds);

ds.Tables[0].TableName = "&lt;/span&gt;&lt;span style="color: darkred;"&gt;Category&lt;/span&gt;&lt;span style="color: black;"&gt;";
ds.Tables[1].TableName = "&lt;/span&gt;&lt;span style="color: darkred;"&gt;Product&lt;/span&gt;&lt;span style="color: black;"&gt;";
&lt;/span&gt;&lt;span style="color: green;"&gt;// relate our tables&lt;/span&gt;&lt;span style="color: black;"&gt;
DataRelation dr = &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; DataRelation("&lt;/span&gt;&lt;span style="color: darkred;"&gt;FK_Products_Categories&lt;/span&gt;&lt;span style="color: black;"&gt;",
ds.Tables["&lt;/span&gt;&lt;span style="color: darkred;"&gt;Category&lt;/span&gt;&lt;span style="color: black;"&gt;"].Columns["&lt;/span&gt;&lt;span style="color: darkred;"&gt;categoryId&lt;/span&gt;&lt;span style="color: black;"&gt;"],
ds.Tables["&lt;/span&gt;&lt;span style="color: darkred;"&gt;Product&lt;/span&gt;&lt;span style="color: black;"&gt;"].Columns["&lt;/span&gt;&lt;span style="color: darkred;"&gt;categoryId&lt;/span&gt;&lt;span style="color: black;"&gt;"]);
&lt;/span&gt;&lt;span style="color: green;"&gt;// we'd like the products nested within &lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: green;"&gt;// each Category Node. Thank you :-)&lt;/span&gt;&lt;span style="color: black;"&gt;
dr.Nested = &lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;&lt;span style="color: black;"&gt;;
ds.Relations.Add(dr);
}
&lt;/span&gt;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&lt;span style="color: black;"&gt; ds.GetXml();
}
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
A small sample output of the generated xml we get by calling dataset's GetXml() method is here -&amp;gt;&amp;nbsp; &lt;a href="https://sites.google.com/a/typps.com/home/DataSetGenerated.txt?attredirects=0&amp;amp;d=1"&gt;DataSet Generated XML&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
As you can see, we have our rootnode "TreeView", then a childNode "Category", which in turn will contain every product node within it that belongs to this category. This nesting was established when we created a relationship btw the Categories table and the Products table using a DataRelation, where we set Nested = true; on it. Enabling the Nested property did just what the name says. It nested all our products within it's specific Category node. &lt;br /&gt;
&lt;br /&gt;
While this is great, it does not help much with the TreeView control. Notice how every field our select statement returned is now an xml node and the data for the field is contained as inner text in our node. The TreeNodeBinding instead expects the fields to be contained as attributes, and the data as attribute values. Following screenshot is what we get when we run our TreeView bound to XmlDatasource &lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-tEFax5iBOWQ/TamKrXvLduI/AAAAAAAAAII/ex9mt0zgkco/s1600/TreeView-Without-Transforma.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-tEFax5iBOWQ/TamKrXvLduI/AAAAAAAAAII/ex9mt0zgkco/s1600/TreeView-Without-Transforma.gif" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
Definately, not what we are after. This is because the datasets GetXml method returned :&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Category&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;CategoryID&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;1&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;CategoryID&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;CategoryName&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;Beverages&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;CategoryName&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Description&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;Soft drinks, coffees, teas, beers, and ales&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Description&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
while, what we need instead for the TreeNodeBinding to work is : &lt;br /&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Category &lt;/span&gt;&lt;span style="color: red;"&gt;CategoryID&lt;/span&gt;&lt;span style="color: blue;"&gt;="1"&lt;/span&gt;&lt;span style="color: red;"&gt; CategoryName&lt;/span&gt;&lt;span style="color: blue;"&gt;="Beverages"&lt;/span&gt;&lt;span style="color: red;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style="color: red;"&gt;     Description&lt;/span&gt;&lt;span style="color: blue;"&gt;="Soft drinks, coffees, teas, beers, and ales"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;
Had we the above xml structure with the fields defined as attributes we could easily create a TreeNodeBinding like this : &lt;br /&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:TreeNodeBinding &lt;/span&gt;&lt;span style="color: red;"&gt;Depth&lt;/span&gt;&lt;span style="color: blue;"&gt;="2"&lt;/span&gt;&lt;span style="color: red;"&gt; DataMember&lt;/span&gt;&lt;span style="color: blue;"&gt;="Category"&lt;/span&gt;&lt;span style="color: red;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style="color: red;"&gt;  TextField&lt;/span&gt;&lt;span style="color: blue;"&gt;="CategoryName"&lt;/span&gt;&lt;span style="color: red;"&gt; ValueField&lt;/span&gt;&lt;span style="color: blue;"&gt;="CategoryID"&lt;/span&gt;&lt;span style="color: red;"&gt; ToolTipField&lt;/span&gt;&lt;span style="color: blue;"&gt;="Description"&lt;/span&gt;&lt;span style="color: red;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;
So, our next task is to transform our xml to represent fields as attributes and their data as attribute values. We can accomplish this easily using XSL Transformations. By defining some instructions in our XSL file, we can read our XML(what our dataset GetXml method output) and transform it into what the Navigation Controls expect, outputting a totally new XML document.&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color: black;"&gt;xml version="1.0" encoding="utf-8"&lt;/span&gt;&lt;span style="color: blue;"&gt;?&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;transform&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;version&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"1.0"&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: red;"&gt;xmlns&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: red;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"http://www.w3.org/1999/XSL/Transform"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;output&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;method&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"xml"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;template&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;match&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"/"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;element&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"TreeView"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;for&lt;/span&gt;&lt;span style="color: black;"&gt;-&lt;/span&gt;&lt;span style="color: red;"&gt;each&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;select&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"TreeView/Category"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;element&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Category"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;attribute&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"id"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;value&lt;/span&gt;&lt;span style="color: black;"&gt;-&lt;/span&gt;&lt;span style="color: red;"&gt;of&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;select&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"CategoryID/text()"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;attribute&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;attribute&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"name"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;value&lt;/span&gt;&lt;span style="color: black;"&gt;-&lt;/span&gt;&lt;span style="color: red;"&gt;of&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;select&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"CategoryName/text()"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;attribute&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;attribute&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"description"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;value&lt;/span&gt;&lt;span style="color: black;"&gt;-&lt;/span&gt;&lt;span style="color: red;"&gt;of&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;select&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Description/text()"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;attribute&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;for&lt;/span&gt;&lt;span style="color: black;"&gt;-&lt;/span&gt;&lt;span style="color: red;"&gt;each&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;select&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Product"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;element&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Product"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;attribute&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"id"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;value&lt;/span&gt;&lt;span style="color: black;"&gt;-&lt;/span&gt;&lt;span style="color: red;"&gt;of&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;select&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"ProductID/text()"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;attribute&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;attribute&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"name"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;value&lt;/span&gt;&lt;span style="color: black;"&gt;-&lt;/span&gt;&lt;span style="color: red;"&gt;of&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;select&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"ProductName/text()"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;attribute&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;element&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;for&lt;/span&gt;&lt;span style="color: black;"&gt;-each&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;element&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;for&lt;/span&gt;&lt;span style="color: black;"&gt;-each&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;element&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;template&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;xsl&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;transform&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;

&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
As you can see from the above XSL, we have defined two for-each loops, one that loops through category nodes and the other that loops through product nodes. Very simple but powerful stuff indeed.&lt;br /&gt;
The output xml after this transformation is &lt;a href="https://sites.google.com/a/typps.com/home/XSLTransformedXML.txt?attredirects=0&amp;amp;d=1"&gt;XSL Transformed Xml output&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;TreeView&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Category&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;CategoryName&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;""&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;id&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;""&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;description&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;""&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Product&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;""&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;id&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;""&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
...
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Category&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
...
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;TreeView&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
ohh this is perfect. It was fun using XSL for the transmformation. Now, now, while it was fun, it can be a big pain in the behind if I'd have to do this all over again and again (Fortunately, i don't. Atleast now right now) :P&lt;br /&gt;
&lt;br /&gt;
So, can we have made this job easier ? Surely. SQL Server and FOR XML Queries to the rescue&lt;br /&gt;
&lt;h1&gt;SQL Server and FOR XML Queries approach :&lt;/h1&gt;Now, what if, instead of having to do all this manual labor, we could get SQL Server to do all the xml generation for us, the way we wanted it ? Very much possible indeed. In effect this does not even need any explainations. Code speaks a thousand words, so here it is, the same xml output but this time we didn't use XSLT, nor did we use a dataset and the code is even more minimized.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt; GetHierarchicalDataFromSqlServer()
{
&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt; xml = &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;.Empty;
&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt; queryString = @"&lt;/span&gt;&lt;span style="color: darkred;"&gt;
SELECT Category.categoryName as [name], Category.categoryId as id, 
Category.description as description, 
Product.productName as name, Product.productId as id
FROM categories AS Category 
INNER JOIN products AS Product
ON Category.categoryId = Product.categoryId
ORDER BY Category.categoryId                    
FOR XML Auto, ROOT('TreeView')&lt;/span&gt;&lt;span style="color: black;"&gt;";

&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt; connectionString = ConfigurationManager.
ConnectionStrings["&lt;/span&gt;&lt;span style="color: darkred;"&gt;LocalSqlServer&lt;/span&gt;&lt;span style="color: black;"&gt;"].ConnectionString;

&lt;/span&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt;&lt;span style="color: black;"&gt; (SqlConnection connection = &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; SqlConnection(
connectionString))
{
SqlCommand SelectCommand = &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; SqlCommand(
queryString, connection);
connection.Open();
XmlReader xr = SelectCommand.ExecuteXmlReader();
xr.MoveToContent();&lt;/span&gt;&lt;span style="color: green;"&gt;// move to the root node&lt;/span&gt;&lt;span style="color: black;"&gt;
xml = xr.ReadOuterXml();
}
&lt;/span&gt;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&lt;span style="color: black;"&gt; xml;
}

&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;The c# code to pass the XML data to our XmlDataSource bound to a treeView : &lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;protected&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; Page_Load(&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt; sender, EventArgs e)
{
&lt;/span&gt;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&lt;span style="color: black;"&gt; (!IsPostBack)
XmlDataSource1.Data = GetHierarchicalDataFromSqlServer();
}
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
That's it. No fuss, SQL Server's xml generation capabilities are just outstanding. And here is the declarative code i used to bind this hiearchical data to a treeview&amp;nbsp;: &lt;br /&gt;
&lt;br /&gt;
&lt;div class="syntaxhighlighter" style="float: left; font-family: courier new; font-size: 14px; padding: 0 5px 0 5px;"&gt;&lt;div class="linecontent" style="float: left; vertical-align: top;"&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;form&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;id&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"form1"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;runat&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"server"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;div&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;asp&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;TreeView&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;ID&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"TreeView1"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;DataSourceID&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"XmlDataSource1"&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;                 runat&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"server"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;DataBindings&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;asp&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;TreeNodeBinding&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Depth&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"1"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;DataMember&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Category"&lt;/span&gt;&lt;span style="color: black;"&gt; 
&lt;/span&gt;&lt;span style="color: red;"&gt;TextField&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"name"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;ValueField&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"id"&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;                                  ToolTipField&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Description"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;asp&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;TreeNodeBinding&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;Depth&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"2"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;DataMember&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"Product"&lt;/span&gt;&lt;span style="color: black;"&gt; 
&lt;/span&gt;&lt;span style="color: red;"&gt;TextField&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"name"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;ValueField&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"id"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;DataBindings&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;asp&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;TreeView&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;div&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;asp&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;XmlDataSource&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;ID&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"XmlDataSource1"&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0;"&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: red;"&gt;                  runat&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: blue;"&gt;"server"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: mediumvioletred;"&gt;asp&lt;/span&gt;&lt;span style="color: black;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;XmlDataSource&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;form&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;&lt;br /&gt;
And here below is a screenshot of the treeview rendering itself. Fantastic!&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-9knMXOnEBok/TamKrOOJowI/AAAAAAAAAIE/eEKSR1eyAlo/s1600/TreeView-After-XSLT.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="320" src="http://1.bp.blogspot.com/-9knMXOnEBok/TamKrOOJowI/AAAAAAAAAIE/eEKSR1eyAlo/s320/TreeView-After-XSLT.gif" width="158" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;h1 style="color: red;"&gt;Update 21 May 2008&lt;/h1&gt;&lt;span style="color: red;"&gt;A note i forgot to mention is that the XmlDataSource control has caching turned on by default. So in case you made a change in your xslt file and didn't see the change occuring, then you know it's using a cached copy. So make sure you disable caching during development.&lt;/span&gt;&lt;img src="http://feeds.feedburner.com/~r/typps/axAH/~4/OYND4E3B334" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.typps.com/feeds/5980663679726872117/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.typps.com/2008/02/building-and-binding-hierarchical-data.html#comment-form" title="10 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/5980663679726872117?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/5980663679726872117?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typps/axAH/~3/OYND4E3B334/building-and-binding-hierarchical-data.html" title="Building and binding hierarchical data from the database to the ASP.NET Navigation Controls" /><author><name>Alessandro</name><uri>http://www.blogger.com/profile/08620937587114860179</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="24" src="http://4.bp.blogspot.com/-9iFBqNPDD_E/TbtHLjeMgZI/AAAAAAAAAK0/9WMh7OUK6KA/s220/alessandro.png" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-lvKRfFhy-tk/TamKrvj02qI/AAAAAAAAAIM/rXWVCRV8lOU/s72-c/categories_products.gif" height="72" width="72" /><thr:total>10</thr:total><feedburner:origLink>http://blog.typps.com/2008/02/building-and-binding-hierarchical-data.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0MGQHsyeyp7ImA9WhZXEUs.&quot;"><id>tag:blogger.com,1999:blog-6778277710194170777.post-7277250473434725735</id><published>2008-02-15T16:47:00.002+01:00</published><updated>2011-04-30T15:17:01.593+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-04-30T15:17:01.593+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET Webforms" /><title>Customizing the ChangePassword control and removing the required CurrentPassword field</title><content type="html">&lt;div&gt;It's very rare that what is already provided in asp.net under the Login controls fits my requirements out of the box without some tweaking. Not that any of these controls offer anything specialized, but certainly they are a big time saver if we can re-utilize their functionality. &lt;/div&gt;&lt;br /&gt;
&lt;div&gt;First some background as to why i personally want to customize the ChangePassword control to suit my needs : &lt;/div&gt;&lt;br /&gt;
&lt;div&gt;Password recovery is what i was after today, however i have hashed passwords, and recovery is impossible. If the user lost their password, then there is no way for me to know what their password is and send it back in clear text. &lt;/div&gt;&lt;br /&gt;
&lt;div&gt;The ideal solution is to reset the password, however the autogenerated password is quite ugly and quite hard to remember. What I've decided to do is send the email during password recovery, but as part of the email, instead of telling the user their old password(which i can't).&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;I'm instead going to ask them to click on a tokenized link that will guarantee to me that they are indeed the ones that requested the password, send them to the page where they can provide a new password, in the background i'd be autogenerating a password first ofcourse, then updating the password with their new password because the MembershipUser.ChangePassword(oldPassword, newPassword) method requires Old password as one of it's two parameters.&lt;/div&gt;&lt;br /&gt;
&lt;div&gt;This password change step, i'd like to be done using the ChangePassword control, however to my big surprise CurrentPassword Field is a required field that i cannot remove. This is also a field that I do not want asked for during the password change request(since my user has forgotten their password and are now going to provide their new pasword).&lt;/div&gt;&lt;br /&gt;
&lt;div&gt;There is ofcourse no property or method in this control that removes the CurrentPassword field requirement, below is a screenshot of the ChangePassword control in designview, as you can note, the highlighted field is the CurrentPassword field i do not want. &lt;/div&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-TLUpOZr89ZM/Talw13ktKwI/AAAAAAAAAIA/Vo_-0ISs_IY/s1600/ChangePassword1.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="131" src="http://1.bp.blogspot.com/-TLUpOZr89ZM/Talw13ktKwI/AAAAAAAAAIA/Vo_-0ISs_IY/s320/ChangePassword1.gif" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div&gt;I've done a quick look on google and in the asp.net/forums and didn't find anybody providing any proper solutions either,&amp;nbsp;mostly vague replies : &lt;a href="http://forums.asp.net/p/1189347/2038354.aspx"&gt;http://forums.asp.net/p/1189347/2038354.aspx&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div&gt;As you can read from the posts there, the issue seems to be two things which&amp;nbsp;were also my same issues&amp;nbsp;: &lt;br /&gt;
1) Remove the current password label/TextBox &lt;br /&gt;
2) Pass the new resetpassword to CurrentPassword Property which by the way is a getter only and not settable (SAD SAD)&lt;/div&gt;&lt;br /&gt;
&lt;div&gt;Both of these things are not supported in this control. So let's quickly fix requirement 1 and there are a couple of ways to fix this :&lt;br /&gt;
a) You have to define a custom&amp;nbsp; &amp;lt;ChangePasswordTemplate&amp;gt;. This can be easily done by taking your ChangePassword control into DesignView in Visual studio, right click on the control and select "Convert to template". You can then switch to HtmlView and set the visibility of CurrentPasswordLabel, CurrentPassword and CurrentPasswordRequired controls.&lt;/div&gt;&lt;br /&gt;
&lt;div&gt;b) If you prefer to do this in code, then you can find the Label and TextBox for CurrentPassword and set its visiblity to false. Since a is a nobrainer, i'm including a sample code of method (b) : &lt;/div&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;Label l &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; (Label)changePassword1.ChangePasswordTemplateContainer.&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;         FindControl(&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;CurrentPasswordLabel&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;);&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&lt;span style="color: black;"&gt; (l &lt;/span&gt;&lt;span style="color: black;"&gt;!=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;null&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;{
    l.Visible &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;;
}
TextBox tb &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; (TextBox)changePassword1.ChangePasswordTemplateContainer.
FindControl(&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;CurrentPassword&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;);&lt;/span&gt;&lt;span style="color: blue;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&lt;span style="color: black;"&gt; (tb &lt;/span&gt;&lt;span style="color: black;"&gt;!=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;null&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;{
    tb.Visible &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;;
}
RequiredFieldValidator rfv &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;   &amp;nbsp;&lt;/span&gt;    (RequiredFieldValidator)changePassword1.&lt;/pre&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;ChangePasswordTemplateContainer.FindControl(&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;CurrentPasswordRequired&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;);&lt;/span&gt;&lt;span style="color: blue;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&lt;span style="color: black;"&gt; (rfv &lt;/span&gt;&lt;span style="color: black;"&gt;!=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;null&lt;/span&gt;&lt;span style="color: black;"&gt;)
{
    rfv.Visible &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;/span&gt;&lt;/pre&gt;}&lt;br /&gt;
&lt;br /&gt;
Now that we have the fields we want disabled, let's head onto fix issue 2 : &lt;br /&gt;
We can't pass the Autogenerated password to the CurrentPassword Property because its a getter only, however this getter returns the value from our CurrentPassword TextBox, and this job is done immidiately after ChangingPassword event fires. This is good news for us, so we can resolve issue 2 like this : &lt;br /&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; changePassword1_ChangingPassword(&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt; sender, 
LoginCancelEventArgs e)
{
    changePassword1.UserName &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; user.UserName;
    TextBox currentPassword &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; (TextBox)changePassword1.
    ChangePasswordTemplateContainer.FindControl(&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;CurrentPassword&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;);&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&lt;span class="Apple-style-span" style="color: black;"&gt;    &lt;/span&gt;if&lt;/span&gt;&lt;span style="color: black;"&gt; (currentPassword &lt;/span&gt;&lt;span style="color: black;"&gt;!=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;null&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;    {
   &amp;nbsp;&lt;/span&gt;    &lt;span style="color: black;"&gt;currentPassword.Text &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; user.ResetPassword();&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;    }&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;}&lt;/pre&gt;&lt;br /&gt;
Note that in the above code, user is a reference to a field of type&amp;nbsp;MembershipUser. Ok, that's it. Now we have what were after, look at the screenshot below :&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-WFdIizMHEF4/Talw1WFoybI/AAAAAAAAAH8/06LcenCyfj0/s1600/ChangePassword2.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="130" src="http://4.bp.blogspot.com/-WFdIizMHEF4/Talw1WFoybI/AAAAAAAAAH8/06LcenCyfj0/s320/ChangePassword2.gif" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/typps/axAH/~4/vEjiG4yINTM" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.typps.com/feeds/7277250473434725735/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.typps.com/2008/02/customizing-changepassword-control-and.html#comment-form" title="10 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/7277250473434725735?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/7277250473434725735?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typps/axAH/~3/vEjiG4yINTM/customizing-changepassword-control-and.html" title="Customizing the ChangePassword control and removing the required CurrentPassword field" /><author><name>Alessandro</name><uri>http://www.blogger.com/profile/08620937587114860179</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="24" src="http://4.bp.blogspot.com/-9iFBqNPDD_E/TbtHLjeMgZI/AAAAAAAAAK0/9WMh7OUK6KA/s220/alessandro.png" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/-TLUpOZr89ZM/Talw13ktKwI/AAAAAAAAAIA/Vo_-0ISs_IY/s72-c/ChangePassword1.gif" height="72" width="72" /><thr:total>10</thr:total><feedburner:origLink>http://blog.typps.com/2008/02/customizing-changepassword-control-and.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0cMQ3Y9eyp7ImA9WhZXEUs.&quot;"><id>tag:blogger.com,1999:blog-6778277710194170777.post-4946226906523960609</id><published>2008-01-30T12:41:00.001+01:00</published><updated>2011-04-30T15:11:22.863+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-04-30T15:11:22.863+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET Webforms" /><title>Reducing UpdatePanel bloat by utilizing UpdateMode="Conditional" and ChildrenAsTriggers="false"</title><content type="html">Just the other day, i was playing around with my DataControls nested inside an updatepanel. While this was working well, since everypostback was being done via an ajax callback, the amount of traffic going back and forth was simply way too bloated. It's easy not to notice at first, because everything is working as expected. however imagine a simple situation as the following pseudo code below. Things could be very complex, depending on how many datacontrols you have and the level of nesting. &lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:updatepanel &lt;/span&gt;&lt;span style="color: red;"&gt;id&lt;/span&gt;&lt;span style="color: blue;"&gt;="UpdatePanel1"&lt;/span&gt;&lt;span style="color: red;"&gt; runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;ContentTemplate&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;    &amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:GridView &lt;/span&gt;&lt;span style="color: red;"&gt;ID&lt;/span&gt;&lt;span style="color: blue;"&gt;="GridView1"&lt;/span&gt;&lt;span style="color: red;"&gt; AutoGenerateSelectButton&lt;/span&gt;&lt;span style="color: blue;"&gt;="true"&lt;/span&gt;&lt;span style="color: red;"&gt; 
runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&lt;/span&gt;&lt;span style="color: red;"&gt; 
OnSelectedIndexChanged&lt;/span&gt;&lt;span style="color: blue;"&gt;="GridView1_SelectedIndexChanged"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:GridView&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style="color: blue;"&gt;&lt;/span&gt;
&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:DetailsView &lt;/span&gt;&lt;span style="color: red;"&gt;ID&lt;/span&gt;&lt;span style="color: blue;"&gt;="DetailsView1"&lt;/span&gt;&lt;span style="color: red;"&gt; AutoGenerateEditButton&lt;/span&gt;&lt;span style="color: blue;"&gt;="true"&lt;/span&gt;&lt;span style="color: red;"&gt;
runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&lt;/span&gt;&lt;span style="color: red;"&gt; OnModeChanging&lt;/span&gt;&lt;span style="color: blue;"&gt;="DetailsView1_ModeChanging"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:DetailsView&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;ContentTemplate&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:updatepanel&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;
As you can note from the code, this is a simple GridView, which enables a DetailsView when a row in the GridView is selected. We then have an Edit button on the DetailsView that should send the DetailsView in edit mode when clicked. All nice so far. Now, this is going to work as advertised ofcourse, all postback is done silently in the background.&lt;br /&gt;
&lt;br /&gt;
But if you look closely enough, both the gridview and the DetailsView are contained within a single UpdatePanel, so obviously the postback caused from any child control nested in the updatepanel will cause the entire contents of the updatepanel to refresh and send back the collective rendered content to the client. &lt;br /&gt;
&lt;br /&gt;
Below screenshots is the traffic analysed through firebug (a firefox extention). Hilighted data denotes the extra data we do not need rendered to the client.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-_b-WyrIAuEI/Talliq79WII/AAAAAAAAAH4/Qucn9rGNc0M/s1600/UpdatePanelBloat1.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="224" src="http://3.bp.blogspot.com/-_b-WyrIAuEI/Talliq79WII/AAAAAAAAAH4/Qucn9rGNc0M/s320/UpdatePanelBloat1.gif" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
As you can note from the screenshots above, my clicking the select button in the gridview, which should be launching the DetailsView in turn, while i'd only need the rendering of the DetailsView send back to me (since the gridview shouldn't need to change), i actually end up with both the rendering of the GridView and the DetailsView. Indeed, there is extra data(the GridView) being rendered which we do not need.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ok, this is indeed a problem. Were doing things wrongly. So, how do we cause only the DetailsView to render back instead ? One might quickly think, let's put each into their own individual UpdatePanels ? :-)&lt;br /&gt;
&lt;br /&gt;
So, let's try that. Nothing to be ashamed of. It was the first solution that came to my mind too :P&lt;br /&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:UpdatePanel &lt;/span&gt;&lt;span style="color: red;"&gt;ID&lt;/span&gt;&lt;span style="color: blue;"&gt;="UpdatePanel1"&lt;/span&gt;&lt;span style="color: red;"&gt; runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;ContentTemplate&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;     &amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:GridView &lt;/span&gt;&lt;span style="color: red;"&gt;ID&lt;/span&gt;&lt;span style="color: blue;"&gt;="GridView1"&lt;/span&gt;&lt;span style="color: red;"&gt; AutoGenerateSelectButton&lt;/span&gt;&lt;span style="color: blue;"&gt;="true"&lt;/span&gt;&lt;span style="color: red;"&gt; runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&lt;/span&gt;&lt;span style="color: red;"&gt; 
OnSelectedIndexChanged&lt;/span&gt;&lt;span style="color: blue;"&gt;="GridView1_SelectedIndexChanged"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;     &amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:GridView&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;ContentTemplate&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:UpdatePanel&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;

&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:UpdatePanel &lt;/span&gt;&lt;span style="color: red;"&gt;ID&lt;/span&gt;&lt;span style="color: blue;"&gt;="UpdatePanel2"&lt;/span&gt;&lt;span style="color: red;"&gt; UpdateMode&lt;/span&gt;&lt;span style="color: blue;"&gt;="conditional"&lt;/span&gt;&lt;span style="color: red;"&gt; runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;  &amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Triggers&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;    &amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:AsyncPostBackTrigger &lt;/span&gt;&lt;span style="color: red;"&gt;ControlID&lt;/span&gt;&lt;span style="color: blue;"&gt;="GridView1"&lt;/span&gt;&lt;span style="color: red;"&gt; EventName&lt;/span&gt;&lt;span style="color: blue;"&gt;="RowCommand"&lt;/span&gt;&lt;span style="color: red;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;  &amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;Triggers&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;ContentTemplate&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;

&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:DetailsView &lt;/span&gt;&lt;span style="color: red;"&gt;ID&lt;/span&gt;&lt;span style="color: blue;"&gt;="DetailsView1"&lt;/span&gt;&lt;span style="color: red;"&gt; AutoGenerateEditButton&lt;/span&gt;&lt;span style="color: blue;"&gt;="true"&lt;/span&gt;&lt;span style="color: red;"&gt; runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&lt;/span&gt;&lt;span style="color: red;"&gt; 
OnModeChanging&lt;/span&gt;&lt;span style="color: blue;"&gt;="DetailsView1_ModeChanging"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:DetailsView&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;ContentTemplate&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:UpdatePanel&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;
And here below is the screnshot of the traffic as seen through firebug. Hilighted data denotes the extra data we do not need rendered to the client.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-A5NhD7YADMw/TallhEnnBMI/AAAAAAAAAHs/tXT2wrBYw5M/s1600/UpdatePanelBloat2.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="223" src="http://3.bp.blogspot.com/-A5NhD7YADMw/TallhEnnBMI/AAAAAAAAAHs/tXT2wrBYw5M/s320/UpdatePanelBloat2.gif" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
As you can see, we ended up with the same data as before. Nothing has changed, even though we included them in two separate UpdatePanels. Strange ? Not really. If you think about it, both panels are included in the page, and by default, both panels have UpdateMode="Always" set on them, which&amp;nbsp;causes both to refresh upon an async callback. &lt;br /&gt;
&lt;br /&gt;
azzzz we have a problem indeed. Time to read the documentation :P&lt;br /&gt;
&lt;br /&gt;
The first thing the docs hint about are two things : UpdateMode="Conditional" versus the Default which is "Always" and the second thing is&amp;nbsp; ChildrenAsTriggers="false" ; both of which are handy. &lt;br /&gt;
&lt;br /&gt;
If my postback was being caused only by children in UpdatePanel2, i couldof just set UpdateMode="Conditional" on UpdatePanel1 and i'd actually achieve what i was after. Only UpdatePanel2's content will be send back to the client. However if you will note in my example above, a control in UpdatePanel1 is the one who is triggering the postback. This satisfies the "Conditional" bit and UpdatePanel1 also renders its contents. Again not what i'm after.&lt;br /&gt;
&lt;br /&gt;
In effect, i've had to set both UpdateMode="Conditional" and also set ChildrenAsTriggers="false" on UpdatePanel1. This stopped the unwanted behaviour. ChildrenAsTriggers property has a proper explaination in the documentation, you can look it up. In short, it simply stops any direct children from making it refresh. This is good for us and what we are after. &lt;br /&gt;
&lt;br /&gt;
That's just perfect. Using this combo, i can keep the panel i do not want updated, while letting the panel with the update trigger refresh at will. This also allows me to control who gets updated by calling the update method manually on the panel that interests me. For example if i edit a record in the detailsview and want to show the change in the gridview, i'd run the update operation and then right after that, call update on the panel that contains my gridview manually.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:UpdatePanel &lt;/span&gt;&lt;span style="color: red;"&gt;ID&lt;/span&gt;&lt;span style="color: blue;"&gt;="UpdatePanel1"&lt;/span&gt;&lt;span style="color: red;"&gt; UpdateMode&lt;/span&gt;&lt;span style="color: blue;"&gt;="conditional"&lt;/span&gt;&lt;span style="color: red;"&gt; 
ChildrenAsTriggers&lt;/span&gt;&lt;span style="color: blue;"&gt;="false"&lt;/span&gt;&lt;span style="color: red;"&gt; runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;ContentTemplate&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;   &amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:GridView &lt;/span&gt;&lt;span style="color: red;"&gt;ID&lt;/span&gt;&lt;span style="color: blue;"&gt;="GridView1"&lt;/span&gt;&lt;span style="color: red;"&gt; AutoGenerateSelectButton&lt;/span&gt;&lt;span style="color: blue;"&gt;="true"&lt;/span&gt;&lt;span style="color: red;"&gt; runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&lt;/span&gt;&lt;span style="color: red;"&gt; 
OnSelectedIndexChanged&lt;/span&gt;&lt;span style="color: blue;"&gt;="GridView1_SelectedIndexChanged"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;   &amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:GridView&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;ContentTemplate&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;

&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:UpdatePanel&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:UpdatePanel &lt;/span&gt;&lt;span style="color: red;"&gt;ID&lt;/span&gt;&lt;span style="color: blue;"&gt;="UpdatePanel2"&lt;/span&gt;&lt;span style="color: red;"&gt; runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;   &amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;Triggers&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;      &amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:AsyncPostBackTrigger &lt;/span&gt;&lt;span style="color: red;"&gt;ControlID&lt;/span&gt;&lt;span style="color: blue;"&gt;="GridView1"&lt;/span&gt;&lt;span style="color: red;"&gt; EventName&lt;/span&gt;&lt;span style="color: blue;"&gt;="RowCommand"&lt;/span&gt;&lt;span style="color: red;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;   &amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;Triggers&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;ContentTemplate&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style="color: blue;"&gt;&lt;/span&gt;
&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:DetailsView &lt;/span&gt;&lt;span style="color: red;"&gt;ID&lt;/span&gt;&lt;span style="color: blue;"&gt;="DetailsView1"&lt;/span&gt;&lt;span style="color: red;"&gt; AutoGenerateEditButton&lt;/span&gt;&lt;span style="color: blue;"&gt;="true"&lt;/span&gt;&lt;span style="color: red;"&gt; runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&lt;/span&gt;&lt;span style="color: red;"&gt; 
OnModeChanging&lt;/span&gt;&lt;span style="color: blue;"&gt;="DetailsView1_ModeChanging"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:DetailsView&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;ContentTemplate&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:UpdatePanel&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style="color: blue;"&gt;
&lt;/span&gt;&lt;/pre&gt;here are the screenies, you can see, only the detailsview is now present in our callback. Just perfect.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-HGzTg3iMiIw/TallgMKRzzI/AAAAAAAAAHk/rMKGmVoMfbU/s1600/UpdatePanelBloat3.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="229" src="http://2.bp.blogspot.com/-HGzTg3iMiIw/TallgMKRzzI/AAAAAAAAAHk/rMKGmVoMfbU/s320/UpdatePanelBloat3.gif" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
While this is a simplistic example, had you many deeply nested updatepanels you can easily workout who gets updated and whose data gets rendered reducing bloat, using the same method i've mentioned above. Don't simply include EVERYTHING in one updatepanel or multiple and depend on the default, posting back un-necessary bloat on each callback. Firebug for Firefox and Fiddler for IE are both great tools for inspecting and analysing your callback traffic. Use either. I prefer firebug :p&lt;br /&gt;
&lt;br /&gt;
Ok, so that was easy(just set UpdateMode="conditional" ChildrenAsTriggers="false"), nonetheless i ended up with quite a lengthy post :x&lt;img src="http://feeds.feedburner.com/~r/typps/axAH/~4/6pq2Nm2SyC4" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.typps.com/feeds/4946226906523960609/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.typps.com/2008/01/reducing-updatepanel-bloat-by-utilizing.html#comment-form" title="5 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/4946226906523960609?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/4946226906523960609?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typps/axAH/~3/6pq2Nm2SyC4/reducing-updatepanel-bloat-by-utilizing.html" title="Reducing UpdatePanel bloat by utilizing UpdateMode=&amp;quot;Conditional&amp;quot; and ChildrenAsTriggers=&amp;quot;false&amp;quot;" /><author><name>Alessandro</name><uri>http://www.blogger.com/profile/08620937587114860179</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="24" src="http://4.bp.blogspot.com/-9iFBqNPDD_E/TbtHLjeMgZI/AAAAAAAAAK0/9WMh7OUK6KA/s220/alessandro.png" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-_b-WyrIAuEI/Talliq79WII/AAAAAAAAAH4/Qucn9rGNc0M/s72-c/UpdatePanelBloat1.gif" height="72" width="72" /><thr:total>5</thr:total><feedburner:origLink>http://blog.typps.com/2008/01/reducing-updatepanel-bloat-by-utilizing.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkMGQ306fSp7ImA9WhZRGUs.&quot;"><id>tag:blogger.com,1999:blog-6778277710194170777.post-830726604369844728</id><published>2008-01-04T15:51:00.000+01:00</published><updated>2011-04-16T15:27:02.315+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-04-16T15:27:02.315+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET Webforms" /><title>Failed to load viewstate ? Typical problem, with an obvious solution.</title><content type="html">&amp;nbsp;&amp;nbsp;Understanding viewstate is fundamental in asp.net, especially if you had run into : &lt;br /&gt;&lt;br /&gt;&lt;span style="color: green;"&gt;Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.&lt;/span&gt; &lt;br /&gt;&lt;br /&gt;The only way to resolve is a proper understanding of viewstate.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://geekswithblogs.net/FrostRed/archive/2007/02/17/106547.aspx" mce_href="http://geekswithblogs.net/FrostRed/archive/2007/02/17/106547.aspx"&gt;http://geekswithblogs.net/FrostRed/archive/2007/02/17/106547.aspx&lt;/a&gt;&amp;nbsp;is a interesting post on viewstate that i happen to read today, pointed out to me by someone who ran into a viewstate problem about the control tree not matching and was clearly afraid of adding controls dynamically after reading some facts presented in that article. Who wouldn't ?&lt;br /&gt;&lt;br /&gt;While the post gives us a very good understanding of viewstate and how it can fail, so i encourage you to read it first, might seem lengthy but I assure&amp;nbsp; you, it's quite interesting. However, when you're done, follow my rant here, since I feel it's important to know, that, the failure can only happen&amp;nbsp;when either done&amp;nbsp;deliberately as per the sample code in the post i linked to above or&amp;nbsp;to *not* understanding viewstate and how it works.&lt;br /&gt;&lt;br /&gt;So how can we easily avoid these failures ? Let's look at his first code example,&amp;nbsp;and build onto that :&lt;br /&gt;&lt;pre style="background:white"&gt;&lt;span style="color: blue;"&gt;protected&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; Page_Init(&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt; sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;    if&lt;/span&gt;&lt;span style="color: black;"&gt; (&lt;/span&gt;&lt;span style="color: black;"&gt;!&lt;/span&gt;&lt;span style="color: black;"&gt;IsPostBack)&lt;br /&gt;    {&lt;br /&gt;       Button btnClickMe &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; Button();&lt;br /&gt;       form1.Controls.Add(btnClickMe);&lt;br /&gt;       btnClickMe.Text &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;Click me&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;    else&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;    {&lt;br /&gt;      Label label &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; Label();&lt;br /&gt;      form1.Controls.Add(label);&lt;br /&gt;    }&lt;br /&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;As you can note above, this is problematic, since the control into which viewstate is restored is matched by control index, so when the index changes, as is clear in the above code, because if btnClickMe was loaded in for example index [0], now upon postback, after the page has been recreated and rebuilt, the Label "label" is loaded in index [0] instead and takes the place of the button. So this means viewstate that was meant for the button is loaded into the label instead, and the output in the screen after clicking the button is "click me" which was clearly not provided to the label's text property.&lt;br /&gt;&lt;br /&gt;Now that we understand the problem, how can this sample apply in real world or why would anybody want to do something like this ? Basically in short, why is viewstate being utilized, if it's not needed after postback ? Button btnClickMe is not reloaded after postback, so it's safe to turn off viewstate on this control, and problem is solved.&lt;br /&gt;&lt;br /&gt;This is a typical situation where you deliberately want viewstate to fail, apart from that i see no real use to want to maintain viewstate, which is also bloat on a control that clearly is not utilizing it.&lt;br /&gt;&lt;br /&gt;so a rewrite ? here : &lt;br /&gt;&lt;pre style="background:white"&gt;&lt;span style="color: blue;"&gt;protected&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; Page_Init(&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt; sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;    if&lt;/span&gt;&lt;span style="color: black;"&gt; (&lt;/span&gt;&lt;span style="color: black;"&gt;!&lt;/span&gt;&lt;span style="color: black;"&gt;IsPostBack)&lt;br /&gt;    {&lt;br /&gt;        Button btnClickMe &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; Button();&lt;br /&gt;&lt;/span&gt;&lt;span style="color: green;"&gt;        //&lt;/span&gt;&lt;span style="color: green;"&gt; note the addition of the following line&lt;/span&gt;&lt;span style="color: green;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;        btnClickMe.EnableViewState &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;; &lt;br /&gt;        form1.Controls.Add(btnClickMe);&lt;br /&gt;        btnClickMe.Text &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;Click me&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;    else&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;    {&lt;br /&gt;      Label label &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; Label();&lt;br /&gt;      form1.Controls.Add(label);&lt;br /&gt;    }&lt;br /&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;Otherwise, again as per the sample code above, had we been using viewstate, then the problem would resolve itself, if we recreated the control also after postback, which is one of the basic rules of dynamic controls creation. I say rules but really it's the logical thing to do since the page is destroyed after postback and asp.net will have no recollection of controls you added dynamically since memory is cleared, so it's upto you to build it up again manually.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre style="background:white"&gt;&lt;span style="color: blue;"&gt;protected&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; Page_Init(&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt; sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;&lt;/span&gt;&lt;span style="color: green;"&gt;       //&lt;/span&gt;&lt;span style="color: green;"&gt; button will be created even after postback&lt;/span&gt;&lt;span style="color: green;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;       Button btnClickMe &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; Button();&lt;br /&gt;       btnClickMe.EnableViewState &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;br /&gt;       form1.Controls.Add(btnClickMe);&lt;br /&gt;       btnClickMe.Text &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;Click me&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;       if&lt;/span&gt;&lt;span style="color: black;"&gt; (IsPostBack)&lt;br /&gt;       {&lt;br /&gt;         Label label &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; Label();&lt;br /&gt;         form1.Controls.Add(label);&lt;br /&gt;       }&lt;br /&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;So, bottom line, a proper understanding of viewstate, knowledge of&amp;nbsp;the page life cycle, so you know in what phase it's safe to build your control, which will guarantee that viewstate is reloaded into the control(so you load it prior to page_load), and you got it right.&amp;nbsp;For a proper understanding of the page life cycle, you can read the following&amp;nbsp;document on msdn : &lt;a href="http://msdn2.microsoft.com/en-us/library/ms178472.aspx?wt.slv=ColumnA"&gt;http://msdn2.microsoft.com/en-us/library/ms178472.aspx?wt.slv=ColumnA&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style="color: red;"&gt;Update Jan/04/2008: I forgot to&amp;nbsp;mention a gotcha, so here it is : &amp;nbsp;&lt;/div&gt;&lt;br /&gt;Another gotcha you want to avoid is also the order of controls, that is, when you're loading a dynamic control, make sure the order in which you create it, has the same order when you recreate it. Confused, here let me explain better : &lt;br /&gt;&lt;pre style="background:white"&gt;&lt;span style="color: blue;"&gt;protected&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; Page_Init(&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt; sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;   if&lt;/span&gt;&lt;span style="color: black;"&gt; (IsPostBack)&lt;br /&gt;   {&lt;br /&gt;      Label label &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; Label();&lt;br /&gt;      label.ID &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;label1&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;br /&gt;      form1.Controls.Add(label);&lt;br /&gt;      label.Text &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;label&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;br /&gt;&lt;br /&gt;      Button btnClickMe &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; Button();&lt;br /&gt;      btnClickMe.ID &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;button1&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;br /&gt;      form1.Controls.Add(btnClickMe);&lt;br /&gt;      btnClickMe.Text &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;Click me&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;br /&gt;   }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;   else&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&lt;span style="color: black;"&gt; (&lt;/span&gt;&lt;span style="color: black;"&gt;!&lt;/span&gt;&lt;span style="color: black;"&gt;IsPostBack)&lt;br /&gt;   {&lt;br /&gt;&lt;/span&gt;&lt;span style="color: green;"&gt;      //&lt;/span&gt;&lt;span style="color: green;"&gt;Now lets change the order &lt;br /&gt;&lt;/span&gt;&lt;span style="color: green;"&gt;      //&lt;/span&gt;&lt;span style="color: green;"&gt;during postback and we are&lt;br /&gt;&lt;/span&gt;&lt;span style="color: green;"&gt;      //&lt;/span&gt;&lt;span style="color: green;"&gt;recreating the controls&lt;/span&gt;&lt;span style="color: green;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;      Button btnClickMe &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; Button();&lt;br /&gt;      btnClickMe.ID &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;button1&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;br /&gt;      form1.Controls.Add(btnClickMe);&lt;br /&gt;      btnClickMe.Text &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;Click me&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;br /&gt;&lt;br /&gt;      Label label &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; Label();&lt;br /&gt;      label.ID &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;label1&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;br /&gt;      form1.Controls.Add(label);&lt;br /&gt;      label.Text &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;label&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;br /&gt;   }&lt;br /&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;As you can note above, the order in which controls are added changes after postback. In this scenario what really happens ? The viewstate meant for the button is loaded into the label and the viewstate meant for the label is loaded into the button. So, you really want to be careful with the order in which you recreate your controls.&lt;img src="http://feeds.feedburner.com/~r/typps/axAH/~4/gFZXjzeuFDY" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.typps.com/feeds/830726604369844728/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.typps.com/2008/01/failed-to-load-viewstate-typical.html#comment-form" title="16 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/830726604369844728?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/830726604369844728?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typps/axAH/~3/gFZXjzeuFDY/failed-to-load-viewstate-typical.html" title="Failed to load viewstate ? Typical problem, with an obvious solution." /><author><name>Alessandro</name><uri>http://www.blogger.com/profile/08620937587114860179</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="24" src="http://4.bp.blogspot.com/-9iFBqNPDD_E/TbtHLjeMgZI/AAAAAAAAAK0/9WMh7OUK6KA/s220/alessandro.png" /></author><thr:total>16</thr:total><feedburner:origLink>http://blog.typps.com/2008/01/failed-to-load-viewstate-typical.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DUAESHwyeyp7ImA9WhZXEUs.&quot;"><id>tag:blogger.com,1999:blog-6778277710194170777.post-7288217141328557693</id><published>2008-01-01T03:50:00.001+01:00</published><updated>2011-04-30T14:48:29.293+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-04-30T14:48:29.293+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET Webforms" /><title>UpdatePanel Css StyleSheet upon partial-refresh bug in IE</title><content type="html">&lt;div&gt;The update panel seems to have a bug when registering an external stylesheet or including css styles from within the contents that will be getting partially rendered. The bug only seems to occur in IE, works nicely in firefox. Impressive indeed. My problems started when i had a control that needed to render a link to an external stylesheet, which was quite mm easy and normal.&lt;br /&gt;
&lt;br /&gt;
I mean i've been there and done that plenty of times, however this time there were situations in which the stylesheet needed to be registered if my control was included in an updatepanel and kept invisible during inital load, while enabling it only upon a partial postback. TRICKY TRICKY TRICKY!&lt;br /&gt;
&lt;br /&gt;
More over, there is an old bug opened and closed&amp;nbsp;with a&amp;nbsp;reason "this is by design". Seems awkward to me that this is by design and only effects IE :-(&lt;br /&gt;
The url to the bug report is here : &lt;/div&gt;&lt;br /&gt;
&lt;div&gt;&lt;a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=103134" mce_href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=103134"&gt;https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=103134&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div&gt;I resolved by registering the css in the OnInit phase of my custom control. Since this would run and register the css even if the control was disabled or invisible, which is what i was after, since it registered the control with the page on first load instead of trying to rendering the style link as part of my rendering for the control(which obviously didn't work in IE). A simplied piece of my code of how i have worked around this problem is as follows : &lt;/div&gt;&lt;pre&gt;&lt;span style="color: blue;"&gt;protected&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;override&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; OnInit(EventArgs e)
{
&lt;/span&gt;&lt;span style="color: blue;"&gt;    base&lt;/span&gt;&lt;span style="color: black;"&gt;.OnInit(e);
ScriptManager sm &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; ScriptManager.GetCurrent(Page);
&lt;/span&gt;&lt;span style="color: blue;"&gt;    if&lt;/span&gt;&lt;span style="color: black;"&gt; (&lt;/span&gt;&lt;span style="color: black;"&gt;!&lt;/span&gt;&lt;span style="color: black;"&gt;sm.IsInAsyncPostBack)
{
&lt;/span&gt;&lt;span style="color: blue;"&gt;       string&lt;/span&gt;&lt;span style="color: black;"&gt; css &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;.Format(&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;&amp;lt;link rel=\"stylesheet\" &lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: black;"&gt;        href&lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt;\&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;{0}\" type=\"text/css\" /&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;, 
ResolveUrl(CssClassFile));

ScriptManager.RegisterClientScriptBlock(&lt;/span&gt;&lt;span style="color: blue;"&gt;this&lt;/span&gt;&lt;span style="color: black;"&gt;, 
&lt;/span&gt;&lt;span style="color: blue;"&gt;      typeof&lt;/span&gt;&lt;span style="color: black;"&gt;(MyBlahControl), &lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;MyBlahId&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;, css, &lt;/span&gt;&lt;span style="color: blue;"&gt;false&lt;/span&gt;&lt;span style="color: black;"&gt;);
}
}&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;span style="color: red;"&gt;Update 01/01/2008 :&lt;/span&gt; Please read the first two comments below.&amp;nbsp;CSS contianment from within the &amp;lt;body element violates xhtml specs and as such here is an update that includes the css in the &amp;lt;head section. Thanks to &lt;b&gt;Ram Krisna&lt;/b&gt; for pointing out/commenting this.&lt;br /&gt;
&lt;pre&gt;&lt;span style="color: blue;"&gt;protected&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;override&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; OnInit(EventArgs e)
{
&lt;/span&gt;&lt;span style="color: blue;"&gt;  base&lt;/span&gt;&lt;span style="color: black;"&gt;.OnInit(e);
ScriptManager sm &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; ScriptManager.GetCurrent(Page);
&lt;/span&gt;&lt;span style="color: blue;"&gt;  if&lt;/span&gt;&lt;span style="color: black;"&gt; (&lt;/span&gt;&lt;span style="color: black;"&gt;!&lt;/span&gt;&lt;span style="color: black;"&gt;sm.IsInAsyncPostBack)
{
HtmlLink l &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;span style="color: black;"&gt; HtmlLink();
l.Href &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; ResolveUrl(CssClassFile);
l.Attributes.Add(&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;rel&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;stylesheet&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;);
l.Attributes.Add(&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;type&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;text/css&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;);
Page.Header.Controls.Add(l);
}
}&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;
A simplified test of what I feel is an open bug and should be fixed can be seen below. After clicking the button, the style applied to the label is lost and happens only in IE7, donno about previous versions since i have not tested : &lt;br /&gt;
&lt;pre&gt;&lt;span style="background-color: yellow; color: black;"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt;@ Page Language&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt;=&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt;"&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt;C#&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt;"&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt; &lt;/span&gt;&lt;span style="background-color: yellow; color: black;"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;

&lt;/span&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;runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt;

protected &lt;/span&gt;&lt;span style="background-color: whitesmoke; color: blue;"&gt;void&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt; Button1_Click(object sender, EventArgs e)
{
&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: green;"&gt;//&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: green;"&gt; do something&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: green;"&gt;
&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt;}

&lt;/span&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: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;

&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;!&lt;/span&gt;&lt;span style="color: magenta;"&gt;DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 
Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;

&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;html &lt;/span&gt;&lt;span style="color: red;"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue;"&gt;="http://www.w3.org/1999/xhtml"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;head &lt;/span&gt;&lt;span style="color: red;"&gt;runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;title&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;Untitled Page&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;title&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;head&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;body&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;form &lt;/span&gt;&lt;span style="color: red;"&gt;id&lt;/span&gt;&lt;span style="color: blue;"&gt;="form1"&lt;/span&gt;&lt;span style="color: red;"&gt; runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&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: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:ScriptManager &lt;/span&gt;&lt;span style="color: red;"&gt;ID&lt;/span&gt;&lt;span style="color: blue;"&gt;="ScriptManager1"&lt;/span&gt;&lt;span style="color: red;"&gt; runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:ScriptManager&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:UpdatePanel &lt;/span&gt;&lt;span style="color: red;"&gt;ID&lt;/span&gt;&lt;span style="color: blue;"&gt;="UpdatePanel1"&lt;/span&gt;&lt;span style="color: red;"&gt; runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;ContentTemplate&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: green;"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color: green;"&gt; 
Note below that for simplicity i am not
using an external stylesheet. Anyway, even with an external style
sheet the result is the same. The style is 
not applied after partial postback 
&lt;/span&gt;&lt;span style="color: green;"&gt;--&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;style &lt;/span&gt;&lt;span style="color: red;"&gt;type&lt;/span&gt;&lt;span style="color: blue;"&gt;="text/css"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: maroon;"&gt;
.MakeGreen&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt;{&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: red;"&gt;background-color&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt;:&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: blue;"&gt;green&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt;;&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt;}&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: maroon;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;style&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:Label &lt;/span&gt;&lt;span style="color: red;"&gt;ID&lt;/span&gt;&lt;span style="color: blue;"&gt;="Label1"&lt;/span&gt;&lt;span style="color: red;"&gt; CssClass&lt;/span&gt;&lt;span style="color: blue;"&gt;="MakeGreen"&lt;/span&gt;&lt;span style="color: red;"&gt; 
runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&lt;/span&gt;&lt;span style="color: red;"&gt; Text&lt;/span&gt;&lt;span style="color: blue;"&gt;="Label"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:Label&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;

&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:Button &lt;/span&gt;&lt;span style="color: red;"&gt;ID&lt;/span&gt;&lt;span style="color: blue;"&gt;="Button1"&lt;/span&gt;&lt;span style="color: red;"&gt; runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&lt;/span&gt;&lt;span style="color: red;"&gt; Text&lt;/span&gt;&lt;span style="color: blue;"&gt;="Partial refresh"&lt;/span&gt;&lt;span style="color: red;"&gt;
OnClick&lt;/span&gt;&lt;span style="color: blue;"&gt;="Button1_Click"&lt;/span&gt;&lt;span style="color: red;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;

&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;ContentTemplate&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:UpdatePanel&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&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: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;form&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;body&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;html&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;
&lt;/span&gt;&lt;/pre&gt;&lt;img src="http://feeds.feedburner.com/~r/typps/axAH/~4/UtPxvkfP1c0" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.typps.com/feeds/7288217141328557693/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.typps.com/2007/12/updatepanel-css-stylesheet-upon-partial.html#comment-form" title="6 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/7288217141328557693?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/7288217141328557693?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typps/axAH/~3/UtPxvkfP1c0/updatepanel-css-stylesheet-upon-partial.html" title="UpdatePanel Css StyleSheet upon partial-refresh bug in IE" /><author><name>Alessandro</name><uri>http://www.blogger.com/profile/08620937587114860179</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="24" src="http://4.bp.blogspot.com/-9iFBqNPDD_E/TbtHLjeMgZI/AAAAAAAAAK0/9WMh7OUK6KA/s220/alessandro.png" /></author><thr:total>6</thr:total><feedburner:origLink>http://blog.typps.com/2007/12/updatepanel-css-stylesheet-upon-partial.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkMGQ305eip7ImA9WhZRGUs.&quot;"><id>tag:blogger.com,1999:blog-6778277710194170777.post-1947691861809689744</id><published>2007-10-09T17:29:00.000+02:00</published><updated>2011-04-16T15:27:02.322+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-04-16T15:27:02.322+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET Webforms" /><title>Custom Serverside paging in GridView Vs DataGrid</title><content type="html">&amp;nbsp;When doing serverside paging(that is paging at the database layer by returning only the paged result), one of the things I miss in the GridView control is the VirtualItemCount, which is supposedly only supported in the&amp;nbsp;older control's like the DataGrid. &lt;br /&gt;&lt;br /&gt;This property was quite useful because while being able to supply to the DataGrid a variable number of paged result sets, i was also able to tell the DataGrid, the total number of records, that way it knew how many pager buttons to display. &lt;br /&gt;&lt;br /&gt;Eg. If we had, say a total of a 100 records and had the pageSize set to 7, so only 7 records are shown at a time, then how does the grid know how many numbered pager buttons to display allowing us to navigate from one page to another ? That's where the VirtualItemCount came into play and saved the day. To this property we'd pass a total records count and that was it. In the GridView today ? There is no VirtualItemCount present. The way it were planned it seems is to use the ObjectDataSource, which in my honest opinion is simply extra work, however it does abstract much of this code nicely and put it where it should be, in the data tier.&lt;br /&gt;&lt;br /&gt;In all the example code in this post, I shall be using the MemberShip.GetAllUsers method. This method is overloaded and can retrieve a paged result of users Versus returning all the user's in the database, which is a quite handy overload and works out nicely for the code i want to use in this post.&lt;br /&gt;&lt;br /&gt;Let's look at a simple example of how we couldof performed custom paging on the DataGrid control back in the old days : &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre style="background: white;"&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; virtualItemCount &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: black;"&gt;0&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;protected&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; Page_Load(&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt; sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&lt;span style="color: black;"&gt; (&lt;/span&gt;&lt;span style="color: black;"&gt;!&lt;/span&gt;&lt;span style="color: black;"&gt;IsPostBack)&lt;br /&gt;{&lt;br /&gt;DataGridMembers.DataSource &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; Membership.GetAllUsers(&lt;/span&gt;&lt;span style="color: black;"&gt;0&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;br /&gt;DataGridMembers.PageSize, &lt;/span&gt;&lt;span style="color: blue;"&gt;out&lt;/span&gt;&lt;span style="color: black;"&gt; virtualItemCount);&lt;br /&gt;DataGridMembers.VirtualItemCount &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; virtualItemCount;&lt;br /&gt;DataGridMembers.DataBind();&lt;br /&gt;}&lt;br /&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;pre style="background: white;"&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;Note the VirtualItemCount ? Then on the PageIndexChanged event of the DataGrid we did :&lt;br /&gt;&lt;br /&gt;&lt;pre style="background: white;"&gt;&lt;span style="color: blue;"&gt;protected&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; DataGridMembers_PageIndexChanged(&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt; source, &lt;br /&gt;DataGridPageChangedEventArgs e)&lt;br /&gt;{&lt;br /&gt;DataGridMembers.CurrentPageIndex &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; e.NewPageIndex;&lt;br /&gt;DataGridMembers.DataSource &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; Membership.GetAllUsers(e.NewPageIndex, &lt;br /&gt;DataGridMembers.PageSize, &lt;/span&gt;&lt;span style="color: blue;"&gt;out&lt;/span&gt;&lt;span style="color: black;"&gt; virtualItemCount);&lt;br /&gt;DataGridMembers.DataBind();&lt;br /&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;pre style="background: white;"&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;And that was it, it was as simple as that.&lt;br /&gt;&lt;br /&gt;Now try to do that on the GridView ? Can't be done and this is a control that is replacing the old DataGrid control. To make things worse, the DataGrid is not a supported control anymore in 2.0 ; It's been obsoleted and by default you wont even find this control in your toolbox. You can still use it however by manually adding it to your toolbox. Unfortunate, because there are moments like this custom paging situation and i'm getting nostalgic already.&lt;br /&gt;&lt;br /&gt;So, how to achieve the same thing in the GridView control which happens to replace the DataGrid ? Well, it's a long shot. Since we cannot achive this directly on the GridView, we are going to have to do it via the DataSource control, which is actually the control that is populating the data for the gridview and also the control that handles paging and sorting amoung other things. While i like this kind of data abstraction, i'm actually doing more work and making the extra effort,but this is how you would implement custom paging on&amp;nbsp; your GridView control.&lt;br /&gt;The GridView alone is lacking a VirtualItemCount property, which i believe shouldn't have been so hard to implement. To compensate for this lacking, you perform custom serverside paging by using an ObjectDataSource control, defining a SelectMethod and a SelectCountMethod method. The SelectCountMethod is your custom method that returns the Total records count. &lt;br /&gt;&lt;br /&gt;So let's look at some code, and there are few gotcha's that weren't exactly obvious to me in the begining : &lt;br /&gt;First our custom SelectMethod :&lt;br /&gt;&lt;br /&gt;&lt;pre style="background: white;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; MembershipUserCollection GetAllUsers(&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; startRowIndex, &lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; maximumRows)&lt;br /&gt;{&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&lt;span style="color: black;"&gt; (startRowIndex &lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: black;"&gt;0&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;br /&gt;startRowIndex &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; startRowIndex &lt;/span&gt;&lt;span style="color: black;"&gt;/&lt;/span&gt;&lt;span style="color: black;"&gt; maximumRows;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&lt;span style="color: black;"&gt; Membership.GetAllUsers(startRowIndex, &lt;br /&gt;maximumRows, &lt;/span&gt;&lt;span style="color: blue;"&gt;out&lt;/span&gt;&lt;span style="color: black;"&gt; selectCountValue);&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="background: white;"&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;One gotcha you want to make note of is how i have some extra code to divide startRowIndex by maximumRows ; This is because startRowIndex is actually the first row in the resultset as the variable name indicates, however what i really need is the current page index, because that is what our stored procedure is expecting, in this case that is what the internal MemberShip.GetAllUsers method is expecting.&lt;br /&gt;&lt;br /&gt;Next we need to add a SelectCountMethod : &lt;br /&gt;&lt;br /&gt;&lt;pre style="background: white;"&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; selectCountValue &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: black;"&gt;0&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; SelectVirtualCount()&lt;br /&gt;{&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&lt;span style="color: black;"&gt; selectCountValue;&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="background: white;"&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;The code is minimum as you can note, but ofcourse, there is some extra effort to making the abstraction. The code above goes into the data layer.&lt;br /&gt;&lt;br /&gt;And lastly, we need to subscribe to PageIndexChanging event of our GridView and pass the selected page index : &lt;br /&gt;&lt;br /&gt;&lt;pre style="background: white;"&gt;&lt;span style="color: blue;"&gt;protected&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; GridViewMembers_PageIndexChanging(&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt; sender, &lt;br /&gt;GridViewPageEventArgs e)&lt;br /&gt;{&lt;br /&gt;GridViewMembers.PageIndex &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; e.NewPageIndex;&lt;br /&gt;} &lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="background: white;"&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;A peculiar behaviour you will notice is that the SelectCountMethod and the SelectMethod both share the same SelectParameters if SelectParameters are defined. Peculiar because i was not really expecting it, however I have no issues with it, For example if we had to rewrite our previous example to include also a search by userName, then our ObjectDataSource would be expecting some SelectParameters like this :&lt;br /&gt;&lt;br /&gt;&lt;pre style="background: white;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:ObjectDataSource &lt;/span&gt;&lt;span style="color: red;"&gt;ID&lt;/span&gt;&lt;span style="color: blue;"&gt;="ObjectDataSourceMembers"&lt;/span&gt;&lt;span style="color: red;"&gt;&lt;br /&gt;EnablePaging&lt;/span&gt;&lt;span style="color: blue;"&gt;="True"&lt;/span&gt;&lt;span style="color: red;"&gt;&lt;br /&gt;SelectCountMethod&lt;/span&gt;&lt;span style="color: blue;"&gt;="SelectVirtualCount"&lt;/span&gt;&lt;span style="color: red;"&gt;&lt;br /&gt;SelectMethod&lt;/span&gt;&lt;span style="color: blue;"&gt;="GetAllUsers"&lt;/span&gt;&lt;span style="color: red;"&gt;&lt;br /&gt;TypeName&lt;/span&gt;&lt;span style="color: blue;"&gt;="MembersData"&lt;/span&gt;&lt;span style="color: red;"&gt; &lt;br /&gt;runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;SelectParameters&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:ControlParameter &lt;/span&gt;&lt;span style="color: red;"&gt;ControlID&lt;/span&gt;&lt;span style="color: blue;"&gt;="TextBoxUserName"&lt;/span&gt;&lt;span style="color: red;"&gt; Name&lt;/span&gt;&lt;span style="color: blue;"&gt;="userName"&lt;/span&gt;&lt;span style="color: red;"&gt; &lt;br /&gt;PropertyName&lt;/span&gt;&lt;span style="color: blue;"&gt;="Text"&lt;/span&gt;&lt;span style="color: red;"&gt; DefaultValue&lt;/span&gt;&lt;span style="color: blue;"&gt;="All"&lt;/span&gt;&lt;span style="color: red;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;SelectParameters&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:ObjectDataSource&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;And then modified our SelectMethod as such : &lt;br /&gt;&lt;br /&gt;&lt;pre style="background: white;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; MembershipUserCollection GetAllUsers(&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; startRowIndex, &lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; maximumRows, &lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt; userName)&lt;br /&gt;{&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&lt;span style="color: black;"&gt; (startRowIndex &lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: black;"&gt;0&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;br /&gt;startRowIndex &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; startRowIndex &lt;/span&gt;&lt;span style="color: black;"&gt;/&lt;/span&gt;&lt;span style="color: black;"&gt; maximumRows;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&lt;span style="color: black;"&gt; (userName &lt;/span&gt;&lt;span style="color: black;"&gt;==&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;all&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;br /&gt;{&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&lt;span style="color: black;"&gt; Membership.GetAllUsers(startRowIndex,&lt;br /&gt;maximumRows, &lt;/span&gt;&lt;span style="color: blue;"&gt;out&lt;/span&gt;&lt;span style="color: black;"&gt; selectCountValue);&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;else&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;{&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&lt;span style="color: black;"&gt; (MembershipUserCollection)&lt;br /&gt;Membership.FindUsersByName(userName &lt;/span&gt;&lt;span style="color: black;"&gt;+&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;%&lt;/span&gt;&lt;span style="color: black;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;br /&gt;startRowIndex, maximumRows, &lt;/span&gt;&lt;span style="color: blue;"&gt;out&lt;/span&gt;&lt;span style="color: black;"&gt; selectCountValue);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="background: white;"&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;As you can see while our GetAllUsers(SelectMethod) has the needed userName parameter, our SelectVirtualCount method defined above does not have any parameters defined on it, since we don't need to pass it anything. However, the ObjectDataSource is going to complain with : &lt;br /&gt;&lt;br /&gt;ObjectDataSource 'ObjectDataSourceMembers' could not find a non-generic method 'SelectVirtualCount' that has parameters: userName&lt;br /&gt;&lt;br /&gt;So it means both the SelectMethod and the SelectCountMethod share the same Select parameters. I resolved by adding the extra userName parameter in the SelectCountMethod as well, while i did not clearly need it, but no big deal.&lt;br /&gt;Here is what the modified SelectCountMethod wouldof looked like :&lt;br /&gt;&lt;br /&gt;&lt;pre style="background: white;"&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; selectCountValue &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: black;"&gt;0&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; SelectVirtualCount(&lt;/span&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt; userName)&lt;br /&gt;{&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&lt;span style="color: black;"&gt; selectCountValue;&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="background: white;"&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;The SelectCountMethod is treated exactly in the same way the SelectMethod is treated, so the ObjectDataSource's Selected Event is going to fire twice for example, once when the SelectMethod is called and once when the SelectCountMethod is called. These are all gotchas i was not really prepared for. &lt;br /&gt;&lt;br /&gt;Full code for custom serverside paging in DataGrid :&lt;br /&gt;&lt;br /&gt;&lt;pre style="background: white;"&gt;&lt;span style="background-color: yellow; color: black;"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt;@ Page Language&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt;=&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt;"&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt;C#&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt;"&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt; &lt;/span&gt;&lt;span style="background-color: yellow; color: black;"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;!&lt;/span&gt;&lt;span style="color: magenta;"&gt;DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &lt;br /&gt;"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&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;runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="background: white;"&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; virtualItemCount &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: black;"&gt;0&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;protected&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; Page_Load(&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt; sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&lt;span style="color: black;"&gt; (&lt;/span&gt;&lt;span style="color: black;"&gt;!&lt;/span&gt;&lt;span style="color: black;"&gt;IsPostBack)&lt;br /&gt;{&lt;br /&gt;DataGridMembers.DataSource &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; Membership.GetAllUsers(&lt;/span&gt;&lt;span style="color: black;"&gt;0&lt;/span&gt;&lt;span style="color: black;"&gt;, &lt;br /&gt;DataGridMembers.PageSize, &lt;/span&gt;&lt;span style="color: blue;"&gt;out&lt;/span&gt;&lt;span style="color: black;"&gt; virtualItemCount);&lt;br /&gt;DataGridMembers.VirtualItemCount &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; virtualItemCount;&lt;br /&gt;DataGridMembers.DataBind();&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;protected&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; DataGridMembers_PageIndexChanged(&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt; source, &lt;br /&gt;DataGridPageChangedEventArgs e)&lt;br /&gt;{&lt;br /&gt;DataGridMembers.CurrentPageIndex &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; e.NewPageIndex;&lt;br /&gt;DataGridMembers.DataSource &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; Membership.GetAllUsers(e.NewPageIndex, &lt;br /&gt;DataGridMembers.PageSize, &lt;/span&gt;&lt;span style="color: blue;"&gt;out&lt;/span&gt;&lt;span style="color: black;"&gt; virtualItemCount);&lt;br /&gt;DataGridMembers.DataBind();&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="background: white;"&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: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;html &lt;/span&gt;&lt;span style="color: red;"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue;"&gt;="http://www.w3.org/1999/xhtml"&lt;/span&gt;&lt;span style="color: red;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;head &lt;/span&gt;&lt;span style="color: red;"&gt;runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;title&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;Untitled Page&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;title&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;head&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;body&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;form &lt;/span&gt;&lt;span style="color: red;"&gt;id&lt;/span&gt;&lt;span style="color: blue;"&gt;="form1"&lt;/span&gt;&lt;span style="color: red;"&gt; runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&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: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:DataGrid &lt;/span&gt;&lt;span style="color: red;"&gt;ID&lt;/span&gt;&lt;span style="color: blue;"&gt;="DataGridMembers"&lt;/span&gt;&lt;span style="color: red;"&gt; AllowPaging&lt;/span&gt;&lt;span style="color: blue;"&gt;="True"&lt;/span&gt;&lt;span style="color: red;"&gt; &lt;br /&gt;AllowCustomPaging&lt;/span&gt;&lt;span style="color: blue;"&gt;="true"&lt;/span&gt;&lt;span style="color: red;"&gt; PageSize&lt;/span&gt;&lt;span style="color: blue;"&gt;="2"&lt;/span&gt;&lt;span style="color: red;"&gt; runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&lt;/span&gt;&lt;span style="color: red;"&gt; &lt;br /&gt;OnPageIndexChanged&lt;/span&gt;&lt;span style="color: blue;"&gt;="DataGridMembers_PageIndexChanged"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;PagerStyle &lt;/span&gt;&lt;span style="color: red;"&gt;Mode&lt;/span&gt;&lt;span style="color: blue;"&gt;="NumericPages"&lt;/span&gt;&lt;span style="color: red;"&gt; &lt;br /&gt;HorizontalAlign&lt;/span&gt;&lt;span style="color: blue;"&gt;="Right"&lt;/span&gt;&lt;span style="color: red;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:DataGrid&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&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: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;form&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;body&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;html&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="background: white;"&gt;&lt;span style="color: blue;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;And the full code for custom paging in GridView : &lt;br /&gt;&lt;br /&gt;&lt;pre style="background: white;"&gt;&lt;span style="background-color: yellow; color: black;"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt;@ Page Language&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt;=&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt;"&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt;C#&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt;"&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt; &lt;/span&gt;&lt;span style="background-color: yellow; color: black;"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="background-color: yellow; color: black;"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt;@ Import Namespace&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt;=&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt;"&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt;System.Data&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt;"&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt; &lt;/span&gt;&lt;span style="background-color: yellow; color: black;"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="background-color: yellow; color: black;"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt;@ Import Namespace&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt;=&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt;"&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt;System.Data.SqlClient&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt;"&lt;/span&gt;&lt;span style="background-color: whitesmoke; color: black;"&gt; &lt;/span&gt;&lt;span style="background-color: yellow; color: black;"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;!&lt;/span&gt;&lt;span style="color: magenta;"&gt;DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &lt;br /&gt;"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&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;runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="background: white;"&gt;&lt;span style="color: blue;"&gt;protected&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style="color: black;"&gt; GridViewMembers_PageIndexChanging(&lt;/span&gt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&lt;span style="color: black;"&gt; sender, &lt;br /&gt;GridViewPageEventArgs e)&lt;br /&gt;{&lt;br /&gt;GridViewMembers.PageIndex &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; e.NewPageIndex;&lt;br /&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;pre style="background: white;"&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: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;html &lt;/span&gt;&lt;span style="color: red;"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue;"&gt;="http://www.w3.org/1999/xhtml"&lt;/span&gt;&lt;span style="color: red;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;head &lt;/span&gt;&lt;span style="color: red;"&gt;runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;title&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;Untitled Page&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;title&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;head&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;body&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;form &lt;/span&gt;&lt;span style="color: red;"&gt;id&lt;/span&gt;&lt;span style="color: blue;"&gt;="form1"&lt;/span&gt;&lt;span style="color: red;"&gt; runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&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: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:GridView &lt;/span&gt;&lt;span style="color: red;"&gt;ID&lt;/span&gt;&lt;span style="color: blue;"&gt;="GridViewMembers"&lt;/span&gt;&lt;span style="color: red;"&gt; DataSourceID&lt;/span&gt;&lt;span style="color: blue;"&gt;="ObjectDataSourceMembers"&lt;/span&gt;&lt;span style="color: red;"&gt; &lt;br /&gt;runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&lt;/span&gt;&lt;span style="color: red;"&gt; AllowPaging&lt;/span&gt;&lt;span style="color: blue;"&gt;="True"&lt;/span&gt;&lt;span style="color: red;"&gt; PageSize&lt;/span&gt;&lt;span style="color: blue;"&gt;="2"&lt;/span&gt;&lt;span style="color: red;"&gt; &lt;br /&gt;OnPageIndexChanging&lt;/span&gt;&lt;span style="color: blue;"&gt;="GridViewMembers_PageIndexChanging"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:GridView&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:ObjectDataSource &lt;/span&gt;&lt;span style="color: red;"&gt;ID&lt;/span&gt;&lt;span style="color: blue;"&gt;="ObjectDataSourceMembers"&lt;/span&gt;&lt;span style="color: red;"&gt;&lt;br /&gt;EnablePaging&lt;/span&gt;&lt;span style="color: blue;"&gt;="True"&lt;/span&gt;&lt;span style="color: red;"&gt;&lt;br /&gt;SelectCountMethod&lt;/span&gt;&lt;span style="color: blue;"&gt;="SelectVirtualCount"&lt;/span&gt;&lt;span style="color: red;"&gt;&lt;br /&gt;SelectMethod&lt;/span&gt;&lt;span style="color: blue;"&gt;="GetAllUsers"&lt;/span&gt;&lt;span style="color: red;"&gt;&lt;br /&gt;TypeName&lt;/span&gt;&lt;span style="color: blue;"&gt;="MembersData"&lt;/span&gt;&lt;span style="color: red;"&gt; &lt;br /&gt;runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp:ObjectDataSource&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&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: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;form&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;body&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;html&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="background: white;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;class&lt;/span&gt;&lt;span style="color: black;"&gt; MembersData&lt;br /&gt;{&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; MembersData()&lt;br /&gt;{&lt;br /&gt;&lt;/span&gt;&lt;span style="color: green;"&gt;//&lt;/span&gt;&lt;span style="color: green;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: green;"&gt;//&lt;/span&gt;&lt;span style="color: green;"&gt; TODO: Add constructor logic here&lt;br /&gt;&lt;/span&gt;&lt;span style="color: green;"&gt;//&lt;br /&gt;&lt;/span&gt;&lt;span style="color: black;"&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; selectCountValue &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: black;"&gt;0&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; SelectVirtualCount()&lt;br /&gt;{&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&lt;span style="color: black;"&gt; selectCountValue;&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style="color: black;"&gt; MembershipUserCollection GetAllUsers(&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; startRowIndex, &lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt; maximumRows)&lt;br /&gt;{&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&lt;span style="color: black;"&gt; (startRowIndex &lt;/span&gt;&lt;span style="color: black;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: black;"&gt;0&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;br /&gt;startRowIndex &lt;/span&gt;&lt;span style="color: black;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; startRowIndex &lt;/span&gt;&lt;span style="color: black;"&gt;/&lt;/span&gt;&lt;span style="color: black;"&gt; maximumRows;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&lt;span style="color: black;"&gt; Membership.GetAllUsers(startRowIndex, &lt;br /&gt;maximumRows, &lt;/span&gt;&lt;span style="color: blue;"&gt;out&lt;/span&gt;&lt;span style="color: black;"&gt; selectCountValue);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;img src="http://feeds.feedburner.com/~r/typps/axAH/~4/vSnrkkpo2NM" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.typps.com/feeds/1947691861809689744/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.typps.com/2007/10/custom-serverside-paging-in-gridview-vs.html#comment-form" title="12 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/1947691861809689744?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/6778277710194170777/posts/default/1947691861809689744?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/typps/axAH/~3/vSnrkkpo2NM/custom-serverside-paging-in-gridview-vs.html" title="Custom Serverside paging in GridView Vs DataGrid" /><author><name>Alessandro</name><uri>http://www.blogger.com/profile/08620937587114860179</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="24" src="http://4.bp.blogspot.com/-9iFBqNPDD_E/TbtHLjeMgZI/AAAAAAAAAK0/9WMh7OUK6KA/s220/alessandro.png" /></author><thr:total>12</thr:total><feedburner:origLink>http://blog.typps.com/2007/10/custom-serverside-paging-in-gridview-vs.html</feedburner:origLink></entry></feed>
