<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title> Unhandled Exception</title><link>http://weblogs.asp.net/davidfowler/default.aspx</link><description>using( A technical blog about asp.net and other geeky things. ) {
}</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>More Data Binding</title><link>http://weblogs.asp.net/davidfowler/archive/2009/11/14/more-data-binding.aspx</link><pubDate>Sun, 15 Nov 2009 07:54:56 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7255230</guid><dc:creator>davidfowl</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/davidfowler/rsscomments.aspx?PostID=7255230</wfw:commentRss><comments>http://weblogs.asp.net/davidfowler/archive/2009/11/14/more-data-binding.aspx#comments</comments><description>&lt;p&gt;Last time I tried to solve one of the deficiencies of data binding by taking advantage of expando attributes. Today I want to throw an idea out there that I’ve been playing with since that blog post.I was looking at WPF’s data binding and wondered what it would take to have data binding in ASP.NET be as first class as data binding in WPF.&lt;/p&gt;  &lt;p&gt;There is no imperative (unless you mimic the generated code) way to setup 2 way data binding in ASP.NET, but what if there was? Maybe we should be able to setup bindings in the markup as well as code.&lt;/p&gt;  &lt;h2&gt;SetBinding&lt;/h2&gt;  &lt;p&gt;This hypothetical re-design of ASP.NET data binding includes a SetBinding method on the base control class that allows users to specify bindings imperatively.&lt;/p&gt;  &lt;div style="padding-bottom: 5px; padding-left: 5px; padding-right: 5px; font-family: consolas; background: #3f3f3f; color: white; font-size: 10pt; padding-top: 5px"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: #cda869"&gt;public&lt;/span&gt; &lt;span style="color: #cda869"&gt;partial&lt;/span&gt; &lt;span style="color: #cda869"&gt;class&lt;/span&gt; &lt;span style="color: #9b703f"&gt;_Default&lt;/span&gt; : System.Web.UI.&lt;span style="color: #9b703f"&gt;Page&lt;/span&gt; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #cda869"&gt;protected&lt;/span&gt; &lt;span style="color: #cda869"&gt;void&lt;/span&gt; Page_Init() {&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; textBox.SetBinding(&lt;span style="color: #8f9d6a"&gt;&amp;quot;Text&amp;quot;&lt;/span&gt;, &lt;span style="color: #cda869"&gt;new&lt;/span&gt; &lt;span style="color: #9b703f"&gt;DataBinding&lt;/span&gt;(&lt;span style="color: #8f9d6a"&gt;&amp;quot;FirstName&amp;quot;&lt;/span&gt;));&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;}&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;The above code would setup 2 way binding for the text property to the FirstName property in the current item being databound (if we were using some data control).&lt;/p&gt;  &lt;div style="padding-bottom: 5px; padding-left: 5px; padding-right: 5px; font-family: consolas; background: #3f3f3f; color: white; font-size: 10pt; padding-top: 5px"&gt;   &lt;p style="margin: 0px"&gt;&amp;lt;&lt;span style="color: #9b703f"&gt;asp:TextBox&lt;/span&gt; &lt;span style="color: #9b703f"&gt;runat=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;Text=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;{Binding FirstName}&amp;quot;&lt;/span&gt;&amp;gt;&amp;lt;/&lt;span style="color: #9b703f"&gt;asp:TextBox&lt;/span&gt;&amp;gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;This is the equivalent markup of the code above. &lt;/p&gt;  &lt;p&gt;Unlike the current data binding story that’s all parse time magic, all the work here would be done in the runtime. The binding syntax would just be syntactic sugar.&lt;/p&gt;  &lt;p&gt;So we’ve re-implemented the current binding that ASP.NET supports today but what else can we do. Let’s take this concept of a binding and extend it.&lt;/p&gt;  &lt;p&gt;Sometimes it’s useful to have control’s properties be dependant on each other. Imagine some UI where you have a pager and a drop down list of possible page sizes. Whenever the page size changes you want to update the pager’s page size so that the list updates and shows the correct number of items. This can be done today with a bunch of code but wouldn’t it be nice to have this support natively in the framework?&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;div style="padding-bottom: 5px; padding-left: 5px; padding-right: 5px; font-family: consolas; background: #3f3f3f; color: white; font-size: 10pt; padding-top: 5px"&gt;   &lt;p style="margin: 0px"&gt;&amp;lt;&lt;span style="color: #9b703f"&gt;asp:ListView&lt;/span&gt; &lt;span style="color: #9b703f"&gt;ID=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;ProductsList&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;runat=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;server&amp;quot;&lt;/span&gt;&amp;gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;span style="color: #9b703f"&gt;ItemTemplate&lt;/span&gt;&amp;gt; &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ...&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/&lt;span style="color: #9b703f"&gt;ItemTemplate&lt;/span&gt;&amp;gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;lt;/&lt;span style="color: #9b703f"&gt;asp:ListView&lt;/span&gt;&amp;gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;lt;&lt;span style="color: #9b703f"&gt;asp:DataPager&lt;/span&gt; &lt;span style="color: #9b703f"&gt;runat=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;server&amp;quot;&lt;/span&gt; &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #9b703f"&gt;PagedControlID=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;ProductsList&amp;quot;&lt;/span&gt; &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #9b703f"&gt;PageSize=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;{ControlBinding pageSizes, Property=SelectedValue}&amp;quot;&lt;/span&gt;&amp;gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;lt;/&lt;span style="color: #9b703f"&gt;asp:DataPager&lt;/span&gt;&amp;gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;lt;&lt;span style="color: #9b703f"&gt;asp:DropDownList&lt;/span&gt; &lt;span style="color: #9b703f"&gt;ID=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;pageSizes&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;runat=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;server&amp;quot;&lt;/span&gt;&amp;gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;span style="color: #9b703f"&gt;asp:ListItem&lt;/span&gt; &lt;span style="color: #9b703f"&gt;Text=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;10&amp;quot;&lt;/span&gt;&amp;gt;&amp;lt;/&lt;span style="color: #9b703f"&gt;asp:ListItem&lt;/span&gt;&amp;gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;span style="color: #9b703f"&gt;asp:ListItem&lt;/span&gt; &lt;span style="color: #9b703f"&gt;Text=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;20&amp;quot;&lt;/span&gt;&amp;gt;&amp;lt;/&lt;span style="color: #9b703f"&gt;asp:ListItem&lt;/span&gt;&amp;gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;span style="color: #9b703f"&gt;asp:ListItem&lt;/span&gt; &lt;span style="color: #9b703f"&gt;Text=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;30&amp;quot;&lt;/span&gt;&amp;gt;&amp;lt;/&lt;span style="color: #9b703f"&gt;asp:ListItem&lt;/span&gt;&amp;gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;span style="color: #9b703f"&gt;asp:ListItem&lt;/span&gt; &lt;span style="color: #9b703f"&gt;Text=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;40&amp;quot;&lt;/span&gt;&amp;gt;&amp;lt;/&lt;span style="color: #9b703f"&gt;asp:ListItem&lt;/span&gt;&amp;gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;lt;/&lt;span style="color: #9b703f"&gt;asp:DropDownList&lt;/span&gt;&amp;gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;We’re using a control binding to bind the pageSizes.SelectedValue property to the pager’s PageSize property. &lt;/p&gt;  &lt;h2&gt;The prototype&lt;/h2&gt;  &lt;p&gt;Can’t have a blog post without code right :)? I’ve put together a little prototype of what this could look like but there are some gotchas.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;There is only one bindable control in the prototype &lt;strong&gt;&amp;lt;asp:BindableTextBox&amp;gt;&lt;/strong&gt; since I don’t have the power (I do but I wanted to give out a sample) to change the base Control class. &lt;/li&gt;    &lt;li&gt;The binding syntax only works for text properties. The ASP parser doesn’t like it when you supply invalid values for properties. i.e&amp;#160; &amp;lt;asp:BindableTextBox TextMode=”{Binding Foo}” /&amp;gt; won’t work since it will complain that it can’t convert that text to a TextMode enum. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;However you have full power in the code behind to do all of these things.&lt;/p&gt;  &lt;p&gt;To use the bindable controls just add the following to the &amp;lt;pages&amp;gt; section in web.config&lt;/p&gt;  &lt;div style="padding-bottom: 5px; padding-left: 5px; padding-right: 5px; font-family: consolas; background: #3f3f3f; color: white; font-size: 10pt; padding-top: 5px"&gt;   &lt;p style="margin: 0px"&gt;&amp;lt;&lt;span style="color: #9b703f"&gt;add&lt;/span&gt; &lt;span style="color: #cda869"&gt;tagPrefix&lt;/span&gt;=&amp;quot;&lt;span style="color: #8f9d6a"&gt;asp&lt;/span&gt;&amp;quot; &lt;span style="color: #cda869"&gt;namespace&lt;/span&gt;=&amp;quot;&lt;span style="color: #8f9d6a"&gt;Web.Binding.Controls&lt;/span&gt;&amp;quot; &lt;span style="color: #cda869"&gt;assembly&lt;/span&gt;=&amp;quot;&lt;span style="color: #8f9d6a"&gt;Web.Binding&lt;/span&gt;&amp;quot;/&amp;gt;&lt;/p&gt; &lt;/div&gt;  &lt;h3&gt;What’s in the package?&lt;/h3&gt;  &lt;p&gt;There is a &lt;strong&gt;BindableControl&lt;/strong&gt; base class just in case you want to write more bindable controls :). &lt;/p&gt;  &lt;p&gt;The &lt;strong&gt;BindableTextBox&lt;/strong&gt; wraps a &lt;strong&gt;BindableControl&lt;/strong&gt; since C# doesn’t support multiple inheritance and we want to get the behavior of both of those classes. Ideally this would be baked into the base &lt;strong&gt;Control &lt;/strong&gt;class so all of the controls get this behavior for free.&lt;/p&gt;  &lt;p&gt;The &lt;strong&gt;BindableControlBuilder &lt;/strong&gt;parses the binding expressions and generates the right binding code.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;div style="padding-bottom: 5px; padding-left: 5px; padding-right: 5px; font-family: consolas; background: #3f3f3f; color: white; font-size: 10pt; padding-top: 5px"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: #cda869"&gt;public&lt;/span&gt; &lt;span style="color: #cda869"&gt;class&lt;/span&gt; &lt;span style="color: #9b703f"&gt;BindableControlBuilder&lt;/span&gt; : &lt;span style="color: #9b703f"&gt;ControlBuilder&lt;/span&gt; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #cda869"&gt;private&lt;/span&gt; &lt;span style="color: #9b703f"&gt;HashSet&lt;/span&gt;&amp;lt;&lt;span style="color: #9b703f"&gt;BindingExpression&lt;/span&gt;&amp;gt; _bindings = &lt;span style="color: #cda869"&gt;new&lt;/span&gt; &lt;span style="color: #9b703f"&gt;HashSet&lt;/span&gt;&amp;lt;&lt;span style="color: #9b703f"&gt;BindingExpression&lt;/span&gt;&amp;gt;();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #cda869"&gt;public&lt;/span&gt; &lt;span style="color: #cda869"&gt;override&lt;/span&gt; &lt;span style="color: #cda869"&gt;void&lt;/span&gt; Init(&lt;span style="color: #9b703f"&gt;TemplateParser&lt;/span&gt; parser, &lt;span style="color: #9b703f"&gt;ControlBuilder&lt;/span&gt; parentBuilder, &lt;span style="color: #9b703f"&gt;Type&lt;/span&gt; type, &lt;span style="color: #cda869"&gt;string&lt;/span&gt; tagName, &lt;span style="color: #cda869"&gt;string&lt;/span&gt; id, &lt;span style="color: #9b703f"&gt;IDictionary&lt;/span&gt; attribs) {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #cda869"&gt;foreach&lt;/span&gt; (&lt;span style="color: #9b703f"&gt;DictionaryEntry&lt;/span&gt; entry &lt;span style="color: #cda869"&gt;in&lt;/span&gt; attribs.Cast&amp;lt;&lt;span style="color: #9b703f"&gt;DictionaryEntry&lt;/span&gt;&amp;gt;().ToList()) {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #cda869"&gt;string&lt;/span&gt; key = (&lt;span style="color: #cda869"&gt;string&lt;/span&gt;)entry.Key;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #cda869"&gt;string&lt;/span&gt; value = entry.Value &lt;span style="color: #cda869"&gt;as&lt;/span&gt; &lt;span style="color: #cda869"&gt;string&lt;/span&gt;;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #9b703f"&gt;BindingExpression&lt;/span&gt; expr;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #cda869"&gt;if&lt;/span&gt; (value != &lt;span style="color: #cda869"&gt;null&lt;/span&gt; &amp;amp;&amp;amp; &lt;span style="color: #9b703f"&gt;BindingExpression&lt;/span&gt;.TryParse(key, value, &lt;span style="color: #cda869"&gt;out&lt;/span&gt; expr)) {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #5f5a60"&gt;// Add to our list of bindings&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _bindings.Add(expr);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #5f5a60"&gt;// Remove the attribute so the binding expression doesn't show up as a property value&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; attribs.Remove(key);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #cda869"&gt;base&lt;/span&gt;.Init(parser, parentBuilder, type, tagName, id, attribs);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #cda869"&gt;public&lt;/span&gt; &lt;span style="color: #cda869"&gt;override&lt;/span&gt; &lt;span style="color: #cda869"&gt;void&lt;/span&gt; ProcessGeneratedCode(&lt;span style="color: #9b703f"&gt;CodeCompileUnit&lt;/span&gt; codeCompileUnit, &lt;span style="color: #9b703f"&gt;CodeTypeDeclaration&lt;/span&gt; baseType, &lt;span style="color: #9b703f"&gt;CodeTypeDeclaration&lt;/span&gt; derivedType, &lt;span style="color: #9b703f"&gt;CodeMemberMethod&lt;/span&gt; buildMethod, &lt;span style="color: #9b703f"&gt;CodeMemberMethod&lt;/span&gt; dataBindingMethod) {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #cda869"&gt;if&lt;/span&gt; (buildMethod != &lt;span style="color: #cda869"&gt;null&lt;/span&gt;) {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #cda869"&gt;foreach&lt;/span&gt; (&lt;span style="color: #cda869"&gt;var&lt;/span&gt; binding &lt;span style="color: #cda869"&gt;in&lt;/span&gt; _bindings) {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #5f5a60"&gt;// Generate code foreach binding and add it to the &lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #5f5a60"&gt;// build method for this control&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #cda869"&gt;var&lt;/span&gt; statement = binding.GenerateCode(buildMethod);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #cda869"&gt;int&lt;/span&gt; len = buildMethod.Statements.Count;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; buildMethod.Statements.Insert(len - &lt;span style="color: #cf6a4c"&gt;1&lt;/span&gt;, statement);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;}&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Also included in the package are 3 types of bindings:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Databinding – What ASP.NET has today. &lt;/li&gt;    &lt;li&gt;ControlBinding – Bind a control property to another control’s property &lt;/li&gt;    &lt;li&gt;ValueBinding – Bind a control property by executing a delegate to get the value. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The first 2 can be used declaratively but the ValueBinding is only supported imperatively.&lt;/p&gt;  &lt;p&gt;I’d love to get feedback on what people think about this. You can download the prototype &lt;a href="http://weblogs.asp.net/blogs/davidfowler/WPFStyleBinding.zip"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7255230" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/davidfowler/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/davidfowler/archive/tags/DataBinding/default.aspx">DataBinding</category><category domain="http://weblogs.asp.net/davidfowler/archive/tags/WPF/default.aspx">WPF</category></item><item><title>Databinding 3.0</title><link>http://weblogs.asp.net/davidfowler/archive/2009/11/13/databinding-3-0.aspx</link><pubDate>Fri, 13 Nov 2009 10:37:21 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7253317</guid><dc:creator>davidfowl</dc:creator><slash:comments>7</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/davidfowler/rsscomments.aspx?PostID=7253317</wfw:commentRss><comments>http://weblogs.asp.net/davidfowler/archive/2009/11/13/databinding-3-0.aspx#comments</comments><description>&lt;p&gt;There was a post on our internal discussion group recently where a customer pointed out one of the weaknesses of 2 way data binding not working within user controls. Consider the following page:&lt;/p&gt;  &lt;div style="padding-bottom: 5px; padding-left: 5px; padding-right: 5px; font-family: consolas; background: #3f3f3f; color: white; font-size: 10pt; padding-top: 5px"&gt;   &lt;p style="margin: 0px"&gt;&amp;lt;&lt;span style="color: #9b703f"&gt;asp:ObjectDataSource&lt;/span&gt; &lt;span style="color: #9b703f"&gt;runat=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;ID=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;personSource&amp;quot;&lt;/span&gt; &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #9b703f"&gt;SelectMethod=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;GetPersons&amp;quot;&lt;/span&gt; &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #9b703f"&gt;UpdateMethod=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;Update&amp;quot;&lt;/span&gt; &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #9b703f"&gt;DataObjectTypeName=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;ExtendedDatabinding.Person&amp;quot;&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #9b703f"&gt;TypeName=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;ExtendedDatabinding.PersonRepository&amp;quot;&lt;/span&gt;&amp;gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;lt;/&lt;span style="color: #9b703f"&gt;asp:ObjectDataSource&lt;/span&gt;&amp;gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;lt;&lt;span style="color: #9b703f"&gt;asp:FormView&lt;/span&gt; &lt;span style="color: #9b703f"&gt;runat=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;ID=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;personFormView&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;DataSourceID=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;personSource&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;DefaultMode=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;Edit&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;DataKeyNames=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;&amp;gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;span style="color: #9b703f"&gt;EditItemTemplate&lt;/span&gt;&amp;gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;span style="color: #9b703f"&gt;custom:personedit&lt;/span&gt; &lt;span style="color: #9b703f"&gt;runat=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;ID=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;personForm&amp;quot;&lt;/span&gt; /&amp;gt; &amp;lt;&lt;span style="color: #9b703f"&gt;br&lt;/span&gt; /&amp;gt; &amp;lt;&lt;span style="color: #9b703f"&gt;br&lt;/span&gt; /&amp;gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;span style="color: #9b703f"&gt;asp:Button&lt;/span&gt; &lt;span style="color: #9b703f"&gt;runat=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;Text=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;Update&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;CommandName=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;Update&amp;quot;&lt;/span&gt; /&amp;gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/&lt;span style="color: #9b703f"&gt;EditItemTemplate&lt;/span&gt;&amp;gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;lt;/&lt;span style="color: #9b703f"&gt;asp:FormView&lt;/span&gt;&amp;gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;I have an ObjectDataSource that points to some business object and is bound to a form view. Also note that we have one usercontrol is the form view, &amp;lt;custom:personedit&amp;gt; which contains UI for an edit form. This is pretty useful since I may want the same UI for edit and insert I could just use the same usercontrol.&lt;/p&gt;  &lt;p&gt;The problem is this doesn’t work with 2 way binding, so the FormView won’t extract the values from within the usercontrol even if there are &amp;lt;%# Bind() #&amp;gt; expressions declared. Here’s what the user control looks like:&lt;/p&gt;  &lt;div style="padding-bottom: 5px; padding-left: 5px; padding-right: 5px; font-family: consolas; background: #3f3f3f; color: white; font-size: 10pt; padding-top: 5px"&gt;   &lt;p style="margin: 0px"&gt;&amp;lt;&lt;span style="color: #9b703f"&gt;strong&lt;/span&gt;&amp;gt;First Name:&amp;lt;/&lt;span style="color: #9b703f"&gt;strong&lt;/span&gt;&amp;gt;&amp;lt;&lt;span style="color: #9b703f"&gt;asp:TextBox&lt;/span&gt; &lt;span style="color: #9b703f"&gt;runat=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;ID=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;TextBox1&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;Text=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;'&lt;/span&gt;&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt;# Bind(&amp;quot;FirstName&amp;quot;) &lt;span style="background: #ffee62"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;'&lt;/span&gt;&amp;gt;&amp;lt;/&lt;span style="color: #9b703f"&gt;asp:TextBox&lt;/span&gt;&amp;gt; &amp;lt;&lt;span style="color: #9b703f"&gt;br&lt;/span&gt; /&amp;gt; &amp;lt;&lt;span style="color: #9b703f"&gt;br&lt;/span&gt; /&amp;gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;lt;&lt;span style="color: #9b703f"&gt;strong&lt;/span&gt;&amp;gt;Last Name:&amp;lt;/&lt;span style="color: #9b703f"&gt;strong&lt;/span&gt;&amp;gt;&amp;lt;&lt;span style="color: #9b703f"&gt;asp:TextBox&lt;/span&gt; &lt;span style="color: #9b703f"&gt;runat=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;ID=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;TextBox2&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;Text=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;'&lt;/span&gt;&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt;# Bind(&amp;quot;LastName&amp;quot;) &lt;span style="background: #ffee62"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;'&lt;/span&gt;&amp;gt;&amp;lt;/&lt;span style="color: #9b703f"&gt;asp:TextBox&lt;/span&gt;&amp;gt; &amp;lt;&lt;span style="color: #9b703f"&gt;br&lt;/span&gt; /&amp;gt; &amp;lt;&lt;span style="color: #9b703f"&gt;br&lt;/span&gt; /&amp;gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;lt;&lt;span style="color: #9b703f"&gt;strong&lt;/span&gt;&amp;gt;Address:&amp;lt;/&lt;span style="color: #9b703f"&gt;strong&lt;/span&gt;&amp;gt;&amp;lt;&lt;span style="color: #9b703f"&gt;asp:TextBox&lt;/span&gt; &lt;span style="color: #9b703f"&gt;runat=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;ID=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;TextBox3&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;TextMode=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;MultiLine&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;Rows=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;5&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;Text=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;'&lt;/span&gt;&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt;# Bind(&amp;quot;Address&amp;quot;) &lt;span style="background: #ffee62"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;'&lt;/span&gt;&amp;gt;&amp;lt;/&lt;span style="color: #9b703f"&gt;asp:TextBox&lt;/span&gt;&amp;gt; &amp;lt;&lt;span style="color: #9b703f"&gt;br&lt;/span&gt; /&amp;gt; &amp;lt;&lt;span style="color: #9b703f"&gt;br&lt;/span&gt; /&amp;gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;lt;&lt;span style="color: #9b703f"&gt;strong&lt;/span&gt;&amp;gt;State:&amp;lt;/&lt;span style="color: #9b703f"&gt;strong&lt;/span&gt;&amp;gt;&amp;lt;&lt;span style="color: #9b703f"&gt;asp:DropDownList&lt;/span&gt; &lt;span style="color: #9b703f"&gt;runat=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;ID=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;DropDownList1&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;SelectedValue=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;'&lt;/span&gt;&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt;# Bind(&amp;quot;State&amp;quot;) &lt;span style="background: #ffee62"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;'&lt;/span&gt;&amp;gt; &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;span style="color: #9b703f"&gt;asp:ListItem&lt;/span&gt; &lt;span style="color: #9b703f"&gt;Text=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&amp;gt;&amp;lt;/&lt;span style="color: #9b703f"&gt;asp:ListItem&lt;/span&gt;&amp;gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;span style="color: #9b703f"&gt;asp:ListItem&lt;/span&gt; &lt;span style="color: #9b703f"&gt;Text=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;FL&amp;quot;&lt;/span&gt;&amp;gt;&amp;lt;/&lt;span style="color: #9b703f"&gt;asp:ListItem&lt;/span&gt;&amp;gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;span style="color: #9b703f"&gt;asp:ListItem&lt;/span&gt; &lt;span style="color: #9b703f"&gt;Text=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;WA&amp;quot;&lt;/span&gt;&amp;gt;&amp;lt;/&lt;span style="color: #9b703f"&gt;asp:ListItem&lt;/span&gt;&amp;gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;span style="color: #9b703f"&gt;asp:ListItem&lt;/span&gt; &lt;span style="color: #9b703f"&gt;Text=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;CA&amp;quot;&lt;/span&gt;&amp;gt;&amp;lt;/&lt;span style="color: #9b703f"&gt;asp:ListItem&lt;/span&gt;&amp;gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;lt;/&lt;span style="color: #9b703f"&gt;asp:DropDownList&lt;/span&gt;&amp;gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;The sad thing is all of those Bind expression will be ignored when we hit update.&lt;/p&gt;  &lt;p&gt;I don’t want to get into exactly why this is the case (you should read about how &lt;a href="http://weblogs.asp.net/davidfowler/archive/2008/12/13/how-bind-works.aspx"&gt;bind works&lt;/a&gt;), instead I’d like to introduce a new way to do 2 way databinding.&lt;/p&gt;  &lt;p&gt;In Dynamic Data 3.5 sp1 we introduced an interface &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.ui.ibindablecontrol.aspx"&gt;IBindableControl&lt;/a&gt; which has an ExtractValues method that takes a dictionary to populate with name value pairs. FormView and ListView know about this interface and will look recursively for controls that implement IBindinableControl and call ExtractValues when performing an update, insert or delete (also to store the edit values in viewstate). We could make our usercontrol implement this interface and manually extract data from each control and put it into the dictionary, but that is tedious. &lt;/p&gt;  &lt;p&gt;Enter databinding 3.0. Using the hidden gem &lt;a href="http://blogs.msdn.com/davidebb/archive/2008/11/19/a-hidden-gem-for-control-builder-writers.aspx"&gt;ProcessGeneratedCode&lt;/a&gt; we can build a base class which I call BindableUserControl that supports a different Bind syntax to make this scenario work.&lt;/p&gt;  &lt;p&gt;The idea exploits the fact that most controls derive from WebControl so we take advantage of their ability to use expando attributes and use it to declare a bogus Binding attribute with a some bind expression as the value. But enough talk lets dive into some code!&lt;/p&gt;  &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;  &lt;div style="padding-bottom: 5px; padding-left: 5px; padding-right: 5px; font-family: consolas; background: #3f3f3f; color: white; font-size: 10pt; padding-top: 5px"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt;@ &lt;span style="color: #9b703f"&gt;Control&lt;/span&gt; &lt;span style="color: #9b703f"&gt;Language=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;C#&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;Inherits=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;Web.Binding.BindableUserControl&amp;quot;&lt;/span&gt; &lt;span style="background: #ffee62"&gt;%&amp;gt;&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;lt;&lt;span style="color: #9b703f"&gt;strong&lt;/span&gt;&amp;gt;First Name:&amp;lt;/&lt;span style="color: #9b703f"&gt;strong&lt;/span&gt;&amp;gt;&amp;lt;&lt;span style="color: #9b703f"&gt;asp:TextBox&lt;/span&gt; &lt;span style="color: #9b703f"&gt;runat=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;ID=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;firstName&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;Binding=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;{Text=FirstName}&amp;quot;&lt;/span&gt;&amp;gt;&amp;lt;/&lt;span style="color: #9b703f"&gt;asp:TextBox&lt;/span&gt;&amp;gt; &amp;lt;&lt;span style="color: #9b703f"&gt;br&lt;/span&gt; /&amp;gt; &amp;lt;&lt;span style="color: #9b703f"&gt;br&lt;/span&gt; /&amp;gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;lt;&lt;span style="color: #9b703f"&gt;strong&lt;/span&gt;&amp;gt;Last Name:&amp;lt;/&lt;span style="color: #9b703f"&gt;strong&lt;/span&gt;&amp;gt;&amp;lt;&lt;span style="color: #9b703f"&gt;asp:TextBox&lt;/span&gt; &lt;span style="color: #9b703f"&gt;runat=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;ID=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;lastName&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;Binding=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;{Text=LastName}&amp;quot;&lt;/span&gt;&amp;gt;&amp;lt;/&lt;span style="color: #9b703f"&gt;asp:TextBox&lt;/span&gt;&amp;gt; &amp;lt;&lt;span style="color: #9b703f"&gt;br&lt;/span&gt; /&amp;gt; &amp;lt;&lt;span style="color: #9b703f"&gt;br&lt;/span&gt; /&amp;gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;lt;&lt;span style="color: #9b703f"&gt;strong&lt;/span&gt;&amp;gt;Address:&amp;lt;/&lt;span style="color: #9b703f"&gt;strong&lt;/span&gt;&amp;gt;&amp;lt;&lt;span style="color: #9b703f"&gt;asp:TextBox&lt;/span&gt; &lt;span style="color: #9b703f"&gt;runat=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;ID=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;address&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;TextMode=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;MultiLine&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;Rows=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;5&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;Binding=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;{Text=Address}&amp;quot;&lt;/span&gt;&amp;gt;&amp;lt;/&lt;span style="color: #9b703f"&gt;asp:TextBox&lt;/span&gt;&amp;gt; &amp;lt;&lt;span style="color: #9b703f"&gt;br&lt;/span&gt; /&amp;gt; &amp;lt;&lt;span style="color: #9b703f"&gt;br&lt;/span&gt; /&amp;gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;lt;&lt;span style="color: #9b703f"&gt;strong&lt;/span&gt;&amp;gt;State:&amp;lt;/&lt;span style="color: #9b703f"&gt;strong&lt;/span&gt;&amp;gt;&amp;lt;&lt;span style="color: #9b703f"&gt;asp:DropDownList&lt;/span&gt; &lt;span style="color: #9b703f"&gt;runat=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;ID=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;ddl&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;Binding=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;{SelectedValue=State}&amp;quot;&lt;/span&gt;&amp;gt; &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;span style="color: #9b703f"&gt;asp:ListItem&lt;/span&gt; &lt;span style="color: #9b703f"&gt;Text=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;FL&amp;quot;&lt;/span&gt;&amp;gt;&amp;lt;/&lt;span style="color: #9b703f"&gt;asp:ListItem&lt;/span&gt;&amp;gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;span style="color: #9b703f"&gt;asp:ListItem&lt;/span&gt; &lt;span style="color: #9b703f"&gt;Text=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;WA&amp;quot;&lt;/span&gt;&amp;gt;&amp;lt;/&lt;span style="color: #9b703f"&gt;asp:ListItem&lt;/span&gt;&amp;gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;span style="color: #9b703f"&gt;asp:ListItem&lt;/span&gt; &lt;span style="color: #9b703f"&gt;Text=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;CA&amp;quot;&lt;/span&gt;&amp;gt;&amp;lt;/&lt;span style="color: #9b703f"&gt;asp:ListItem&lt;/span&gt;&amp;gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;lt;/&lt;span style="color: #9b703f"&gt;asp:DropDownList&lt;/span&gt;&amp;gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;We’re going to use the &lt;strong&gt;Binding&lt;/strong&gt; attribute to determine what to bind and also the kind of binding to do. From just looking you can pretty much follow the syntax:     &lt;br /&gt;{ControlPropertyName=PropertyName}&lt;/p&gt;  &lt;p&gt;There is also an enum you can use to specify what kind of binding you want to do:    &lt;br /&gt;    &lt;br /&gt;{Text=Description, Mode=TwoWay}     &lt;br /&gt;{Text=Description, Mode=In}     &lt;br /&gt;{Text=Description, Mode=Out}&lt;/p&gt;  &lt;p&gt;It basically tells the ControlBuilder what to generate i.e. Eval or ExtractValues statements or both.&lt;/p&gt;  &lt;p&gt;The special base class BindableUserControl has a FileLevelControlBuilder that does all of the magic. The good news is that binding will work as expected now.&lt;/p&gt;  &lt;p&gt;You can download the sample project here:    &lt;br /&gt;    &lt;br /&gt;&lt;a title="ExtendedDatabinding.zip" href="http://weblogs.asp.net/blogs/davidfowler/ExtendedDatabinding.zip"&gt;ExtendedDatabinding.zip&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7253317" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/davidfowler/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/davidfowler/archive/tags/DataBinding/default.aspx">DataBinding</category><category domain="http://weblogs.asp.net/davidfowler/archive/tags/Data+Controls/default.aspx">Data Controls</category></item><item><title>My first channel 9 video!</title><link>http://weblogs.asp.net/davidfowler/archive/2009/10/22/my-first-channel-9-video.aspx</link><pubDate>Fri, 23 Oct 2009 04:16:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7236675</guid><dc:creator>davidfowl</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/davidfowler/rsscomments.aspx?PostID=7236675</wfw:commentRss><comments>http://weblogs.asp.net/davidfowler/archive/2009/10/22/my-first-channel-9-video.aspx#comments</comments><description>Wow, I haven't blogged in a while...but that will end soon. Watch me talk about the QueryExtender control. It's a new control we added in ASP.NET 4.0 to make Linq queries even simpler. The new control works with LinqDataSource and EntityDataSource. We also built all of the new DynamicData filters on top of this new control.

&lt;br/&gt; &lt;br/&gt;
Check it out &lt;a href="http://channel9.msdn.com/posts/Glucose/Hanselminutes-on-9-ASPNET-4-and-David-Fowler-on-LinqExtender/"&gt; here &lt;/a&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7236675" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/davidfowler/archive/tags/ASP.NET/default.aspx">ASP.NET</category></item><item><title>Dynamic Data in Regular Websites/Web Applications</title><link>http://weblogs.asp.net/davidfowler/archive/2009/05/06/dynamic-data-preview-4.aspx</link><pubDate>Thu, 07 May 2009 01:33:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7075337</guid><dc:creator>davidfowl</dc:creator><slash:comments>14</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/davidfowler/rsscomments.aspx?PostID=7075337</wfw:commentRss><comments>http://weblogs.asp.net/davidfowler/archive/2009/05/06/dynamic-data-preview-4.aspx#comments</comments><description>&lt;h4&gt;Update: David Ebbo has a great video on channel9 about this. You can watch it &lt;a href="http://channel9.msdn.com/posts/Glucose/Hanselminutes-on-9-ASPNET-4-and-David-Ebbo-on-Dynamic-Data-for-Older-Apps/"&gt;here&lt;/a&gt;.&lt;/h4&gt;  &lt;p mce_keep="true"&gt;Today I'm excited to share that we've released DynamicData Preview 4 on codeplex. Check out the latest bits &lt;a href="http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=27026" mce_href="http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=27026"&gt;here&lt;/a&gt;.     &lt;br /&gt;    &lt;br /&gt;This release is particularly interesting not only for people that have been using Dynamic Data for a while now, but anyone that has an existing application today who wants to use some of the niceties Dynamic Data offers without having to take all the &lt;strong&gt;junk&lt;/strong&gt; associated. Take a look at the &lt;strong&gt;SimpleDynamicData&lt;/strong&gt; project for examples.     &lt;br /&gt;    &lt;br /&gt;&lt;strong&gt;&lt;u&gt;Existing Sites&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p mce_keep="true"&gt;Here are 2 good reasons to use Dynamic Data:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;     &lt;div mce_keep="true"&gt;Rich model validation&lt;/div&gt;   &lt;/li&gt;    &lt;li&gt;     &lt;div mce_keep="true"&gt;Rich templating support via FieldTemplates&amp;#160; &lt;/div&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;p mce_keep="true"&gt;If you've ever written a data driven app in webforms using our data controls, you would have realized that we are lacking a lot when it comes to validation. You can enable all of this goodness with a magic extension method.&lt;/p&gt;  &lt;div style="border-bottom: #ccc 1px solid; border-left: #ccc 1px solid; padding-bottom: 2px; padding-left: 2px; padding-right: 2px; font-family: consolas; background: black; color: white; font-size: 8pt; border-top: #ccc 1px solid; border-right: #ccc 1px solid; padding-top: 2px"&gt;   &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&lt;span style="color: #cda869"&gt;protected&lt;/span&gt; &lt;span style="color: #cda869"&gt;void&lt;/span&gt; Page_Init() {&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;    GridView1.EnableDynamicData(&lt;font color="#cda869"&gt;your type here&lt;/font&gt;);&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;}&lt;/pre&gt;
&lt;!--EndFragment--&gt;&lt;/div&gt;

&lt;p mce_keep="true"&gt;&lt;strong&gt;Note: The method requires that you pass in the type that may have annotations in order for us to read Metadata. If you're not familiar with the way annotations work in Dynamic Data then watch the videos &lt;/strong&gt;&lt;a href="http://www.asp.net/dynamicdata/" mce_href="http://www.asp.net/dynamicdata/"&gt;&lt;strong&gt;here&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p mce_keep="true"&gt;This method call enables &lt;strong&gt;DynamicControl&lt;/strong&gt;/&lt;strong&gt;DynamicField&lt;/strong&gt; to work within any of the data controls which makes use of &lt;a href="http://msdn.microsoft.com/en-us/library/cc488523.aspx" mce_href="http://msdn.microsoft.com/en-us/library/cc488523.aspx"&gt;&lt;strong&gt;FieldTemplates&lt;/strong&gt;&lt;/a&gt;, and enables the rich validation support.&lt;/p&gt;

&lt;p mce_keep="true"&gt;&lt;strong&gt;&lt;u&gt;Making it work&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p mce_keep="true"&gt;So we know about this magic method call and somehow calling it with a type makes it all just work. Let's walk through an example of how we would use this. Here is my model:&lt;/p&gt;

&lt;div style="border-bottom: #ccc 1px solid; border-left: #ccc 1px solid; padding-bottom: 2px; padding-left: 2px; padding-right: 2px; font-family: consolas; background: black; color: white; font-size: 8pt; border-top: #ccc 1px solid; border-right: #ccc 1px solid; padding-top: 2px"&gt;
  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&lt;span style="color: #cda869"&gt;public&lt;/span&gt; &lt;span style="color: #cda869"&gt;class&lt;/span&gt; &lt;span style="color: #9b703f"&gt;Student&lt;/span&gt; {&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160; [&lt;span style="color: #9b703f"&gt;Required&lt;/span&gt;]&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160; [&lt;span style="color: #9b703f"&gt;Range&lt;/span&gt;(&lt;span style="color: #cf6a4c"&gt;0&lt;/span&gt;, &lt;span style="color: #cf6a4c"&gt;100&lt;/span&gt;)]&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #cda869"&gt;public&lt;/span&gt; &lt;span style="color: #cda869"&gt;int&lt;/span&gt; Age { &lt;span style="color: #cda869"&gt;get&lt;/span&gt;; &lt;span style="color: #cda869"&gt;set&lt;/span&gt;; }&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160; [&lt;span style="color: #9b703f"&gt;Range&lt;/span&gt;(&lt;span style="color: #cf6a4c"&gt;0.0&lt;/span&gt;, &lt;span style="color: #cf6a4c"&gt;4.0&lt;/span&gt;)]&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #cda869"&gt;public&lt;/span&gt; &lt;span style="color: #cda869"&gt;double&lt;/span&gt; GPA { &lt;span style="color: #cda869"&gt;get&lt;/span&gt;; &lt;span style="color: #cda869"&gt;set&lt;/span&gt;; }&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160; [&lt;span style="color: #9b703f"&gt;Required&lt;/span&gt;]&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #cda869"&gt;public&lt;/span&gt; &lt;span style="color: #cda869"&gt;string&lt;/span&gt; FirstName { &lt;span style="color: #cda869"&gt;get&lt;/span&gt;; &lt;span style="color: #cda869"&gt;set&lt;/span&gt;; }&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160; [&lt;span style="color: #9b703f"&gt;Required&lt;/span&gt;]&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #cda869"&gt;public&lt;/span&gt; &lt;span style="color: #cda869"&gt;string&lt;/span&gt; LastName { &lt;span style="color: #cda869"&gt;get&lt;/span&gt;; &lt;span style="color: #cda869"&gt;set&lt;/span&gt;; }&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160; [&lt;span style="color: #9b703f"&gt;DisplayFormat&lt;/span&gt;(DataFormatString = &lt;span style="color: #8f9d6a"&gt;&amp;quot;{0:d/M/yyyy}&amp;quot;&lt;/span&gt;)]&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #cda869"&gt;public&lt;/span&gt; &lt;span style="color: #9b703f"&gt;DateTime&lt;/span&gt; BirthDate { &lt;span style="color: #cda869"&gt;get&lt;/span&gt;; &lt;span style="color: #cda869"&gt;set&lt;/span&gt;; }&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;!--EndFragment--&gt;

&lt;p&gt;This is the type we are going to use for metadata. Using the attributes from &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.aspx"&gt;System.ComponentModel.DataAnnotations&lt;/a&gt; we can add useful annotations to our model that will be used for validation and display formatting. Adding these attributes allows Dynamic Data to enable the appropriate validator. i.e RangeValidator, RequiredFieldValidator etc.&lt;/p&gt;

&lt;p&gt;Now we're going to enable this on our GridView using the same method call as above in conjunction with an ObjectDataSource to complete our application: 
  &lt;br /&gt;&lt;/p&gt;

&lt;div style="border-bottom: #ccc 1px solid; border-left: #ccc 1px solid; padding-bottom: 2px; padding-left: 2px; padding-right: 2px; font-family: consolas; background: black; color: white; font-size: 8pt; border-top: #ccc 1px solid; border-right: #ccc 1px solid; padding-top: 2px"&gt;
  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&lt;span style="color: #cda869"&gt;protected&lt;/span&gt; &lt;span style="color: #cda869"&gt;void&lt;/span&gt; Page_Init() {&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160; GridView1.EnableDynamicData(&lt;span style="color: #cda869"&gt;typeof&lt;/span&gt;(&lt;span style="color: #9b703f"&gt;Student&lt;/span&gt;));&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;!--EndFragment--&gt;

&lt;p mce_keep="true"&gt;&lt;strong&gt;&lt;u&gt;Markup&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;div style="border-bottom: #ccc 1px solid; border-left: #ccc 1px solid; padding-bottom: 2px; padding-left: 2px; padding-right: 2px; font-family: consolas; background: black; color: white; font-size: 8pt; border-top: #ccc 1px solid; border-right: #ccc 1px solid; padding-top: 2px"&gt;
  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;lt;&lt;span style="color: #9b703f"&gt;asp:GridView&lt;/span&gt; &lt;span style="color: #9b703f"&gt;ID=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;GridView1&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;runat=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;DataSourceID=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;ObjectDataSource1&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;AutoGenerateEditButton=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;true&amp;quot;&lt;/span&gt;&amp;gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/&lt;span style="color: #9b703f"&gt;asp:GridView&lt;/span&gt;&amp;gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;span style="color: #9b703f"&gt;asp:ObjectDataSource&lt;/span&gt; &lt;span style="color: #9b703f"&gt;ID=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;ObjectDataSource1&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;runat=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;server&amp;quot;&lt;/span&gt; &lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #9b703f"&gt;DataObjectTypeName=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;Student&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;DeleteMethod=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;DeleteStudent&amp;quot;&lt;/span&gt; &lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #9b703f"&gt;InsertMethod=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;InsertStudent&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;SelectMethod=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;GetStudents&amp;quot;&lt;/span&gt; &lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #9b703f"&gt;TypeName=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;StudentsRepository&amp;quot;&lt;/span&gt; &lt;span style="color: #9b703f"&gt;UpdateMethod=&lt;/span&gt;&lt;span style="color: #8f9d6a"&gt;&amp;quot;UpdateStudent&amp;quot;&lt;/span&gt;&amp;gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/&lt;span style="color: #9b703f"&gt;asp:ObjectDataSource&lt;/span&gt;&amp;gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Note: Dynamic Data takes care of the Metadata not the data. You still need databind the data control against some data source/data source control.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here are the results when we are editing:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/davidfowler/dynamic_data_grid.png" mce_href="http://weblogs.asp.net/blogs/davidfowler/dynamic_data_grid.png"&gt;&lt;img style="width: 927px; height: 160px" border="0" src="http://weblogs.asp.net/blogs/davidfowler/dynamic_data_grid.png" width="1094" height="174" mce_src="http://weblogs.asp.net/blogs/davidfowler/dynamic_data_grid.png" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, the attributes we specified on our Student class directly affect the grid and validation is enabled.&lt;/p&gt;

&lt;p&gt;There's more cool stuff to talk about but I'll mention those in upcoming posts. For now, download the preview and read up on Dynamic Data!&lt;/p&gt;
&lt;!--EndFragment--&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7075337" width="1" height="1"&gt;</description></item><item><title>External ITemplates and Hierarchical Databinding</title><link>http://weblogs.asp.net/davidfowler/archive/2009/03/16/external-itemplates-and-hierarchical-databinding.aspx</link><pubDate>Tue, 17 Mar 2009 06:25:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6969059</guid><dc:creator>davidfowl</dc:creator><slash:comments>7</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/davidfowler/rsscomments.aspx?PostID=6969059</wfw:commentRss><comments>http://weblogs.asp.net/davidfowler/archive/2009/03/16/external-itemplates-and-hierarchical-databinding.aspx#comments</comments><description>&lt;p mce_keep="true"&gt;Ever wish you could declare a template outside of the control you were defining the template for? We always get requests to have FormView's InsertItem template fall back on the EditItemTemplate and vice versa. That would be easy if we could do what was mentioned above. &lt;/p&gt;  &lt;p mce_keep="true"&gt;Consider:&lt;/p&gt;  &lt;div style="border-bottom: #ccc 1px solid; border-left: #ccc 1px solid; padding-bottom: 2px; padding-left: 2px; padding-right: 2px; font-family: consolas; background: black; color: white; font-size: 8pt; border-top: #ccc 1px solid; border-right: #ccc 1px solid; padding-top: 2px"&gt;   &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;lt;&lt;span style="color: #ff8000"&gt;asp&lt;/span&gt;:&lt;span style="color: #ff8000"&gt;FormView&lt;/span&gt; &lt;span style="color: yellow"&gt;ID&lt;/span&gt;=&amp;quot;myFormView&amp;quot; &lt;span style="color: yellow"&gt;runat&lt;/span&gt;=&amp;quot;server&amp;quot; &lt;span style="color: yellow"&gt;DefaultMode&lt;/span&gt;=&amp;quot;Edit&amp;quot; &lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: yellow"&gt;EditItemTemplate&lt;/span&gt;=&amp;quot;editTemplate&amp;quot; &lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: yellow"&gt;InsertItemTemplate&lt;/span&gt;=&amp;quot;editTemplate&amp;quot;&amp;gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;lt;/&lt;span style="color: #ff8000"&gt;asp&lt;/span&gt;:&lt;span style="color: #ff8000"&gt;FormView&lt;/span&gt;&amp;gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;lt;&lt;span style="color: #ff8000"&gt;asp&lt;/span&gt;:&lt;span style="color: #ff8000"&gt;Template&lt;/span&gt; &lt;span style="color: yellow"&gt;runat&lt;/span&gt;=&amp;quot;server&amp;quot; &lt;span style="color: yellow"&gt;ID&lt;/span&gt;=&amp;quot;editTemplate&amp;quot;&amp;gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160; Name : &amp;lt;&lt;span style="color: #ff8000"&gt;asp&lt;/span&gt;:&lt;span style="color: #ff8000"&gt;TextBox&lt;/span&gt; &lt;span style="color: yellow"&gt;runat&lt;/span&gt;=&amp;quot;server&amp;quot; &lt;span style="color: yellow"&gt;Text&lt;/span&gt;='&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt;# Bind(&amp;quot;Name&amp;quot;) &lt;span style="background: #ffee62"&gt;%&amp;gt;&lt;/span&gt;'&amp;gt;&amp;lt;/&lt;span style="color: #ff8000"&gt;asp&lt;/span&gt;:&lt;span style="color: #ff8000"&gt;TextBox&lt;/span&gt;&amp;gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160; Age : &amp;lt;&lt;span style="color: #ff8000"&gt;asp&lt;/span&gt;:&lt;span style="color: #ff8000"&gt;TextBox&lt;/span&gt; &lt;span style="color: yellow"&gt;runat&lt;/span&gt;=&amp;quot;server&amp;quot; &lt;span style="color: yellow"&gt;Text&lt;/span&gt;='&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt;# Bind(&amp;quot;Age&amp;quot;) &lt;span style="background: #ffee62"&gt;%&amp;gt;&lt;/span&gt;'&amp;gt;&amp;lt;/&lt;span style="color: #ff8000"&gt;asp&lt;/span&gt;:&lt;span style="color: #ff8000"&gt;TextBox&lt;/span&gt;&amp;gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;lt;/&lt;span style="color: #ff8000"&gt;asp&lt;/span&gt;:&lt;span style="color: #ff8000"&gt;Template&lt;/span&gt;&amp;gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;!--EndFragment--&gt;

&lt;p mce_keep="true"&gt;Then you could do things like hierarchical databinding pretty easily; just define the template in terms of itself. Today, properties typed as ITemplate are treated specially by the ASP.NET parser, and what is written above will not work.
  &lt;br /&gt;

  &lt;br /&gt;How would you do this with what asp.net offers now? Well check out this sample:

  &lt;br /&gt;

  &lt;br /&gt;&lt;/p&gt;

&lt;div style="border-bottom: #ccc 1px solid; border-left: #ccc 1px solid; padding-bottom: 2px; padding-left: 2px; padding-right: 2px; font-family: consolas; background: black; color: white; font-size: 8pt; border-top: #ccc 1px solid; border-right: #ccc 1px solid; padding-top: 2px"&gt;
  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;lt;&lt;span style="color: #ff8000"&gt;cc&lt;/span&gt;:&lt;span style="color: #ff8000"&gt;SpecialRepeater&lt;/span&gt; &lt;span style="color: yellow"&gt;runat&lt;/span&gt;=&amp;quot;server&amp;quot; &lt;span style="color: yellow"&gt;ItemTemplateID&lt;/span&gt;=&amp;quot;folderTemplate&amp;quot; &lt;span style="color: yellow"&gt;DataSource&lt;/span&gt;='&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt;# GetDirectories() &lt;span style="background: #ffee62"&gt;%&amp;gt;&lt;/span&gt;' /&amp;gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;lt;&lt;span style="color: #ff8000"&gt;cc&lt;/span&gt;:&lt;span style="color: #ff8000"&gt;Template&lt;/span&gt; &lt;span style="color: yellow"&gt;runat&lt;/span&gt;=&amp;quot;server&amp;quot; &lt;span style="color: yellow"&gt;ID&lt;/span&gt;=&amp;quot;folderTemplate&amp;quot;&amp;gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;span style="color: #ff8000"&gt;ItemTemplate&lt;/span&gt;&amp;gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;span style="color: #ff8000"&gt;ul&lt;/span&gt;&amp;gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;span style="color: #ff8000"&gt;li&lt;/span&gt;&amp;gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt;# Eval(&lt;span style="color: #a31515"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;) &lt;span style="background: #ffee62"&gt;%&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;span style="color: #ff8000"&gt;cc&lt;/span&gt;:&lt;span style="color: #ff8000"&gt;SpecialRepeater&lt;/span&gt; &lt;span style="color: yellow"&gt;runat&lt;/span&gt;=&amp;quot;server&amp;quot; &lt;span style="color: yellow"&gt;DataSource&lt;/span&gt;='&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt;# GetDirectories((string)Eval(&amp;quot;FullName&amp;quot;)) &lt;span style="background: #ffee62"&gt;%&amp;gt;&lt;/span&gt;' &lt;span style="color: yellow"&gt;ItemTemplateID&lt;/span&gt;=&amp;quot;folderTemplate&amp;quot; /&amp;gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;span style="color: #ff8000"&gt;ul&lt;/span&gt;&amp;gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;span style="color: #ff8000"&gt;cc&lt;/span&gt;:&lt;span style="color: #ff8000"&gt;SpecialRepeater&lt;/span&gt; &lt;span style="color: yellow"&gt;runat&lt;/span&gt;=&amp;quot;server&amp;quot; &lt;span style="color: yellow"&gt;DataSource&lt;/span&gt;='&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt;# GetFiles((string)Eval(&amp;quot;FullName&amp;quot;)) &lt;span style="background: #ffee62"&gt;%&amp;gt;&lt;/span&gt;' &lt;span style="color: yellow"&gt;ItemTemplateID&lt;/span&gt;=&amp;quot;fileTemplate&amp;quot; /&amp;gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/&lt;span style="color: #ff8000"&gt;ul&lt;/span&gt;&amp;gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/&lt;span style="color: #ff8000"&gt;li&lt;/span&gt;&amp;gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/&lt;span style="color: #ff8000"&gt;ul&lt;/span&gt;&amp;gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/&lt;span style="color: #ff8000"&gt;ItemTemplate&lt;/span&gt;&amp;gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;lt;/&lt;span style="color: #ff8000"&gt;cc&lt;/span&gt;:&lt;span style="color: #ff8000"&gt;Template&lt;/span&gt;&amp;gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;lt;&lt;span style="color: #ff8000"&gt;cc&lt;/span&gt;:&lt;span style="color: #ff8000"&gt;Template&lt;/span&gt; &lt;span style="color: yellow"&gt;runat&lt;/span&gt;=&amp;quot;server&amp;quot; &lt;span style="color: yellow"&gt;ID&lt;/span&gt;=&amp;quot;fileTemplate&amp;quot;&amp;gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;span style="color: #ff8000"&gt;ItemTemplate&lt;/span&gt;&amp;gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;span style="color: #ff8000"&gt;li&lt;/span&gt;&amp;gt; &lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt;# Eval(&lt;span style="color: #a31515"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;) &lt;span style="background: #ffee62"&gt;%&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/&lt;span style="color: #ff8000"&gt;li&lt;/span&gt;&amp;gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/&lt;span style="color: #ff8000"&gt;ItemTemplate&lt;/span&gt;&amp;gt;&lt;/pre&gt;

  &lt;pre style="margin: 0px; font-family: consolas; font-size: 10pt"&gt;&amp;lt;/&lt;span style="color: #ff8000"&gt;cc&lt;/span&gt;:&lt;span style="color: #ff8000"&gt;Template&lt;/span&gt;&amp;gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p mce_keep="true"&gt;&lt;!--EndFragment--&gt;We have a &lt;strong&gt;SpecialRepeater&lt;/strong&gt; that understands how to hookup template properties through their ID (it's just &lt;strong&gt;FindControl&lt;/strong&gt;) and a &lt;strong&gt;Template&lt;/strong&gt; control that defines our file template and folder template. We define the folder template in terms of itself. Can you think of any more uses for something like this?

  &lt;br /&gt;

  &lt;br /&gt;Get the code &lt;a href="http://weblogs.asp.net/blogs/davidfowler/RecursiveDatabinding.zip"&gt;here&lt;/a&gt;.

  &lt;br /&gt;

  &lt;br /&gt;What do you think?&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6969059" width="1" height="1"&gt;</description></item><item><title>Invalid postback or callback argument</title><link>http://weblogs.asp.net/davidfowler/archive/2009/03/09/invalid-postback-or-callback-argument-in-the-datacontrols.aspx</link><pubDate>Mon, 09 Mar 2009 07:54:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6951301</guid><dc:creator>davidfowl</dc:creator><slash:comments>9</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/davidfowler/rsscomments.aspx?PostID=6951301</wfw:commentRss><comments>http://weblogs.asp.net/davidfowler/archive/2009/03/09/invalid-postback-or-callback-argument-in-the-datacontrols.aspx#comments</comments><description>&lt;P mce_keep="true"&gt;I'm sure many of you have seen this error message when developing your web application:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Server Error in '/' Application.&lt;BR&gt;--------------------------------------------------------------------------------&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Invalid postback or callback argument.&amp;nbsp; Event validation is enabled using &amp;lt;pages enableEventValidation="true"/&amp;gt; in configuration or &amp;lt;%@ Page EnableEventValidation="true" %&amp;gt; in a page.&amp;nbsp; For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.&amp;nbsp; If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation. &lt;/STRONG&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;I'm going to discuss this in the context of the data controls. This happens when a control that isn't registered for event validation causes a postback, but surely that can't be the case.. right?&lt;/P&gt;
&lt;P mce_keep="true"&gt;Let's look at a small repro:&lt;BR&gt;&lt;BR&gt;&lt;STRONG&gt;Markup:&lt;/STRONG&gt;&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: #ccc 1px solid; BORDER-LEFT: #ccc 1px solid; PADDING-BOTTOM: 2px; PADDING-LEFT: 2px; PADDING-RIGHT: 2px; FONT-FAMILY: Consolas; BACKGROUND: black; COLOR: white; FONT-SIZE: 8pt; BORDER-TOP: #ccc 1px solid; BORDER-RIGHT: #ccc 1px solid; PADDING-TOP: 2px"&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;lt;&lt;SPAN style="COLOR: #ff8000"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff8000"&gt;GridView&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;ID&lt;/SPAN&gt;="GridView1" &lt;SPAN style="COLOR: yellow"&gt;runat&lt;/SPAN&gt;="server"&amp;gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: #ff8000"&gt;Columns&lt;/SPAN&gt;&amp;gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: #ff8000"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff8000"&gt;TemplateField&lt;/SPAN&gt;&amp;gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: #ff8000"&gt;ItemTemplate&lt;/SPAN&gt;&amp;gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: #ff8000"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff8000"&gt;Button&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;runat&lt;/SPAN&gt;="server" &lt;SPAN style="COLOR: yellow"&gt;Text&lt;/SPAN&gt;="Button" /&amp;gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;SPAN style="COLOR: #ff8000"&gt;ItemTemplate&lt;/SPAN&gt;&amp;gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;SPAN style="COLOR: #ff8000"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff8000"&gt;TemplateField&lt;/SPAN&gt;&amp;gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;SPAN style="COLOR: #ff8000"&gt;Columns&lt;/SPAN&gt;&amp;gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;lt;/&lt;SPAN style="COLOR: #ff8000"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff8000"&gt;GridView&lt;/SPAN&gt;&amp;gt;&lt;/PRE&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;
&lt;P mce_keep="true"&gt;&lt;STRONG&gt;Code behind:&lt;/STRONG&gt;&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: #ccc 1px solid; BORDER-LEFT: #ccc 1px solid; PADDING-BOTTOM: 2px; PADDING-LEFT: 2px; PADDING-RIGHT: 2px; FONT-FAMILY: Consolas; BACKGROUND: black; COLOR: white; FONT-SIZE: 8pt; BORDER-TOP: #ccc 1px solid; BORDER-RIGHT: #ccc 1px solid; PADDING-TOP: 2px"&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: yellow"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;partial&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;_Default&lt;/SPAN&gt; : System.Web.UI.&lt;SPAN style="COLOR: #2b91af"&gt;Page&lt;/SPAN&gt; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;protected&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;void&lt;/SPAN&gt; Page_Load(&lt;SPAN style="COLOR: yellow"&gt;object&lt;/SPAN&gt; sender, &lt;SPAN style="COLOR: #2b91af"&gt;EventArgs&lt;/SPAN&gt; e) {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; GridView1.DataSource = &lt;SPAN style="COLOR: #2b91af"&gt;Enumerable&lt;/SPAN&gt;.Range(0, 5);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; GridView1.DataBind();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Now click on the button and see the dreaded error message. Why does this happen? EventValidation was added in ASP.NET to ensure that controls causing the postback came from the same page being rendered. Take a look at &lt;STRONG&gt;__EVENTVALIDATION &lt;/STRONG&gt;hidden field on the page. It is a serialized version of all of the controls registered for postbacks(read more &lt;A href="http://odetocode.com/Blogs/scott/archive/2006/03/20/3145.aspx" mce_href="http://odetocode.com/Blogs/scott/archive/2006/03/20/3145.aspx"&gt;here&lt;/A&gt;). You might be wondering how they got in there and why is the button inside of a GridView a special case. It's not a special case, in fact, &lt;STRONG&gt;Button&lt;/STRONG&gt; registers itself with the current page. &lt;/P&gt;
&lt;P&gt;The reason this happens is because we rebind the data control in Page_Load every time which means that we will lose all of the posted data and viewstate. As a result, the ID of&amp;nbsp;the button is different and when the event is validated there will be no matching unique id&amp;nbsp;and hence event validation will fail. We are acutally raising an event for a button that is no longer in the control tree.&lt;/P&gt;
&lt;P&gt;You can work around this by wrapping that code in &lt;STRONG&gt;if (!IsPostBack)&lt;/STRONG&gt;. This is a good proof of why you should use &lt;A href="http://weblogs.asp.net/davidfowler/archive/2008/10/21/datacontrols-101-part-2-why-you-should-love-datasource-controls.aspx" mce_href="http://weblogs.asp.net/davidfowler/archive/2008/10/21/datacontrols-101-part-2-why-you-should-love-datasource-controls.aspx"&gt;DataSource controls&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;Hope this helps&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6951301" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/davidfowler/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/davidfowler/archive/tags/DataControls/default.aspx">DataControls</category></item><item><title>A new way to DataBind()</title><link>http://weblogs.asp.net/davidfowler/archive/2009/02/04/a-new-way-to-databind-possibly.aspx</link><pubDate>Thu, 05 Feb 2009 06:27:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6884056</guid><dc:creator>davidfowl</dc:creator><slash:comments>12</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/davidfowler/rsscomments.aspx?PostID=6884056</wfw:commentRss><comments>http://weblogs.asp.net/davidfowler/archive/2009/02/04/a-new-way-to-databind-possibly.aspx#comments</comments><description>&lt;P mce_keep="true"&gt;&lt;U&gt;&lt;STRONG&gt;The Problem&lt;/STRONG&gt;&amp;nbsp;&lt;BR&gt;&lt;/U&gt;&lt;BR&gt;I've been thinking alot recently about the problems with data binding (and they're alot). There are some patterns that play well with ASP.NET (and are repeated everywhere) and some that don't quite fit the model. One of those&amp;nbsp;patterns that don't mesh well&amp;nbsp;is, setting the DataSource property of any of the DataControls. Before ASP.NET 2.0 and DataSource controls we'd have to set the DataSource property and manually call DataBind. &lt;/P&gt;
&lt;P mce_keep="true"&gt;What's wrong with this you may ask? When are you supposed to DataBind? Page_Load ? Page_Init? Page_PreRender? I'm sure anyone that has had to manually data bind has had the problem of figuring out which code goes in and out of the &lt;STRONG&gt;IsPostBack&lt;/STRONG&gt; block.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;STRONG&gt;&lt;U&gt;Whats the solution?&lt;/U&gt;&lt;BR&gt;&lt;BR&gt;&lt;/STRONG&gt;I'm looking at an alternative approach to this problem. Event handlers are nice because we all feel confident that the author of the event knows when that event should be raised. No need to figure out the postback logic because the control author already thought about that. All you have to worry about is&amp;nbsp;handling the event.&lt;BR&gt;&lt;BR&gt;I propose a new hypothetical event &lt;STRONG&gt;OnDataRetrieveing&lt;/STRONG&gt;,&lt;STRONG&gt; &lt;/STRONG&gt;that would be on all DataControls in conjunction with a new flag &lt;STRONG&gt;AutoBind&amp;nbsp;&lt;/STRONG&gt;to enable it.&lt;BR&gt;&lt;BR&gt;Let's take a look at a sample implementation of a derived GridView control that has this new behavior. First the markup:&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: #ccc 1px solid; BORDER-LEFT: #ccc 1px solid; PADDING-BOTTOM: 2px; PADDING-LEFT: 2px; PADDING-RIGHT: 2px; FONT-FAMILY: Consolas; BACKGROUND: black; COLOR: white; FONT-SIZE: 8pt; BORDER-TOP: #ccc 1px solid; BORDER-RIGHT: #ccc 1px solid; PADDING-TOP: 2px"&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;lt;&lt;SPAN style="COLOR: #ff8000"&gt;custom&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff8000"&gt;AutoGridView&lt;/SPAN&gt; &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;OnDataRetrieving&lt;/SPAN&gt;="OnDataRetrieving"&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;OnPageIndexChanging&lt;/SPAN&gt;="OnGridViewPageIndexChanging"&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;OnRowEditing&lt;/SPAN&gt;="OnGridViewRowEditing"&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;OnRowCancelingEdit&lt;/SPAN&gt;="OnGridViewRowCancelingEdit"&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;AllowPaging&lt;/SPAN&gt;="true" &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;runat&lt;/SPAN&gt;="server" &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;ID&lt;/SPAN&gt;="GridView1" &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;AutoGenerateEditButton&lt;/SPAN&gt;="true"&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;AutoBind&lt;/SPAN&gt;="true"&amp;gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;lt;/&lt;SPAN style="COLOR: #ff8000"&gt;custom&lt;/SPAN&gt;:&lt;SPAN style="COLOR: #ff8000"&gt;AutoGridView&lt;/SPAN&gt;&amp;gt;&lt;/PRE&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;
&lt;P&gt;Looks pretty similar to the regular GridView besides the &lt;STRONG&gt;AutoBind="true" &lt;/STRONG&gt;flag and &lt;STRONG&gt;OnDataRetrieving&lt;/STRONG&gt; event. &lt;BR&gt;&lt;BR&gt;And the code behind&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: #ccc 1px solid; BORDER-LEFT: #ccc 1px solid; PADDING-BOTTOM: 2px; PADDING-LEFT: 2px; PADDING-RIGHT: 2px; FONT-FAMILY: Consolas; BACKGROUND: black; COLOR: white; FONT-SIZE: 8pt; BORDER-TOP: #ccc 1px solid; BORDER-RIGHT: #ccc 1px solid; PADDING-TOP: 2px"&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: yellow"&gt;protected&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;void&lt;/SPAN&gt; OnGridViewRowCancelingEdit(&lt;SPAN style="COLOR: yellow"&gt;object&lt;/SPAN&gt; sender, &lt;SPAN style="COLOR: #2b91af"&gt;GridViewCancelEditEventArgs&lt;/SPAN&gt; e) {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; GridView1.EditIndex = -1;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: yellow"&gt;protected&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;void&lt;/SPAN&gt; OnGridViewPageIndexChanging(&lt;SPAN style="COLOR: yellow"&gt;object&lt;/SPAN&gt; sender, &lt;SPAN style="COLOR: #2b91af"&gt;GridViewPageEventArgs&lt;/SPAN&gt; e) {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; GridView1.PageIndex = e.NewPageIndex;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: yellow"&gt;protected&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;void&lt;/SPAN&gt; OnGridViewRowEditing(&lt;SPAN style="COLOR: yellow"&gt;object&lt;/SPAN&gt; sender, &lt;SPAN style="COLOR: #2b91af"&gt;GridViewEditEventArgs&lt;/SPAN&gt; e) {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; GridView1.EditIndex = e.NewEditIndex;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: yellow"&gt;protected&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;void&lt;/SPAN&gt; OnDataRetrieving(&lt;SPAN style="COLOR: yellow"&gt;object&lt;/SPAN&gt; sender, &lt;SPAN style="COLOR: #2b91af"&gt;DataBindingEventArgs&lt;/SPAN&gt; e) {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;NorthwindDataContext&lt;/SPAN&gt; db = &lt;SPAN style="COLOR: yellow"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;NorthwindDataContext&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; e.DataSource = db.Products;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;We write code as we normally would if we'd been binding manually, but instead, we assign our data to the DataSource property in the &lt;STRONG&gt;DataBindingEventArgs&lt;/STRONG&gt; (Which gets called at the "right" time).&lt;/P&gt;
&lt;P&gt;So&amp;nbsp;what is the right time to Databind and why does this control do it better than you? I'm not sure :) but let's look at the source of this control.&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: #ccc 1px solid; BORDER-LEFT: #ccc 1px solid; PADDING-BOTTOM: 2px; PADDING-LEFT: 2px; PADDING-RIGHT: 2px; FONT-FAMILY: Consolas; BACKGROUND: black; COLOR: white; FONT-SIZE: 8pt; BORDER-TOP: #ccc 1px solid; BORDER-RIGHT: #ccc 1px solid; PADDING-TOP: 2px"&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: yellow"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;override&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;object&lt;/SPAN&gt; DataSource {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;get&lt;/SPAN&gt; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;base&lt;/SPAN&gt;.DataSource;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;set&lt;/SPAN&gt; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;if&lt;/SPAN&gt; (!_settingDataSource &amp;amp;&amp;amp; AutoBind) {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;InvalidOperationException&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"When AutoBind is enabled, setting the DataSource property explicitly is not allowed."&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;base&lt;/SPAN&gt;.DataSource = &lt;SPAN style="COLOR: yellow"&gt;value&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: yellow"&gt;protected&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;override&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;void&lt;/SPAN&gt; PerformSelect() {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;if&lt;/SPAN&gt; (AutoBind) {&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;DataBindingEventArgs&lt;/SPAN&gt; args = &lt;SPAN style="COLOR: yellow"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;DataBindingEventArgs&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; OnDataRetrieving(args); &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; _settingDataSource = &lt;SPAN style="COLOR: yellow"&gt;true&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; DataSource = args.DataSource;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; _settingDataSource = &lt;SPAN style="COLOR: yellow"&gt;false&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;base&lt;/SPAN&gt;.PerformSelect();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: yellow"&gt;protected&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;override&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;void&lt;/SPAN&gt; EnsureDataBound() {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;if&lt;/SPAN&gt; (RequiresDataBinding &amp;amp;&amp;amp; (AutoBind || IsBoundUsingDataSourceID)) {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; DataBind();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;The most interesting method is &lt;STRONG&gt;EnsureDataBound. &lt;/STRONG&gt;This method is called from PreRender and the original condition for making the control DataBind is:&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: #ccc 1px solid; BORDER-LEFT: #ccc 1px solid; PADDING-BOTTOM: 2px; PADDING-LEFT: 2px; PADDING-RIGHT: 2px; FONT-FAMILY: Consolas; BACKGROUND: black; COLOR: white; FONT-SIZE: 8pt; BORDER-TOP: #ccc 1px solid; BORDER-RIGHT: #ccc 1px solid; PADDING-TOP: 2px"&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&lt;SPAN style="COLOR: yellow"&gt;if&lt;/SPAN&gt; (&lt;SPAN style="COLOR: yellow"&gt;this&lt;/SPAN&gt;.RequiresDataBinding &amp;amp;&amp;amp; ((&lt;SPAN style="COLOR: yellow"&gt;this&lt;/SPAN&gt;.DataSourceID.Length &amp;gt; 0) || &lt;SPAN style="COLOR: yellow"&gt;this&lt;/SPAN&gt;._requiresBindToNull)) {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;this&lt;/SPAN&gt;.DataBind();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;this&lt;/SPAN&gt;._requiresBindToNull = &lt;SPAN style="COLOR: yellow"&gt;false&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px; FONT-FAMILY: Consolas; FONT-SIZE: 10pt"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;In the case of manually binding this would never be called unless _&lt;STRONG&gt;requiresBindToNull&lt;/STRONG&gt; is true. Our AutoGridView control changes this logic by detecting the AutoBind flag and continues to data bind as usual in PreRender.&lt;/P&gt;
&lt;P&gt;If you interested in when the event gets called you can put some break points in the control's code. What do you think about this alternative?&lt;/P&gt;
&lt;P&gt;Here is a link to the &lt;A href="http://oyugjg.blu.livefilestore.com/y1phr4bXPrRonbzv9yn0DWdv4ELxhaBgg2lbiii9fJO8uvwKS1Fkizp658A3V4ZsIG-iSRu49g1FzK6LZzVI0suWw/AutoGridView.cs?download" mce_href="http://oyugjg.blu.livefilestore.com/y1phr4bXPrRonbzv9yn0DWdv4ELxhaBgg2lbiii9fJO8uvwKS1Fkizp658A3V4ZsIG-iSRu49g1FzK6LZzVI0suWw/AutoGridView.cs?download"&gt;&lt;STRONG&gt;source&lt;/STRONG&gt;&lt;/A&gt;&lt;STRONG&gt;.&lt;BR&gt;&lt;/STRONG&gt;&lt;!--EndFragment--&gt;&lt;!--EndFragment--&gt;&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6884056" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/davidfowler/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/davidfowler/archive/tags/DataBinding/default.aspx">DataBinding</category><category domain="http://weblogs.asp.net/davidfowler/archive/tags/DataControls/default.aspx">DataControls</category></item><item><title>Client side devevelopment in ASP.NET</title><link>http://weblogs.asp.net/davidfowler/archive/2009/01/29/client-side-devevelopment-in-asp-net.aspx</link><pubDate>Thu, 29 Jan 2009 10:09:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6868170</guid><dc:creator>davidfowl</dc:creator><slash:comments>13</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/davidfowler/rsscomments.aspx?PostID=6868170</wfw:commentRss><comments>http://weblogs.asp.net/davidfowler/archive/2009/01/29/client-side-devevelopment-in-asp-net.aspx#comments</comments><description>&lt;P mce_keep="true"&gt;We ASP.NET developers know how much of a pain it is writing javascript in any app we have today because of naming container madness! You've probably done something like this:&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: #ccc 1px solid; BORDER-LEFT: #ccc 1px solid; PADDING-BOTTOM: 2px; PADDING-LEFT: 2px; PADDING-RIGHT: 2px; FONT-FAMILY: Consolas; BACKGROUND: black; COLOR: white; FONT-SIZE: 10pt; BORDER-TOP: #ccc 1px solid; BORDER-RIGHT: #ccc 1px solid; PADDING-TOP: 2px"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: yellow"&gt;function&lt;/SPAN&gt; DoSomeThingCool() {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;var&lt;/SPAN&gt; textBox = document.getElementById(&lt;SPAN style="COLOR: #a31515"&gt;'ct100_contentplaceholder1_TextBox1'&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;or something not so hardcoded&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: #ccc 1px solid; BORDER-LEFT: #ccc 1px solid; PADDING-BOTTOM: 2px; PADDING-LEFT: 2px; PADDING-RIGHT: 2px; FONT-FAMILY: Consolas; BACKGROUND: black; COLOR: white; FONT-SIZE: 10pt; BORDER-TOP: #ccc 1px solid; BORDER-RIGHT: #ccc 1px solid; PADDING-TOP: 2px"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: yellow"&gt;function&lt;/SPAN&gt; DoSomeThingCool() {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;var&lt;/SPAN&gt; textBox = document.getElementById(&lt;SPAN style="COLOR: #a31515"&gt;'&amp;lt;%= TextBox1.ClientID %&amp;gt;'&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;A href="http://blog.osbornm.com/" mce_href="http://blog.osbornm.com"&gt;Matthew Osborn&lt;/A&gt;, QA on the ASP.NET team&amp;nbsp;has a great &lt;A href="http://blog.osbornm.com/archive/2009/01/06/asp.net-4.0-clientid-overview-again.aspx" mce_href="http://blog.osbornm.com/archive/2009/01/06/asp.net-4.0-clientid-overview-again.aspx"&gt;post&lt;/A&gt; on a new ASP.NET 4.0 feature that gives developers more control over how ClientIDs are generated.&lt;!--EndFragment--&gt;&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6868170" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/davidfowler/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/davidfowler/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/davidfowler/archive/tags/Javascript/default.aspx">Javascript</category></item><item><title>Follow me on twitter!</title><link>http://weblogs.asp.net/davidfowler/archive/2009/01/25/follow-me-on-twitter.aspx</link><pubDate>Sun, 25 Jan 2009 09:52:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6859847</guid><dc:creator>davidfowl</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/davidfowler/rsscomments.aspx?PostID=6859847</wfw:commentRss><comments>http://weblogs.asp.net/davidfowler/archive/2009/01/25/follow-me-on-twitter.aspx#comments</comments><description>&lt;P mce_keep="true"&gt;I've finally got sucked into twitter after avoiding it for many months. Check me out:&lt;BR&gt;&lt;BR&gt;&lt;A href="http://twitter.com/davidfowl/"&gt;http://twitter.com/davidfowl/&lt;/A&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;Since my name is so common it really annoys me when I sign up for any new service because I have to get accept some partially mangled version of my name (i.e. davidfowl).&lt;/P&gt;
&lt;P mce_keep="true"&gt;Anyways, stay tuned!&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6859847" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/davidfowler/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/davidfowler/archive/tags/twitter/default.aspx">twitter</category></item><item><title>Dynamic ListView LayoutTemplate</title><link>http://weblogs.asp.net/davidfowler/archive/2008/12/14/dynamic-listview-layouttemplate.aspx</link><pubDate>Sun, 14 Dec 2008 20:34:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6784962</guid><dc:creator>davidfowl</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/davidfowler/rsscomments.aspx?PostID=6784962</wfw:commentRss><comments>http://weblogs.asp.net/davidfowler/archive/2008/12/14/dynamic-listview-layouttemplate.aspx#comments</comments><description>&lt;P mce_keep="true"&gt;There are times when you want to let the user change layout&amp;nbsp;dynamically. You can use css to do this but lets look at what the ListView control offers. To get started with the ListView you need a &lt;STRONG&gt;LayoutTemplate&lt;/STRONG&gt; and &lt;STRONG&gt;ItemTemplate&lt;/STRONG&gt;. &lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: #ddd 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #ddd 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10pt; BACKGROUND: black; PADDING-BOTTOM: 2px; BORDER-LEFT: #ddd 1px solid; COLOR: white; PADDING-TOP: 2px; BORDER-BOTTOM: #ddd 1px solid; FONT-FAMILY: Consolas"&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;lt;&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;ListView&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;runat&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"server"&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;ID&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"listView"&lt;/SPAN&gt;&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: yellow"&gt;LayoutTemplate&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;PlaceHolder&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;runat&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"server"&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;ID&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"itemPlaceholder"&lt;/SPAN&gt;&amp;gt;&amp;lt;/&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;PlaceHolder&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;SPAN style="COLOR: yellow"&gt;LayoutTemplate&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: yellow"&gt;ItemTemplate&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;# Eval(&lt;SPAN style="COLOR: #ff8000"&gt;"CategoryName"&lt;/SPAN&gt;) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;SPAN style="COLOR: yellow"&gt;ItemTemplate&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;lt;/&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;ListView&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;The ListView replaces the control with ID="itemPlaceholder" with the zero or more instances of the&amp;nbsp;Selected/Alternating/ItemTemplate. &lt;BR&gt;&lt;BR&gt;There is a method on &lt;STRONG&gt;TemplateControl (LoadTemplate) &lt;/STRONG&gt;which allows users to dynamically load a user control as a &lt;STRONG&gt;ITemplate&lt;/STRONG&gt;. Lets use this to load our LayoutTemplate from a user control.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;User Control:&lt;BR&gt;&lt;IMG style="WIDTH: 163px; HEIGHT: 39px" height=39 src="http://weblogs.asp.net/blogs/davidfowler/layout.bmp" width=163 mce_src="http://weblogs.asp.net/blogs/davidfowler/layout.bmp"&gt;&lt;BR&gt;&lt;BR&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: #ddd 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #ddd 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10pt; BACKGROUND: black; PADDING-BOTTOM: 2px; BORDER-LEFT: #ddd 1px solid; COLOR: white; PADDING-TOP: 2px; BORDER-BOTTOM: #ddd 1px solid; FONT-FAMILY: Consolas"&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;@ &lt;SPAN style="COLOR: yellow"&gt;Control&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;Language&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"C#"&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;AutoEventWireup&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"true"&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;CodeBehind&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"FlowLayout.ascx.cs"&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;Inherits&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"ListViewLayouts.FlowLayout"&lt;/SPAN&gt; &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;lt;&lt;SPAN style="COLOR: yellow"&gt;div&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;id&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"flow"&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;PlaceHolder&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;runat&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"server"&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;ID&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"itemPlaceholder"&lt;/SPAN&gt;&amp;gt;&amp;lt;/&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;PlaceHolder&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;lt;/&lt;SPAN style="COLOR: yellow"&gt;div&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;STRONG&gt;Code Behind:&lt;/STRONG&gt;&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: #ddd 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #ddd 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10pt; BACKGROUND: black; PADDING-BOTTOM: 2px; BORDER-LEFT: #ddd 1px solid; COLOR: white; PADDING-TOP: 2px; BORDER-BOTTOM: #ddd 1px solid; FONT-FAMILY: Consolas"&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: yellow"&gt;protected&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;void&lt;/SPAN&gt; Page_Init(&lt;SPAN style="COLOR: yellow"&gt;object&lt;/SPAN&gt; sender, &lt;SPAN style="COLOR: #2b91af"&gt;EventArgs&lt;/SPAN&gt; e) {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; listView.LayoutTemplate = LoadTemplate(&lt;SPAN style="COLOR: #ff8000"&gt;"~/ListViewLayouts/FlowLayout.ascx"&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;}&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;When we run the page we get the following exception:&lt;BR&gt;&lt;STRONG&gt;&lt;EM&gt;&lt;BR&gt;An item placeholder must be specified on ListView 'listView'. Specify an item placeholder by setting a control's ID property to "itemPlaceholder". The item placeholder control must also specify runat="server".&lt;/EM&gt; &lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;The problem here is that user controls are naming containers and when the ListView internally tries to find the control with ID="itemPlaceholder" it fails. So what is the id of the itemPlaceholder? We'll we could start guessing that it might be something like&amp;nbsp;&lt;STRONG&gt;ctl001$itemPlaceholder&lt;/STRONG&gt;, but that doesn't seem like a good solution. Instead we can create our own template that will allow us to specify the&amp;nbsp;ID of the user control&amp;nbsp;so that the itemPlaceholderID is more predictable.&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: #ddd 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #ddd 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10pt; BACKGROUND: black; PADDING-BOTTOM: 2px; BORDER-LEFT: #ddd 1px solid; COLOR: white; PADDING-TOP: 2px; BORDER-BOTTOM: #ddd 1px solid; FONT-FAMILY: Consolas"&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: yellow"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;CustomTemplate&lt;/SPAN&gt; : &lt;SPAN style="COLOR: #2b91af"&gt;ITemplate&lt;/SPAN&gt; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;string&lt;/SPAN&gt; _virtualPath;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;string&lt;/SPAN&gt; _controlID;&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;public&lt;/SPAN&gt; CustomTemplate(&lt;SPAN style="COLOR: yellow"&gt;string&lt;/SPAN&gt; virtualPath, &lt;SPAN style="COLOR: yellow"&gt;string&lt;/SPAN&gt; controlID) {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _virtualPath = virtualPath;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _controlID = controlID;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;void&lt;/SPAN&gt; InstantiateIn(&lt;SPAN style="COLOR: #2b91af"&gt;Control&lt;/SPAN&gt; container) {&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Control&lt;/SPAN&gt; control = (&lt;SPAN style="COLOR: #2b91af"&gt;Control&lt;/SPAN&gt;)&lt;SPAN style="COLOR: #2b91af"&gt;BuildManager&lt;/SPAN&gt;.CreateInstanceFromVirtualPath(_virtualPath, &lt;SPAN style="COLOR: yellow"&gt;typeof&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;Control&lt;/SPAN&gt;));&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; control.ID = _controlID;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; container.Controls.Add(control);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;}&lt;/P&gt;&lt;/DIV&gt;&lt;BR&gt;This template gives us the opportunity to specify a controlID for the user control we are going to load. Now we instantiate a new CustomTemplate and specify the control ID as well as path to the user control.&lt;BR&gt;&lt;BR&gt;
&lt;DIV style="BORDER-RIGHT: #ddd 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #ddd 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10pt; BACKGROUND: black; PADDING-BOTTOM: 2px; BORDER-LEFT: #ddd 1px solid; COLOR: white; PADDING-TOP: 2px; BORDER-BOTTOM: #ddd 1px solid; FONT-FAMILY: Consolas"&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: yellow"&gt;protected&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;void&lt;/SPAN&gt; Page_Load(&lt;SPAN style="COLOR: yellow"&gt;object&lt;/SPAN&gt; sender, &lt;SPAN style="COLOR: #2b91af"&gt;EventArgs&lt;/SPAN&gt; e) {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; listView.LayoutTemplate = &lt;SPAN style="COLOR: yellow"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;CustomTemplate&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #ff8000"&gt;"~/ListViewLayouts/FlowLayout.ascx"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #ff8000"&gt;"flowLayout"&lt;/SPAN&gt;);&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;}&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;Don't forget to set the &lt;STRONG&gt;ItemPlaceHolderID&lt;/STRONG&gt; proprety on the ListView.&lt;BR&gt;&lt;BR&gt;&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: #ddd 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #ddd 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10pt; BACKGROUND: black; PADDING-BOTTOM: 2px; BORDER-LEFT: #ddd 1px solid; COLOR: white; PADDING-TOP: 2px; BORDER-BOTTOM: #ddd 1px solid; FONT-FAMILY: Consolas"&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;lt;&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;ListView&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;runat&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"server"&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;ID&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"listView"&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;ItemPlaceholderID&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"flowLayout$itemPlaceholder"&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: yellow"&gt;ItemTemplate&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;# Eval(&lt;SPAN style="COLOR: #ff8000"&gt;"CategoryName"&lt;/SPAN&gt;) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;SPAN style="COLOR: yellow"&gt;ItemTemplate&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;lt;/&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;ListView&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;Now we can load the &lt;STRONG&gt;LayoutTemplate&lt;/STRONG&gt;&amp;nbsp;at runtime.&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6784962" width="1" height="1"&gt;</description></item><item><title>AutoFieldGenerators</title><link>http://weblogs.asp.net/davidfowler/archive/2008/12/14/autofieldgenerator.aspx</link><pubDate>Sun, 14 Dec 2008 08:01:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6783754</guid><dc:creator>davidfowl</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/davidfowler/rsscomments.aspx?PostID=6783754</wfw:commentRss><comments>http://weblogs.asp.net/davidfowler/archive/2008/12/14/autofieldgenerator.aspx#comments</comments><description>&lt;P mce_keep="true"&gt;In 3.5 SP1 we added new properties to GridView and DetailsView which allows the page developer to change the way AutoGenerateColumns creates its columns. This feature is well know in &lt;A href="http://www.asp.net/dynamicdata" mce_href="http://www.asp.net/dynamicdata"&gt;Dynamic Data&lt;/A&gt;, but it is not tied to this technology. Dynamic Data takes advantage of&amp;nbsp;this by looking at the meta data that users set on properties to generate columns. &lt;BR&gt;&lt;BR&gt;You too can roll your own IAutoFieldGenerator. Lets look at the interface:&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: #ddd 1px solid; BORDER-LEFT: #ddd 1px solid; PADDING-BOTTOM: 2px; PADDING-LEFT: 2px; PADDING-RIGHT: 2px; FONT-FAMILY: Consolas; BACKGROUND: black; COLOR: white; FONT-SIZE: 10pt; BORDER-TOP: #ddd 1px solid; BORDER-RIGHT: #ddd 1px solid; PADDING-TOP: 2px"&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: yellow"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;interface&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IAutoFieldGenerator&lt;/SPAN&gt; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;ICollection&lt;/SPAN&gt; GenerateFields(&lt;SPAN style="COLOR: #2b91af"&gt;Control&lt;/SPAN&gt; control);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;}&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;The interface itself is pretty weird but it gets the job done. &lt;STRONG&gt;GenerateFields&lt;/STRONG&gt; takes the control that we're generating the fields for, and expects to get some ICollection of stuff back. If we&amp;nbsp;had the&amp;nbsp;chance to redo this interface we'd probaby rewrite it to be like this:&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: #ddd 1px solid; BORDER-LEFT: #ddd 1px solid; PADDING-BOTTOM: 2px; PADDING-LEFT: 2px; PADDING-RIGHT: 2px; FONT-FAMILY: Consolas; BACKGROUND: black; COLOR: white; FONT-SIZE: 10pt; BORDER-TOP: #ddd 1px solid; BORDER-RIGHT: #ddd 1px solid; PADDING-TOP: 2px"&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: yellow"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;interface&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IAutoFieldGenerator&lt;/SPAN&gt; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;IEnumerable&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;DataControlField&lt;/SPAN&gt;&amp;gt; GenerateFields(&lt;SPAN style="COLOR: #2b91af"&gt;Control&lt;/SPAN&gt; control);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;}&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;Now it's clear what we expect to get back, but thats besides the point. Lets implement our own.&lt;BR&gt;&lt;BR&gt;&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: #ddd 1px solid; BORDER-LEFT: #ddd 1px solid; PADDING-BOTTOM: 2px; PADDING-LEFT: 2px; PADDING-RIGHT: 2px; FONT-FAMILY: Consolas; BACKGROUND: black; COLOR: white; FONT-SIZE: 10pt; BORDER-TOP: #ddd 1px solid; BORDER-RIGHT: #ddd 1px solid; PADDING-TOP: 2px"&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: yellow"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ColumnGenerator&lt;/SPAN&gt; : &lt;SPAN style="COLOR: #2b91af"&gt;IAutoFieldGenerator&lt;/SPAN&gt; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: yellow"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IEnumerable&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: yellow"&gt;string&lt;/SPAN&gt;&amp;gt; _columns;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: yellow"&gt;public&lt;/SPAN&gt; ColumnGenerator(&lt;SPAN style="COLOR: #2b91af"&gt;IEnumerable&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: yellow"&gt;string&lt;/SPAN&gt;&amp;gt; columns) {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; _columns = columns ?? &lt;SPAN style="COLOR: #2b91af"&gt;Enumerable&lt;/SPAN&gt;.Empty&amp;lt;&lt;SPAN style="COLOR: yellow"&gt;string&lt;/SPAN&gt;&amp;gt;();&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;}&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: yellow"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ICollection&lt;/SPAN&gt; GenerateFields(&lt;SPAN style="COLOR: #2b91af"&gt;Control&lt;/SPAN&gt; control) {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;return&lt;/SPAN&gt; (&lt;SPAN style="COLOR: yellow"&gt;from&lt;/SPAN&gt; column &lt;SPAN style="COLOR: yellow"&gt;in&lt;/SPAN&gt; _columns&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;select&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;BoundField&lt;/SPAN&gt; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SortExpression = column,&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HeaderText = column,&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DataField = column&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }).ToArray();&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;}&lt;/P&gt;&lt;/DIV&gt;&lt;BR&gt;We're going to pass a set of column names&amp;nbsp;to our generator that just creates bound fields with the column's name. To make use of our new generator we can just set it like this:&lt;BR&gt;&lt;BR&gt;&lt;STRONG&gt;GridView&lt;BR&gt;&lt;/STRONG&gt;&lt;BR&gt;
&lt;DIV style="BORDER-BOTTOM: #ddd 1px solid; BORDER-LEFT: #ddd 1px solid; PADDING-BOTTOM: 2px; PADDING-LEFT: 2px; PADDING-RIGHT: 2px; FONT-FAMILY: Consolas; BACKGROUND: black; COLOR: white; FONT-SIZE: 10pt; BORDER-TOP: #ddd 1px solid; BORDER-RIGHT: #ddd 1px solid; PADDING-TOP: 2px"&gt;
&lt;P style="MARGIN: 0px"&gt;gridView1.ColumnsGenerator = &lt;SPAN style="COLOR: yellow"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ColumnGenerator&lt;/SPAN&gt;(Columns);&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;STRONG&gt;DetailsView&lt;/STRONG&gt;&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: #ddd 1px solid; BORDER-LEFT: #ddd 1px solid; PADDING-BOTTOM: 2px; PADDING-LEFT: 2px; PADDING-RIGHT: 2px; FONT-FAMILY: Consolas; BACKGROUND: black; COLOR: white; FONT-SIZE: 10pt; BORDER-TOP: #ddd 1px solid; BORDER-RIGHT: #ddd 1px solid; PADDING-TOP: 2px"&gt;
&lt;P style="MARGIN: 0px"&gt;detailsView1.RowsGenerator = &lt;SPAN style="COLOR: yellow"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ColumnGenerator&lt;/SPAN&gt;(Columns);&lt;/P&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;
&lt;P&gt;You can do alot of cool things with these generators. Some things I can think of off the top of my head:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Hide/Show columns dynamically based on metadata (like what we do with dynamic data)&lt;/LI&gt;
&lt;LI&gt;Hide/Show columns based on permissions&lt;/LI&gt;
&lt;LI&gt;Create a configurable UI that allows users to hide or show columns based on their preferences.&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;I've written a sample that allows you to hide or show columns based on a selectable UI. You can download it here:&lt;BR&gt;&lt;BR&gt;&lt;A href="http://oyugjg.blu.livefilestore.com/y1pf8xrm5H8UZkhLkj1XfZ4szalx85Z8IAK7WxViSrSvdMOGBVOiQ0KBZMecFovM31f3ShTpvIrC45NQHJoIsfFcA/AutoFieldGenerator.zip?download" mce_href="http://oyugjg.blu.livefilestore.com/y1pf8xrm5H8UZkhLkj1XfZ4szalx85Z8IAK7WxViSrSvdMOGBVOiQ0KBZMecFovM31f3ShTpvIrC45NQHJoIsfFcA/AutoFieldGenerator.zip?download"&gt;AutoFieldGenerator.zip&lt;/A&gt;&lt;BR&gt;&lt;BR&gt;Here is a screen shot of it running.&lt;BR&gt;&lt;BR&gt;&lt;A href="http://weblogs.asp.net/blogs/davidfowler/autogenerator.jpg" mce_href="http://weblogs.asp.net/blogs/davidfowler/autogenerator.jpg" blogs davidfowler autogenerator.jpg?&gt;&lt;IMG style="WIDTH: 498px; HEIGHT: 300px" border=0 src="http://weblogs.asp.net/blogs/davidfowler/autogenerator.jpg" width=498 height=300 mce_src="http://weblogs.asp.net/blogs/davidfowler/autogenerator.jpg"&gt;&lt;/A&gt;&lt;/P&gt;&lt;!--EndFragment--&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6783754" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/davidfowler/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/davidfowler/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/davidfowler/archive/tags/Dynamic+Data/default.aspx">Dynamic Data</category><category domain="http://weblogs.asp.net/davidfowler/archive/tags/DataControls/default.aspx">DataControls</category></item><item><title>How &lt;%# Bind %&gt; Works</title><link>http://weblogs.asp.net/davidfowler/archive/2008/12/13/how-bind-works.aspx</link><pubDate>Sat, 13 Dec 2008 16:57:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6782397</guid><dc:creator>davidfowl</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/davidfowler/rsscomments.aspx?PostID=6782397</wfw:commentRss><comments>http://weblogs.asp.net/davidfowler/archive/2008/12/13/how-bind-works.aspx#comments</comments><description>&lt;P mce_keep="true"&gt;In my last post I spoke about 2-way databinding and how it can be used to extract values from control properties. How does this all work? Lets take a look at a page with 2-way databinding:&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: #ddd 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #ddd 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10pt; BACKGROUND: black; PADDING-BOTTOM: 2px; BORDER-LEFT: #ddd 1px solid; COLOR: white; PADDING-TOP: 2px; BORDER-BOTTOM: #ddd 1px solid; FONT-FAMILY: Consolas"&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;lt;&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;LinqDataSource&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;ID&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"productsSource"&lt;/SPAN&gt; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;runat&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"server"&lt;/SPAN&gt; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;ContextTypeName&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"FowlerSamples.NorthwindDataContext"&lt;/SPAN&gt; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;EnableDelete&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"True"&lt;/SPAN&gt; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;EnableInsert&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"True"&lt;/SPAN&gt; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;EnableUpdate&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"True"&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;TableName&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"Products"&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;lt;/&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;LinqDataSource&lt;/SPAN&gt;&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;lt;&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;GridView&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;ID&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"products"&lt;/SPAN&gt; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;DataKeyNames&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"ProductID,CategoryID"&lt;/SPAN&gt; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;AutoGenerateColumns&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"False"&lt;/SPAN&gt; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;runat&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"server"&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;DataSourceID&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"productsSource"&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: yellow"&gt;Columns&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;CommandField&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;ShowEditButton&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"True"&lt;/SPAN&gt; /&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;BoundField&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;DataField&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"ProductName"&lt;/SPAN&gt; /&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;TemplateField&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: yellow"&gt;EditItemTemplate&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;LinqDataSource&lt;/SPAN&gt; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;ID&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"categoriesSource"&lt;/SPAN&gt; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;runat&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"server"&lt;/SPAN&gt; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;ContextTypeName&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"FowlerSamples.NorthwindDataContext"&lt;/SPAN&gt; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;TableName&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"Categories"&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;AutoGenerateWhereClause&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"true"&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;LinqDataSource&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;DropDownList&lt;/SPAN&gt; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;runat&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"server"&lt;/SPAN&gt; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;ID&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"categories"&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;DataSourceID&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"categoriesSource"&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;DataTextField&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"CategoryName"&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;DataValueField&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"CategoryID"&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;SelectedValue&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;# Bind("CategoryID") &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff8000"&gt;'&lt;/SPAN&gt;&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;DropDownList&lt;/SPAN&gt;&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;SPAN style="COLOR: yellow"&gt;EditItemTemplate&lt;/SPAN&gt;&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;TemplateField&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;SPAN style="COLOR: yellow"&gt;Columns&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;lt;/&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;GridView&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;&lt;/DIV&gt;
&lt;P mce_keep="true"&gt;&lt;!--EndFragment--&gt;&amp;nbsp;In the above example, the GridView has a template field with an &lt;STRONG&gt;EditItemTemplate &lt;/STRONG&gt;that has a &lt;STRONG&gt;DropDownList &lt;/STRONG&gt;that is 2-way databound. We're going to introduce a small error in the page in order to see what the generated code looks like:&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: #ddd 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #ddd 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10pt; BACKGROUND: black; PADDING-BOTTOM: 2px; BORDER-LEFT: #ddd 1px solid; COLOR: white; PADDING-TOP: 2px; BORDER-BOTTOM: #ddd 1px solid; FONT-FAMILY: Consolas"&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;lt;&lt;SPAN style="COLOR: yellow"&gt;EditItemTemplate&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;LinqDataSource&lt;/SPAN&gt; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;ID&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"categoriesSource"&lt;/SPAN&gt; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;runat&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"server"&lt;/SPAN&gt; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;ContextTypeName&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"FowlerSamples.NorthwindDataContext"&lt;/SPAN&gt; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;TableName&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"Categories"&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;AutoGenerateWhereClause&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"true"&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;LinqDataSource&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;DropDownList&lt;/SPAN&gt; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;runat&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"server"&lt;/SPAN&gt; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;ID&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"categories"&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;DataSourceID&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"categoriesSource"&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;DataTextField&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"CategoryName"&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;DataValueField&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"CategoryID"&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;SelectedValue&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;# Bind("CategoryID") &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff8000"&gt;'&lt;/SPAN&gt;&amp;gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;DropDownList&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;# Eval(3) &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;lt;/&lt;SPAN style="COLOR: yellow"&gt;EditItemTemplate&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;&lt;/DIV&gt;&lt;BR&gt;When we try to run this page we'll get a compile error and the famous &lt;STRONG&gt;ASP.NET&lt;/STRONG&gt; YSOD(Yellow Screen of Death):&lt;BR&gt;&lt;BR&gt;&lt;IMG src="http://weblogs.asp.net/blogs/davidfowler/ysod.bmp" mce_src="http://weblogs.asp.net/blogs/davidfowler/ysod.bmp"&gt;&lt;BR&gt;&lt;BR&gt;Click on &lt;STRONG&gt;Show Complete Compilation Source,&lt;/STRONG&gt;&amp;nbsp;if your curious about how ASP.NET converts your the markup to code.&lt;BR&gt;&lt;BR&gt;When examining the source, we see a rather interesting method:&lt;BR&gt;&lt;BR&gt;
&lt;DIV style="BORDER-RIGHT: #ddd 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #ddd 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10pt; BACKGROUND: black; PADDING-BOTTOM: 2px; BORDER-LEFT: #ddd 1px solid; COLOR: white; PADDING-TOP: 2px; BORDER-BOTTOM: #ddd 1px solid; FONT-FAMILY: Consolas"&gt;
&lt;P style="MARGIN: 0px"&gt;[System.Diagnostics.&lt;SPAN style="COLOR: #2b91af"&gt;DebuggerNonUserCodeAttribute&lt;/SPAN&gt;()]&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: yellow"&gt;public&lt;/SPAN&gt; System.Collections.Specialized.&lt;SPAN style="COLOR: #2b91af"&gt;IOrderedDictionary&lt;/SPAN&gt; @__ExtractValues__control8(System.Web.UI.&lt;SPAN style="COLOR: #2b91af"&gt;Control&lt;/SPAN&gt; @__container) {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.Collections.Specialized.&lt;SPAN style="COLOR: #2b91af"&gt;OrderedDictionary&lt;/SPAN&gt; @__table;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.Web.UI.WebControls.&lt;SPAN style="COLOR: #2b91af"&gt;DropDownList&lt;/SPAN&gt; categories;&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; categories = ((System.Web.UI.WebControls.&lt;SPAN style="COLOR: #2b91af"&gt;DropDownList&lt;/SPAN&gt;)(@__container.FindControl(&lt;SPAN style="COLOR: #ff8000"&gt;"categories"&lt;/SPAN&gt;)));&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @__table = &lt;SPAN style="COLOR: yellow"&gt;new&lt;/SPAN&gt; System.Collections.Specialized.&lt;SPAN style="COLOR: #2b91af"&gt;OrderedDictionary&lt;/SPAN&gt;();&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;if&lt;/SPAN&gt; ((categories != &lt;SPAN style="COLOR: yellow"&gt;null&lt;/SPAN&gt;)) {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @__table[&lt;SPAN style="COLOR: #ff8000"&gt;"CategoryID"&lt;/SPAN&gt;] = categories.SelectedValue;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;return&lt;/SPAN&gt; @__table;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;}&lt;/P&gt;&lt;/DIV&gt;&lt;BR&gt;As you can see in the above method, an OrderedDictionary is created and the SelectedValue property of the DropDownList is pushed into the "CategoryID" field. But how does this get all the way to the data control? Each control has a BuildControl method associated with it, if we examine the BuildControl method for the TemplateField it becomes a bit more clear how things get hooked up.&lt;BR&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: #ddd 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #ddd 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10pt; BACKGROUND: black; PADDING-BOTTOM: 2px; BORDER-LEFT: #ddd 1px solid; COLOR: white; PADDING-TOP: 2px; BORDER-BOTTOM: #ddd 1px solid; FONT-FAMILY: Consolas"&gt;[System.Diagnostics.&lt;SPAN style="COLOR: #2b91af"&gt;DebuggerNonUserCodeAttribute&lt;/SPAN&gt;()] 
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: yellow"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;global&lt;/SPAN&gt;::System.Web.UI.WebControls.&lt;SPAN style="COLOR: #2b91af"&gt;TemplateField&lt;/SPAN&gt; @__BuildControl__control7() {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;global&lt;/SPAN&gt;::System.Web.UI.WebControls.&lt;SPAN style="COLOR: #2b91af"&gt;TemplateField&lt;/SPAN&gt; @__ctrl;&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @__ctrl = &lt;SPAN style="COLOR: yellow"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;global&lt;/SPAN&gt;::System.Web.UI.WebControls.&lt;SPAN style="COLOR: #2b91af"&gt;TemplateField&lt;/SPAN&gt;();&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @__ctrl.EditItemTemplate = &lt;SPAN style="COLOR: yellow"&gt;new&lt;/SPAN&gt; System.Web.UI.&lt;SPAN style="COLOR: #2b91af"&gt;CompiledBindableTemplateBuilder&lt;/SPAN&gt;(&lt;SPAN style="COLOR: yellow"&gt;new&lt;/SPAN&gt; System.Web.UI.&lt;SPAN style="COLOR: #2b91af"&gt;BuildTemplateMethod&lt;/SPAN&gt;(&lt;SPAN style="COLOR: yellow"&gt;this&lt;/SPAN&gt;.@__BuildControl__control8), &lt;SPAN style="COLOR: yellow"&gt;new&lt;/SPAN&gt; System.Web.UI.&lt;SPAN style="COLOR: #2b91af"&gt;ExtractTemplateValuesMethod&lt;/SPAN&gt;(&lt;SPAN style="COLOR: yellow"&gt;this&lt;/SPAN&gt;.@__ExtractValues__control8));&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;return&lt;/SPAN&gt; @__ctrl;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;}&lt;/P&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;
&lt;P&gt;The EditItemTemplate property is of type &lt;STRONG&gt;&lt;A class="" href="http://msdn.microsoft.com/en-us/library/system.web.ui.itemplate.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.web.ui.itemplate.aspx"&gt;ITemplate&lt;/A&gt;&lt;/STRONG&gt;. &lt;STRONG&gt;CompiledBindableTemplate &lt;/STRONG&gt;implements both &lt;STRONG&gt;ITemplate &lt;/STRONG&gt;and &lt;STRONG&gt;IBindableTempalte&lt;/STRONG&gt;.&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: #ddd 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #ddd 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10pt; BACKGROUND: black; PADDING-BOTTOM: 2px; BORDER-LEFT: #ddd 1px solid; COLOR: white; PADDING-TOP: 2px; BORDER-BOTTOM: #ddd 1px solid; FONT-FAMILY: Consolas"&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: yellow"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;interface&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IBindableTemplate&lt;/SPAN&gt; : &lt;SPAN style="COLOR: #2b91af"&gt;ITemplate&lt;/SPAN&gt; {&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;IOrderedDictionary&lt;/SPAN&gt; ExtractValues(&lt;SPAN style="COLOR: #2b91af"&gt;Control&lt;/SPAN&gt; container);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;}&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;!--EndFragment--&gt;It's slowly coming together. So lets put together what we're learnt so far:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Code Gen&amp;nbsp;creates &lt;STRONG&gt;ExtractValues&lt;/STRONG&gt; method that returns the dictionary of values for each &lt;STRONG&gt;ITemplate&lt;/STRONG&gt;&amp;nbsp;that has a &lt;STRONG&gt;Bind&lt;/STRONG&gt; expression.&lt;/LI&gt;
&lt;LI&gt;The &lt;STRONG&gt;BuildControl&lt;/STRONG&gt; method&amp;nbsp;for the ITemplate's container (&lt;STRONG&gt;TemplateField&lt;/STRONG&gt; in this case)&amp;nbsp;assigns a new &lt;STRONG&gt;CompiledBindableTemplate&lt;/STRONG&gt; to an &lt;STRONG&gt;ITemplate &lt;/STRONG&gt;(EditItemTemplate in this case)&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;CompiledBindableTemplate&lt;/STRONG&gt; implements &lt;STRONG&gt;IBindableTemplate&lt;/STRONG&gt;, which has a method, &lt;STRONG&gt;ExtractValues &lt;/STRONG&gt;which returns the dictionary given a container.&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;It almost all makes sense now. Each data control uses this mechanism to extract values from template fields with 2-way databinding expressions.&lt;BR&gt;What can you do with your new found knowledge?&lt;BR&gt;&lt;BR&gt;&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: #ddd 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #ddd 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10pt; BACKGROUND: black; PADDING-BOTTOM: 2px; BORDER-LEFT: #ddd 1px solid; COLOR: white; PADDING-TOP: 2px; BORDER-BOTTOM: #ddd 1px solid; FONT-FAMILY: Consolas"&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;lt;&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;FormView&lt;/SPAN&gt; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;runat&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"server"&lt;/SPAN&gt; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;ID&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"formView"&lt;/SPAN&gt; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;DefaultMode&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"Edit"&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: yellow"&gt;EditItemTemplate&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;TextBox&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;ID&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"textBox"&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;runat&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"server"&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;Text&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;# Bind("Name") &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff8000"&gt;'&lt;/SPAN&gt;&amp;gt;&amp;lt;/&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;TextBox&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;TextBox&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;ID&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"textBox1"&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;runat&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"server"&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;Text&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;# Bind("Age") &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff8000"&gt;'&lt;/SPAN&gt;&amp;gt;&amp;lt;/&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;TextBox&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;Button&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;runat&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"server"&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;ID&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"updateButton"&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;CommandName&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"Update"&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;Text&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"Update"&lt;/SPAN&gt; /&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;SPAN style="COLOR: yellow"&gt;EditItemTemplate&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;lt;/&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;FormView&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;&lt;/DIV&gt;&lt;BR&gt;And the code behind:&lt;BR&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: #ddd 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #ddd 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10pt; BACKGROUND: black; PADDING-BOTTOM: 2px; BORDER-LEFT: #ddd 1px solid; COLOR: white; PADDING-TOP: 2px; BORDER-BOTTOM: #ddd 1px solid; FONT-FAMILY: Consolas"&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: yellow"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Person&lt;/SPAN&gt; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;string&lt;/SPAN&gt; Name { &lt;SPAN style="COLOR: yellow"&gt;get&lt;/SPAN&gt;; &lt;SPAN style="COLOR: yellow"&gt;set&lt;/SPAN&gt;; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;int&lt;/SPAN&gt; Age { &lt;SPAN style="COLOR: yellow"&gt;get&lt;/SPAN&gt;; &lt;SPAN style="COLOR: yellow"&gt;set&lt;/SPAN&gt;; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;}&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: yellow"&gt;protected&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;void&lt;/SPAN&gt; Page_Load() {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; formView.ItemUpdating += formView_ItemUpdating;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;SPAN style="COLOR: yellow"&gt;if&lt;/SPAN&gt; (!IsPostBack) {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;formView.DataSource = &lt;SPAN style="COLOR: yellow"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Person&lt;/SPAN&gt;[] { &lt;SPAN style="COLOR: yellow"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Person&lt;/SPAN&gt; { Name = &lt;SPAN style="COLOR: #ff8000"&gt;"David"&lt;/SPAN&gt;, Age = 22 } };&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; formView.DataBind();&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;}&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: yellow"&gt;protected&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;void&lt;/SPAN&gt; formView_ItemUpdating(&lt;SPAN style="COLOR: yellow"&gt;object&lt;/SPAN&gt; sender, &lt;SPAN style="COLOR: #2b91af"&gt;FormViewUpdateEventArgs&lt;/SPAN&gt; e) {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;IBindableTemplate&lt;/SPAN&gt; template = formView.EditItemTemplate &lt;SPAN style="COLOR: yellow"&gt;as&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IBindableTemplate&lt;/SPAN&gt;;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;if&lt;/SPAN&gt; (template != &lt;SPAN style="COLOR: yellow"&gt;null&lt;/SPAN&gt;) {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;IOrderedDictionary&lt;/SPAN&gt; values = template.ExtractValues(formView);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Response.Write(values[&lt;SPAN style="COLOR: #ff8000"&gt;"Name"&lt;/SPAN&gt;]);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Response.Write(values[&lt;SPAN style="COLOR: #ff8000"&gt;"Age"&lt;/SPAN&gt;]);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;}&lt;/P&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;&lt;BR&gt;How cool is that? &lt;STRONG&gt;:)&lt;/STRONG&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6782397" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/davidfowler/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/davidfowler/archive/tags/DataBinding/default.aspx">DataBinding</category><category domain="http://weblogs.asp.net/davidfowler/archive/tags/DataControls/default.aspx">DataControls</category></item><item><title>Getting your data out of the data controls</title><link>http://weblogs.asp.net/davidfowler/archive/2008/12/12/getting-your-data-out-of-the-data-controls.aspx</link><pubDate>Fri, 12 Dec 2008 23:03:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6780118</guid><dc:creator>davidfowl</dc:creator><slash:comments>27</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/davidfowler/rsscomments.aspx?PostID=6780118</wfw:commentRss><comments>http://weblogs.asp.net/davidfowler/archive/2008/12/12/getting-your-data-out-of-the-data-controls.aspx#comments</comments><description>&lt;P&gt;After you've created a layout with a data control of your choice and eventually bound them to some datasource, you almost always want to get that data back out of them(and why wouldn't you, its your data). One thing you'll learn about developing asp.net webpages is that its like petting a porcupine (yikes). If you go with the flow, you probably won't get stuck, but the moment you try to go against the grain you end up with a hand full of thorns. &lt;BR&gt;&lt;BR&gt;Most of the data controls have events ending in &lt;STRONG&gt;ing&lt;/STRONG&gt; and &lt;STRONG&gt;ed e.g. RowUpdating, RowUpdated&lt;/STRONG&gt; etc. In most of the event handlers of the &lt;STRONG&gt;ing&lt;/STRONG&gt; events it is easy to get the values from the event args.&lt;!--EndFragment--&gt;&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: #ddd 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #ddd 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10pt; BACKGROUND: black; PADDING-BOTTOM: 2px; BORDER-LEFT: #ddd 1px solid; COLOR: white; PADDING-TOP: 2px; BORDER-BOTTOM: #ddd 1px solid; FONT-FAMILY: Consolas"&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: yellow"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;void&lt;/SPAN&gt; OnGridViewRowUpdating(&lt;SPAN style="COLOR: yellow"&gt;object&lt;/SPAN&gt; sender, &lt;SPAN style="COLOR: #2b91af"&gt;GridViewUpdateEventArgs&lt;/SPAN&gt; e) {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;IDictionary&lt;/SPAN&gt; keys = e.Keys;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;IDictionary&lt;/SPAN&gt; newValues = e.NewValues;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;IDictionary&lt;/SPAN&gt; oldValues = e.OldValues;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;}&lt;/P&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;&lt;!--EndFragment--&gt;
&lt;P mce_keep="true"&gt;Forgive me for the variation in my code formatting I'm trying to find the right one. &lt;BR&gt;&lt;BR&gt;If we look at this event we can see the GridView nicely packages for us the new values, old values and the keys for the updating row. Unfortunately these dictionaries are only filled out if you are bound to a datasource control &lt;STRONG&gt;:(&lt;/STRONG&gt;. So that means when you&amp;nbsp;bind to some raw collection and hook up to the DataSource property then call databind, if you try to handle the delete or update events these dictionaries are going to be empty. Right now you must be asking yourself how can you get those dictionaries filled out just as if you &lt;STRONG&gt;were&lt;/STRONG&gt; bound to a Datasource control? The good news is you &lt;STRONG&gt;CAN &lt;/STRONG&gt;do it(this is what this blog post is all about right?).&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;STRONG&gt;&lt;U&gt;Going Hunting in the Control Tree&lt;BR&gt;&lt;BR&gt;&lt;IMG style="WIDTH: 200px; HEIGHT: 200px" height=200 src="http://peacecorpsonline.org/messages/jpeg/minefield.jpg" width=200 mce_src="http://peacecorpsonline.org/messages/jpeg/minefield.jpg"&gt;&lt;BR&gt;&lt;/U&gt;&lt;/STRONG&gt;&lt;BR&gt;This is probably one of the worst things you can do. From the time you write code that depends on the immediate layout of your page then your asking for trouble. I often see people on the forums writing code like this:&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: #ddd 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #ddd 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10pt; BACKGROUND: black; PADDING-BOTTOM: 2px; BORDER-LEFT: #ddd 1px solid; COLOR: white; PADDING-TOP: 2px; BORDER-BOTTOM: #ddd 1px solid; FONT-FAMILY: Consolas"&gt;
&lt;P style="MARGIN: 0px"&gt;((&lt;SPAN style="COLOR: #2b91af"&gt;TextBox&lt;/SPAN&gt;)GridView1.Rows[e.RowIndex].Cells[2].Controls[1]).Text&lt;/P&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;
&lt;P mce_keep="true"&gt;Seeing stuff like that makes my spine tingle (and not in a good way). &lt;STRONG&gt;DO NOT &lt;/STRONG&gt;write code like this!&lt;BR&gt;&lt;BR&gt;&lt;STRONG&gt;&lt;U&gt;FindControl&lt;/U&gt;&lt;/STRONG&gt;&lt;BR&gt;FindControl is a very powerful method on Control that allows you to search for a nested control, &lt;STRONG&gt;NOT&lt;/STRONG&gt; synonymous to DOM function &lt;STRONG&gt;getElementById. &lt;/STRONG&gt;I Often see people abusing find control and not understand that it is &lt;STRONG&gt;NOT&lt;/STRONG&gt; recursive by default and complain when code like this&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: #ddd 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #ddd 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10pt; BACKGROUND: black; PADDING-BOTTOM: 2px; BORDER-LEFT: #ddd 1px solid; COLOR: white; PADDING-TOP: 2px; BORDER-BOTTOM: #ddd 1px solid; FONT-FAMILY: Consolas"&gt;((&lt;SPAN style="COLOR: #2b91af"&gt;TextBox&lt;/SPAN&gt;)GridView1.FindControl(&lt;SPAN style="COLOR: #ff8000"&gt;"TextBox1"&lt;/SPAN&gt;)).Text&lt;/DIV&gt;
&lt;P&gt;Throws&amp;nbsp;a null reference exception. My advice is use &lt;STRONG&gt;Use FindControl as a last resort.&lt;/STRONG&gt; &lt;BR&gt;&lt;BR&gt;&lt;STRONG&gt;2-way DataBinding&lt;BR&gt;&lt;/STRONG&gt;2-way Databinding is a cool feature in &lt;STRONG&gt;ASP.NET&lt;/STRONG&gt; &lt;STRONG&gt;2.0&lt;/STRONG&gt; which allows the user to write some special syntax to bind against property values they would like to extract from a control.&lt;BR&gt;There is alot of magic going on behind the scenes (which I will blog about in a separate post), but you do not need to know how it works to use it, e.g&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: #ddd 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #ddd 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10pt; BACKGROUND: black; PADDING-BOTTOM: 2px; BORDER-LEFT: #ddd 1px solid; COLOR: white; PADDING-TOP: 2px; BORDER-BOTTOM: #ddd 1px solid; FONT-FAMILY: Consolas"&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;lt;&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;GridView&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;runat&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"server"&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;ID&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"GridView1"&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: yellow"&gt;Columns&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;TemplateField&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: yellow"&gt;EditItemTemplate&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;TextBox&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;ID&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"TextBox1" &lt;/SPAN&gt;&lt;SPAN style="COLOR: yellow"&gt;runat&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"server" &lt;/SPAN&gt;&lt;SPAN style="COLOR: yellow"&gt;Text&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;'&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND: #ffee62"&gt;&amp;lt;%&lt;/SPAN&gt;# Bind("ProductName") &lt;SPAN style="BACKGROUND: #ffee62"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff8000"&gt;'&lt;/SPAN&gt;&amp;gt;&amp;lt;/&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;TextBox&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;SPAN style="COLOR: yellow"&gt;EditItemTemplate&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;TemplateField&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;SPAN style="COLOR: yellow"&gt;Columns&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;lt;/&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;GridView&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;!--EndFragment--&gt;When you write the &lt;STRONG&gt;&amp;lt;%# Bind("ProductName") #&amp;gt;&lt;/STRONG&gt; expression then the value of the Text property is pushed into a dictionary which the data control can retrieve later. So how does &lt;STRONG&gt;GridView/FormView/DetailsView/ListView&lt;/STRONG&gt; get these values? Each data control has a method which is responsible for populating a dictionary of name value pairs from field name to value. Here is a mapping of data control to method used to extract values:&lt;BR&gt;&lt;BR&gt;&lt;STRONG&gt;GridView =&amp;gt; protected ExtractRowValues&lt;BR&gt;FormView =&amp;gt; protected ExtractRowValues&lt;BR&gt;DetailsView = &amp;gt; protected ExtractRowValues&lt;BR&gt;ListView =&amp;gt; public ExtractItemValues&lt;BR&gt;&lt;BR&gt;&lt;/STRONG&gt;As we can see with the exception of &lt;STRONG&gt;ListView&lt;/STRONG&gt; these oh so useful methods are protected, that means we can't call them from our code if we are using these built in controls. What can we do to surface these methods:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Derive new controls that expose ExtractRowValues through a public method&lt;/LI&gt;
&lt;LI&gt;Use private reflection to call the protected method (yikes)&lt;/LI&gt;
&lt;LI&gt;Stick all of your interesting fields in &lt;STRONG&gt;DataKeyNames&lt;/STRONG&gt;, then use the &lt;STRONG&gt;DataKeys[rowIndex].Values[fieldName]&lt;/STRONG&gt; (and watch your ViewState grow &lt;STRONG&gt;:( &lt;/STRONG&gt;)&lt;/LI&gt;
&lt;LI&gt;Do Nothing :)&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;To rectify some of this I've written a little helper for the GridView (and the other controls if you demand) that basically duplicates the functionality of &lt;STRONG&gt;ExtractRowValues&lt;/STRONG&gt; method.&lt;BR&gt;&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: #ddd 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #ddd 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10pt; BACKGROUND: black; PADDING-BOTTOM: 2px; BORDER-LEFT: #ddd 1px solid; COLOR: white; PADDING-TOP: 2px; BORDER-BOTTOM: #ddd 1px solid; FONT-FAMILY: Consolas"&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: yellow"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IDictionary&lt;/SPAN&gt; GetValues(&lt;SPAN style="COLOR: #2b91af"&gt;GridViewRow&lt;/SPAN&gt; row) {&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;IOrderedDictionary&lt;/SPAN&gt; values = &lt;SPAN style="COLOR: yellow"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;OrderedDictionary&lt;/SPAN&gt;();&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;foreach&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;DataControlFieldCell&lt;/SPAN&gt;&lt;/SPAN&gt; cell &lt;SPAN style="COLOR: yellow"&gt;in&lt;/SPAN&gt; row.Cells) {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;if&lt;/SPAN&gt; (cell.Visible) {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Extract values from the cell&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cell.ContainingField.ExtractValuesFromCell(values, cell, row.RowState, &lt;SPAN style="COLOR: yellow"&gt;true&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;return&lt;/SPAN&gt; values;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;} &lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;The method itself is pretty simple. It iterates over the control collection of the GridViewRow and calls &lt;STRONG&gt;ExtractValuesFromCell&lt;/STRONG&gt; on each cell which puts values into the dictionary.&lt;BR&gt;&lt;BR&gt;&lt;STRONG&gt;Uses&lt;/STRONG&gt;&lt;BR&gt;Most data controls support writing custom commands and handling some Command event(&lt;STRONG&gt;RowCommand for&lt;/STRONG&gt; &lt;STRONG&gt;GridView&lt;/STRONG&gt;). This method would come in handy if you needed to get the values out of the GridView for some custom command you wanted to execute.&lt;/P&gt;
&lt;P&gt;If you can, use 2 way databinding, if your control doesn't have&amp;nbsp;a useful&amp;nbsp;property to bind against and you need to do some more logic to get the right value then use &lt;STRONG&gt;FindControl&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Edit:&lt;BR&gt;&lt;/STRONG&gt;I've updated the code to loop over the Cells collection instead of the Controls collection (which is alot cleaner). Also there is no real compelling reason to return a strongly typed IDictionary&amp;lt;string, object&amp;gt;, so I just return the IDictionary instead.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;DetailsView:&lt;BR&gt;&lt;BR&gt;&lt;/STRONG&gt;Reader daveh551 needed some help adapting the code to DetailsView so here it is:&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: #ccc 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #ccc 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 8pt; BACKGROUND: black; PADDING-BOTTOM: 2px; BORDER-LEFT: #ccc 1px solid; COLOR: white; PADDING-TOP: 2px; BORDER-BOTTOM: #ccc 1px solid; FONT-FAMILY: Consolas"&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&lt;SPAN style="COLOR: yellow"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IDictionary&lt;/SPAN&gt; GetValues(&lt;SPAN style="COLOR: #2b91af"&gt;DetailsView&lt;/SPAN&gt; detailsView) {&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;IOrderedDictionary&lt;/SPAN&gt; values = &lt;SPAN style="COLOR: yellow"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;OrderedDictionary&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&lt;SPAN style="COLOR: yellow"&gt;    foreach&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;DetailsViewRow&lt;/SPAN&gt; row &lt;SPAN style="COLOR: yellow"&gt;in&lt;/SPAN&gt; detailsView.Rows) {&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Only look at Data Rows&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;if&lt;/SPAN&gt; (row.RowType != &lt;SPAN style="COLOR: #2b91af"&gt;DataControlRowType&lt;/SPAN&gt;.DataRow) {&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;continue&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Assume the first cell is a header cell&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;DataControlFieldCell&lt;/SPAN&gt; dataCell = (&lt;SPAN style="COLOR: #2b91af"&gt;DataControlFieldCell&lt;/SPAN&gt;)row.Cells[0];&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// If we are showing the header for this row then the data is in the adjacent cell&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;if&lt;/SPAN&gt; (dataCell.ContainingField.ShowHeader) {&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; dataCell = (&lt;SPAN style="COLOR: #2b91af"&gt;DataControlFieldCell&lt;/SPAN&gt;)row.Cells[1];&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; dataCell.ContainingField.ExtractValuesFromCell(values, dataCell, row.RowState, &lt;SPAN style="COLOR: yellow"&gt;true&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;return&lt;/SPAN&gt; values;&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;STRONG&gt;FormView:&lt;/STRONG&gt;&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: #ccc 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #ccc 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 8pt; BACKGROUND: black; PADDING-BOTTOM: 2px; BORDER-LEFT: #ccc 1px solid; COLOR: white; PADDING-TOP: 2px; BORDER-BOTTOM: #ccc 1px solid; FONT-FAMILY: Consolas"&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&lt;SPAN style="COLOR: yellow"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IDictionary&lt;/SPAN&gt; GetValues(&lt;SPAN style="COLOR: #2b91af"&gt;FormView&lt;/SPAN&gt; formView) {&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;IOrderedDictionary&lt;/SPAN&gt; fieldValues = &lt;SPAN style="COLOR: yellow"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;OrderedDictionary&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ExtractValuesFromBindableControls(fieldValues, formView);&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;IBindableTemplate&lt;/SPAN&gt; itemTemplate = &lt;SPAN style="COLOR: yellow"&gt;null&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;if&lt;/SPAN&gt; (formView.CurrentMode == &lt;SPAN style="COLOR: #2b91af"&gt;FormViewMode&lt;/SPAN&gt;.ReadOnly &amp;amp;&amp;amp; formView.ItemTemplate != &lt;SPAN style="COLOR: yellow"&gt;null&lt;/SPAN&gt;) {&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; itemTemplate = formView.ItemTemplate &lt;SPAN style="COLOR: yellow"&gt;as&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IBindableTemplate&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;else&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;if&lt;/SPAN&gt; (formView.CurrentMode == &lt;SPAN style="COLOR: #2b91af"&gt;FormViewMode&lt;/SPAN&gt;.Edit &amp;amp;&amp;amp; formView.EditItemTemplate != &lt;SPAN style="COLOR: yellow"&gt;null&lt;/SPAN&gt;) {&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; itemTemplate = formView.EditItemTemplate &lt;SPAN style="COLOR: yellow"&gt;as&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IBindableTemplate&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;else&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;if&lt;/SPAN&gt; (formView.CurrentMode == &lt;SPAN style="COLOR: #2b91af"&gt;FormViewMode&lt;/SPAN&gt;.Insert &amp;amp;&amp;amp; formView.InsertItemTemplate != &lt;SPAN style="COLOR: yellow"&gt;null&lt;/SPAN&gt;) {&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; itemTemplate = formView.InsertItemTemplate &lt;SPAN style="COLOR: yellow"&gt;as&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IBindableTemplate&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;if&lt;/SPAN&gt; (itemTemplate != &lt;SPAN style="COLOR: yellow"&gt;null&lt;/SPAN&gt;) {&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;foreach&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;DictionaryEntry&lt;/SPAN&gt; entry &lt;SPAN style="COLOR: yellow"&gt;in&lt;/SPAN&gt; itemTemplate.ExtractValues(formView)) {&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; fieldValues[entry.Key] = entry.Value;&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;return&lt;/SPAN&gt; fieldValues;&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;}&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&lt;SPAN style="COLOR: yellow"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;void&lt;/SPAN&gt; ExtractValuesFromBindableControls(&lt;SPAN style="COLOR: #2b91af"&gt;IOrderedDictionary&lt;/SPAN&gt; values, &lt;SPAN style="COLOR: #2b91af"&gt;Control&lt;/SPAN&gt; container) {&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;IBindableControl&lt;/SPAN&gt; control = container &lt;SPAN style="COLOR: yellow"&gt;as&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IBindableControl&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;if&lt;/SPAN&gt; (control != &lt;SPAN style="COLOR: yellow"&gt;null&lt;/SPAN&gt;) {&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; control.ExtractValues(values);&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;foreach&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;Control&lt;/SPAN&gt; childControl &lt;SPAN style="COLOR: yellow"&gt;in&lt;/SPAN&gt; container.Controls) {&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ExtractValuesFromBindableControls(values, childControl);&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="FONT-SIZE: 10pt; MARGIN: 0px; FONT-FAMILY: Consolas"&gt;} &lt;/PRE&gt;&lt;!--EndFragment--&gt;&lt;/DIV&gt;
&lt;P&gt;Let me know if you find any bugs. &lt;BR&gt;Hope this helps&lt;BR&gt;&lt;BR&gt;&lt;STRONG&gt;UPDATE: &lt;/STRONG&gt;I've had some requests to add more data controls and to convert the code to VB etc. So I decided to instead put the code in an assembly and make available for download &lt;A class="" href="http://oyugjg.blu.livefilestore.com/y1pTp6Vnt6m4G4hqa_xxVrVZQfdqUSaNcX9Id7-Rrr6DIimN5SH4T904X9_xwvgwFxygSsC6wVmysVGh27oxwOQxA/Fowler.Web.zip?download" mce_href="http://oyugjg.blu.livefilestore.com/y1pTp6Vnt6m4G4hqa_xxVrVZQfdqUSaNcX9Id7-Rrr6DIimN5SH4T904X9_xwvgwFxygSsC6wVmysVGh27oxwOQxA/Fowler.Web.zip?download"&gt;here&lt;/A&gt;.&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6780118" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/davidfowler/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/davidfowler/archive/tags/DataBinding/default.aspx">DataBinding</category><category domain="http://weblogs.asp.net/davidfowler/archive/tags/DataControls/default.aspx">DataControls</category></item><item><title>Dynamic Sorting with Linq</title><link>http://weblogs.asp.net/davidfowler/archive/2008/12/11/dynamic-sorting-with-linq.aspx</link><pubDate>Thu, 11 Dec 2008 13:26:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6778202</guid><dc:creator>davidfowl</dc:creator><slash:comments>10</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/davidfowler/rsscomments.aspx?PostID=6778202</wfw:commentRss><comments>http://weblogs.asp.net/davidfowler/archive/2008/12/11/dynamic-sorting-with-linq.aspx#comments</comments><description>&lt;P mce_keep="true"&gt;When trying to implement a &lt;A title="Business Layer" href="http://en.wikipedia.org/wiki/Business_layer" mce_href="http://en.wikipedia.org/wiki/Business_layer"&gt;Business Logic Layer&lt;/A&gt;&amp;nbsp;(i will refer to this as &lt;STRONG&gt;BLL&lt;/STRONG&gt;) with linq one thing that is an annoyance is sorting. Lets say you had a BLL that was being used by &lt;STRONG&gt;ObjectDataSource &lt;/STRONG&gt;and had a select method that does sorting and paging, then you'd probably be tempted to write something like this: &lt;PRE&gt;&lt;DIV style="BORDER-BOTTOM: #ddd 1px solid; BORDER-LEFT: #ddd 1px solid; PADDING-BOTTOM: 2px; PADDING-LEFT: 2px; PADDING-RIGHT: 2px; FONT-FAMILY: Consolas; BACKGROUND: black; COLOR: white; FONT-SIZE: 10pt; BORDER-TOP: #ddd 1px solid; BORDER-RIGHT: #ddd 1px solid; PADDING-TOP: 2px"&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: yellow"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IQueryable&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Product&lt;/SPAN&gt;&amp;gt; SortBy(&lt;SPAN style="COLOR: #2b91af"&gt;IQueryable&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Product&lt;/SPAN&gt;&amp;gt; source, &lt;SPAN style="COLOR: yellow"&gt;string&lt;/SPAN&gt; sortBy) {
&lt;/P&gt;&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;int&lt;/SPAN&gt; desc = sortBy.IndexOf(&lt;SPAN style="COLOR: #ff8000"&gt;"DESC"&lt;/SPAN&gt;);
&lt;/P&gt;&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;bool&lt;/SPAN&gt; isDescending = desc &amp;gt;= 0;
&lt;/P&gt;&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;if&lt;/SPAN&gt; (isDescending) {
&lt;/P&gt;&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; sortBy = sortBy.Substring(0, desc).Trim();
&lt;/P&gt;&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/P&gt;&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;switch&lt;/SPAN&gt; (sortBy) {
&lt;/P&gt;&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;case&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff8000"&gt;"ProductName"&lt;/SPAN&gt;:
&lt;/P&gt;&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;if&lt;/SPAN&gt; (isDescending) {
&lt;/P&gt;&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;return&lt;/SPAN&gt; source.OrderByDescending(p =&amp;gt; p.ProductName);
&lt;/P&gt;&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/P&gt;&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; source = source.OrderBy(p =&amp;gt; p.ProductName);
&lt;/P&gt;&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;break&lt;/SPAN&gt;;
&lt;/P&gt;&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;case&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff8000"&gt;"UnitPrice"&lt;/SPAN&gt;:
&lt;/P&gt;&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;if&lt;/SPAN&gt; (isDescending) {
&lt;/P&gt;&lt;P style="MARGIN: 0px"&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;return&lt;/SPAN&gt; source.OrderByDescending(p =&amp;gt; p.UnitPrice);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/P&gt;&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; source = source.OrderBy(p =&amp;gt; p.UnitPrice);
&lt;/P&gt;&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;break&lt;/SPAN&gt;;&lt;BR&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;         ..... One for every property
&lt;/P&gt;&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&lt;/P&gt;&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;return&lt;/SPAN&gt; source;&lt;/P&gt;&lt;P style="MARGIN: 0px"&gt;}&lt;/P&gt;&lt;/DIV&gt;&lt;/PRE&gt;But that can be really tedious and you probably have more than one object. Would you want to duplicate that for Categories, Suppliers, etc? I don't think so. Linq takes&lt;BR&gt;advantage of another C# 3.0 feature called &lt;A href="http://msdn.microsoft.com/en-us/library/bb397951.aspx" mce_href="http://msdn.microsoft.com/en-us/library/bb397951.aspx"&gt;Expression Trees&lt;/A&gt; and we can take advantage of these in our code to build a dynamic sort expression for any object.&lt;STRONG&gt; &lt;/STRONG&gt;&lt;STRONG&gt;&lt;/STRONG&gt;&lt;STRONG&gt;
&lt;P&gt;The code:&lt;/STRONG&gt; &lt;BR&gt;So how do we do this? I'm not going to explain how expression trees work in this blog post but there are plenty of resources &lt;A href="http://www.google.com/search?hl=en&amp;amp;rls=com.microsoft%3A*&amp;amp;q=C%23+3.0+Expression+Trees" mce_href="http://www.google.com/search?hl=en&amp;amp;rls=com.microsoft%3A*&amp;amp;q=C%23+3.0+Expression+Trees"&gt;out there&amp;nbsp;&lt;/A&gt;&amp;nbsp;that you can take a look at if your interested.&lt;BR&gt;The method is an &lt;STRONG&gt;extension method&lt;/STRONG&gt; on IQueryable&amp;lt;T&amp;gt; that takes the IQuerable&amp;lt;T&amp;gt; source and the sort parameter which should be the property name that you would like to sort by. In the case of DataSources&amp;nbsp;like ObjectDataSource, the sort parameter will contain "DESC' if the sort direction is descending. Here is the code:&lt;/P&gt;
&lt;DIV style="BORDER-BOTTOM: #ddd 1px solid; BORDER-LEFT: #ddd 1px solid; PADDING-BOTTOM: 2px; PADDING-LEFT: 2px; PADDING-RIGHT: 2px; FONT-FAMILY: Consolas; BACKGROUND: black; COLOR: white; FONT-SIZE: 10pt; BORDER-TOP: #ddd 1px solid; BORDER-RIGHT: #ddd 1px solid; PADDING-TOP: 2px"&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: yellow"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;QueryExtensions&lt;/SPAN&gt; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IQueryable&lt;/SPAN&gt;&amp;lt;T&amp;gt; SortBy&amp;lt;T&amp;gt;(&lt;SPAN style="COLOR: yellow"&gt;this&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IQueryable&lt;/SPAN&gt;&amp;lt;T&amp;gt; source, &lt;SPAN style="COLOR: yellow"&gt;string&lt;/SPAN&gt; propertyName) {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;if&lt;/SPAN&gt; (source == &lt;SPAN style="COLOR: yellow"&gt;null&lt;/SPAN&gt;) {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ArgumentNullException&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #ff8000"&gt;"source"&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// DataSource control passes the sort parameter with a direction&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// if the direction is descending&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;int&lt;/SPAN&gt; descIndex = propertyName.IndexOf(&lt;SPAN style="COLOR: #ff8000"&gt;" DESC"&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;if&lt;/SPAN&gt; (descIndex &amp;gt;= 0) {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; propertyName = propertyName.Substring(0, descIndex).Trim();&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;if&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;String&lt;/SPAN&gt;.IsNullOrEmpty(propertyName)) {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;return&lt;/SPAN&gt; source;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;ParameterExpression&lt;/SPAN&gt; parameter = &lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;.Parameter(source.ElementType, &lt;SPAN style="COLOR: #2b91af"&gt;String&lt;/SPAN&gt;.Empty);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;MemberExpression&lt;/SPAN&gt; property = &lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;.Property(parameter, propertyName);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;LambdaExpression&lt;/SPAN&gt; lambda = &lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;.Lambda(property, parameter);&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;string&lt;/SPAN&gt; methodName = (descIndex &amp;lt; 0) ? &lt;SPAN style="COLOR: #ff8000"&gt;"OrderBy"&lt;/SPAN&gt; : &lt;SPAN style="COLOR: #ff8000"&gt;"OrderByDescending"&lt;/SPAN&gt;;&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt; methodCallExpression = &lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;.Call(&lt;SPAN style="COLOR: yellow"&gt;typeof&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;Queryable&lt;/SPAN&gt;), methodName,&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Type&lt;/SPAN&gt;[] { source.ElementType, property.Type },&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; source.Expression, &lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;.Quote(lambda));&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;return&lt;/SPAN&gt; source.Provider.CreateQuery&amp;lt;T&amp;gt;(methodCallExpression);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;}&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;And a link to the file. &lt;/P&gt;
&lt;P&gt;&lt;A href="http://cid-d5fe29d177d33421.skydrive.live.com/self.aspx/Public/QueryExtensions.cs" mce_href="http://cid-d5fe29d177d33421.skydrive.live.com/self.aspx/Public/QueryExtensions.cs"&gt;QueryExtensions.cs&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Hope this helps&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6778202" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/davidfowler/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/davidfowler/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/davidfowler/archive/tags/LINQ/default.aspx">LINQ</category></item><item><title>DataControls 101 Part 2: Why you should love Datasource controls</title><link>http://weblogs.asp.net/davidfowler/archive/2008/10/21/datacontrols-101-part-2-why-you-should-love-datasource-controls.aspx</link><pubDate>Tue, 21 Oct 2008 09:41:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6696001</guid><dc:creator>davidfowl</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/davidfowler/rsscomments.aspx?PostID=6696001</wfw:commentRss><comments>http://weblogs.asp.net/davidfowler/archive/2008/10/21/datacontrols-101-part-2-why-you-should-love-datasource-controls.aspx#comments</comments><description>&lt;P&gt;I've been scouring the forums recently trying to find problems people encounter when using the data controls. One thing that I found is mostly asked for but is kind of a hidden art: &lt;BR&gt;&lt;B&gt;How do I use the GridView/ListView/DetailsView.... without a Datasource control? (and still get all the fancy features offered).&lt;/B&gt; 
&lt;P&gt;Now I've always know that you &lt;B&gt;can&lt;/B&gt; do this but I decided to explore how much is involved in making this stuff to work. This sample shows a GridView using raw Linq queries in C#. Let’s dive into the code: 
&lt;P&gt;&lt;U&gt;&lt;B&gt;Markup&lt;/B&gt; &lt;BR&gt;&lt;/U&gt;&lt;BR&gt;
&lt;DIV style="BORDER-RIGHT: #ddd 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #ddd 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10pt; BACKGROUND: black; PADDING-BOTTOM: 2px; BORDER-LEFT: #ddd 1px solid; COLOR: white; PADDING-TOP: 2px; BORDER-BOTTOM: #ddd 1px solid; FONT-FAMILY: Consolas"&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;lt;&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;GridView&lt;/SPAN&gt; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;DataKeyNames&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"ProductID"&lt;/SPAN&gt; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;ID&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"products"&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;runat&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"server"&lt;/SPAN&gt; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;AllowPaging&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"True"&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;AllowSorting&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"True"&lt;/SPAN&gt; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;CellPadding&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"4"&lt;/SPAN&gt; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;ForeColor&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"#333333"&lt;/SPAN&gt; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;GridLines&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"None"&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: yellow"&gt;RowStyle&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;BackColor&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"#F7F6F3"&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;ForeColor&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"#333333"&lt;/SPAN&gt; /&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: yellow"&gt;Columns&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;CommandField&lt;/SPAN&gt; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;ShowDeleteButton&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"True"&lt;/SPAN&gt; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;ShowEditButton&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"True"&lt;/SPAN&gt; &lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;ShowSelectButton&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"True"&lt;/SPAN&gt; /&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;SPAN style="COLOR: yellow"&gt;Columns&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: yellow"&gt;FooterStyle&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;BackColor&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"#5D7B9D"&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;Font-Bold&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"True"&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;ForeColor&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"White"&lt;/SPAN&gt; /&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: yellow"&gt;PagerStyle&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;BackColor&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"#284775"&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;ForeColor&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"White"&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;HorizontalAlign&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"Center"&lt;/SPAN&gt; /&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: yellow"&gt;SelectedRowStyle&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;BackColor&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"#E2DED6"&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;Font-Bold&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"True"&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;ForeColor&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"#333333"&lt;/SPAN&gt; /&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: yellow"&gt;HeaderStyle&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;BackColor&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"#5D7B9D"&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;Font-Bold&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"True"&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;ForeColor&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"White"&lt;/SPAN&gt; /&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: yellow"&gt;EditRowStyle&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;BackColor&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"#999999"&lt;/SPAN&gt; /&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;SPAN style="COLOR: yellow"&gt;AlternatingRowStyle&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;BackColor&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"White"&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;ForeColor&lt;/SPAN&gt;=&lt;SPAN style="COLOR: #ff8000"&gt;"#284775"&lt;/SPAN&gt; /&amp;gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;lt;/&lt;SPAN style="COLOR: yellow"&gt;asp&lt;/SPAN&gt;:&lt;SPAN style="COLOR: yellow"&gt;GridView&lt;/SPAN&gt;&amp;gt;&lt;/P&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Code behind&lt;/STRONG&gt; &lt;BR&gt;&lt;/U&gt;&lt;BR&gt;
&lt;DIV style="BORDER-RIGHT: #ddd 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #ddd 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10pt; BACKGROUND: black; PADDING-BOTTOM: 2px; BORDER-LEFT: #ddd 1px solid; COLOR: white; PADDING-TOP: 2px; BORDER-BOTTOM: #ddd 1px solid; FONT-FAMILY: Consolas"&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: yellow"&gt;protected&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;void&lt;/SPAN&gt; Page_Load(&lt;SPAN style="COLOR: yellow"&gt;object&lt;/SPAN&gt; sender, &lt;SPAN style="COLOR: #2b91af"&gt;EventArgs&lt;/SPAN&gt; e) {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Setup Events&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; products.RowEditing += &lt;SPAN style="COLOR: yellow"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;GridViewEditEventHandler&lt;/SPAN&gt;(OnProductsRowEditing);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; products.RowUpdating += &lt;SPAN style="COLOR: yellow"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;GridViewUpdateEventHandler&lt;/SPAN&gt;(OnProductsRowUpdating);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; products.RowCancelingEdit += &lt;SPAN style="COLOR: yellow"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;GridViewCancelEditEventHandler&lt;/SPAN&gt;(OnProductsRowCancelingEdit);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; products.RowDeleting += &lt;SPAN style="COLOR: yellow"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;GridViewDeleteEventHandler&lt;/SPAN&gt;(OnProductsRowDeleting);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; products.Sorting += &lt;SPAN style="COLOR: yellow"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;GridViewSortEventHandler&lt;/SPAN&gt;(OnProductsSorting);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; products.PageIndexChanging += &lt;SPAN style="COLOR: yellow"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;GridViewPageEventHandler&lt;/SPAN&gt;(OnProductsPageIndexChanging);&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;if&lt;/SPAN&gt; (!IsPostBack) {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DataBindProducts();&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;}&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: yellow"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;void&lt;/SPAN&gt; DataBindProducts() {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;using&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;NorthwindDataContext&lt;/SPAN&gt; db = &lt;SPAN style="COLOR: yellow"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;NorthwindDataContext&lt;/SPAN&gt;()) {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; products.DataSource = db.Products;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; products.DataBind();&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;}&lt;/P&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;
&lt;P&gt;The code above can be done in the markup but I chose to do it in the code behind. The code in Page_Load is very basic and self explanatory. 
&lt;P&gt;&lt;U&gt;&lt;B&gt;Editing&lt;/B&gt; &lt;BR&gt;&lt;/U&gt;&lt;BR&gt;
&lt;DIV style="BORDER-RIGHT: #ddd 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #ddd 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10pt; BACKGROUND: black; PADDING-BOTTOM: 2px; BORDER-LEFT: #ddd 1px solid; COLOR: white; PADDING-TOP: 2px; BORDER-BOTTOM: #ddd 1px solid; FONT-FAMILY: Consolas"&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: yellow"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;void&lt;/SPAN&gt; OnProductsRowEditing(&lt;SPAN style="COLOR: yellow"&gt;object&lt;/SPAN&gt; sender, &lt;SPAN style="COLOR: #2b91af"&gt;GridViewEditEventArgs&lt;/SPAN&gt; e) {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Set EditIndex&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; products.EditIndex = e.NewEditIndex;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; DataBindProducts();&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;}&lt;/P&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;The Edit event handler is pretty simple and straight forward. We set the new edit index and Databind the grid. 
&lt;P&gt;&lt;U&gt;&lt;B&gt;Paging&lt;/B&gt; &lt;BR&gt;&lt;/U&gt;&lt;BR&gt;
&lt;DIV style="BORDER-RIGHT: #ddd 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #ddd 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10pt; BACKGROUND: black; PADDING-BOTTOM: 2px; BORDER-LEFT: #ddd 1px solid; COLOR: white; PADDING-TOP: 2px; BORDER-BOTTOM: #ddd 1px solid; FONT-FAMILY: Consolas"&gt;&lt;SPAN style="COLOR: yellow"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;void&lt;/SPAN&gt; OnProductsPageIndexChanging(&lt;SPAN style="COLOR: yellow"&gt;object&lt;/SPAN&gt; sender, &lt;SPAN style="COLOR: #2b91af"&gt;GridViewPageEventArgs&lt;/SPAN&gt; e) { 
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; products.EditIndex = -1;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; products.PageIndex = e.NewPageIndex;&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp; DataBindProducts();&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;}&lt;/P&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;Paging code is similar to editing; we just set the new page index and reset the edit index to -1. 
&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Cancel&lt;/STRONG&gt; &lt;BR&gt;&lt;BR&gt;&lt;/U&gt;
&lt;DIV style="BORDER-RIGHT: #ddd 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #ddd 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10pt; BACKGROUND: black; PADDING-BOTTOM: 2px; BORDER-LEFT: #ddd 1px solid; COLOR: white; PADDING-TOP: 2px; BORDER-BOTTOM: #ddd 1px solid; FONT-FAMILY: Consolas"&gt;&lt;SPAN style="COLOR: yellow"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;void&lt;/SPAN&gt; OnProductsRowCancelingEdit(&lt;SPAN style="COLOR: yellow"&gt;object&lt;/SPAN&gt; sender, &lt;SPAN style="COLOR: #2b91af"&gt;GridViewCancelEditEventArgs&lt;/SPAN&gt; e) { 
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Reset the edit index&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; products.EditIndex = -1;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; DataBindProducts();&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;}&lt;/P&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;Now the hard stuff... 
&lt;P&gt;&lt;U&gt;&lt;B&gt;Sorting&lt;/B&gt; &lt;BR&gt;&lt;/U&gt;&lt;BR&gt;
&lt;DIV style="BORDER-RIGHT: #ddd 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #ddd 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10pt; BACKGROUND: black; PADDING-BOTTOM: 2px; BORDER-LEFT: #ddd 1px solid; COLOR: white; PADDING-TOP: 2px; BORDER-BOTTOM: #ddd 1px solid; FONT-FAMILY: Consolas"&gt;&lt;SPAN style="COLOR: yellow"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;void&lt;/SPAN&gt; OnProductsSorting(&lt;SPAN style="COLOR: yellow"&gt;object&lt;/SPAN&gt; sender, &lt;SPAN style="COLOR: #2b91af"&gt;GridViewSortEventArgs&lt;/SPAN&gt; e) { 
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; products.EditIndex = -1;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; products.PageIndex = 0;&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;if&lt;/SPAN&gt; ((SortExpression == e.SortExpression) &amp;amp;&amp;amp; (SortDirection == &lt;SPAN style="COLOR: #2b91af"&gt;SortDirection&lt;/SPAN&gt;.Ascending)) {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; e.SortDirection = &lt;SPAN style="COLOR: #2b91af"&gt;SortDirection&lt;/SPAN&gt;.Descending;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;using&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;NorthwindDataContext&lt;/SPAN&gt; db = &lt;SPAN style="COLOR: yellow"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;NorthwindDataContext&lt;/SPAN&gt;()) {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; products.DataSource = &lt;SPAN style="COLOR: #2b91af"&gt;QueryableExtensions&lt;/SPAN&gt;.SortBy(db.Products, e.SortExpression, e.SortDirection);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; products.DataBind();&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; SortExpression = e.SortExpression;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; SortDirection = e.SortDirection;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;}&lt;/P&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;Woah! What’s going on here? When you examine the event arguments of the sorting event you will realize that the SortExpression changes but the SortDirection is always Ascending... This seems very weird, because the GridView keeps track of the sorting state (SortExpression and SortDirection). The problem is this state is only updated when the GridView is bound to a Datasource control :(. Why did we make that decision I’m not sure, but it is what it is so we must keep track of this ourselves.&lt;BR&gt;&lt;BR&gt;
&lt;DIV style="BORDER-RIGHT: #ddd 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #ddd 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10pt; BACKGROUND: black; PADDING-BOTTOM: 2px; BORDER-LEFT: #ddd 1px solid; COLOR: white; PADDING-TOP: 2px; BORDER-BOTTOM: #ddd 1px solid; FONT-FAMILY: Consolas"&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: yellow"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;string&lt;/SPAN&gt; SortExpression {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;get&lt;/SPAN&gt; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;return&lt;/SPAN&gt; (&lt;SPAN style="COLOR: yellow"&gt;string&lt;/SPAN&gt;)ViewState[&lt;SPAN style="COLOR: #ff8000"&gt;"SortExpression"&lt;/SPAN&gt;] ?? &lt;SPAN style="COLOR: #2b91af"&gt;String&lt;/SPAN&gt;.Empty;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;set&lt;/SPAN&gt; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ViewState[&lt;SPAN style="COLOR: #ff8000"&gt;"SortExpression"&lt;/SPAN&gt;] = &lt;SPAN style="COLOR: yellow"&gt;value&lt;/SPAN&gt;;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;}&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: yellow"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;SortDirection&lt;/SPAN&gt; SortDirection {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;get&lt;/SPAN&gt; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;object&lt;/SPAN&gt; o = ViewState[&lt;SPAN style="COLOR: #ff8000"&gt;"SortDirection"&lt;/SPAN&gt;];&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;return&lt;/SPAN&gt; o != &lt;SPAN style="COLOR: yellow"&gt;null&lt;/SPAN&gt; ? (&lt;SPAN style="COLOR: #2b91af"&gt;SortDirection&lt;/SPAN&gt;)o : &lt;SPAN style="COLOR: #2b91af"&gt;SortDirection&lt;/SPAN&gt;.Ascending;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: yellow"&gt;set&lt;/SPAN&gt; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ViewState[&lt;SPAN style="COLOR: #ff8000"&gt;"SortDirection"&lt;/SPAN&gt;] = &lt;SPAN style="COLOR: yellow"&gt;value&lt;/SPAN&gt;;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;}&lt;/P&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;We store the current SortExpression and SortDirection in the viewstate (GridView keeps it in control state). Then we do the obvious logic to change the sort direction appropriately. &lt;BR&gt;The rest of the sort method just uses my helper to convert SortExpression and SortDirection to a Linq expression. 
&lt;P&gt;&lt;B&gt;&lt;U&gt;Updating/Deleting&lt;BR&gt;&lt;/U&gt;&lt;BR&gt;&lt;/B&gt;
&lt;DIV style="BORDER-RIGHT: #ddd 1px solid; PADDING-RIGHT: 2px; BORDER-TOP: #ddd 1px solid; PADDING-LEFT: 2px; FONT-SIZE: 10pt; BACKGROUND: black; PADDING-BOTTOM: 2px; BORDER-LEFT: #ddd 1px solid; COLOR: white; PADDING-TOP: 2px; BORDER-BOTTOM: #ddd 1px solid; FONT-FAMILY: Consolas"&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: yellow"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;void&lt;/SPAN&gt; OnProductsRowUpdating(&lt;SPAN style="COLOR: yellow"&gt;object&lt;/SPAN&gt; sender, &lt;SPAN style="COLOR: #2b91af"&gt;GridViewUpdateEventArgs&lt;/SPAN&gt; e) {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;GridViewRow&lt;/SPAN&gt; row = products.Rows[e.RowIndex];&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;IOrderedDictionary&lt;/SPAN&gt; values = &lt;SPAN style="COLOR: yellow"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;OrderedDictionary&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;StringComparer&lt;/SPAN&gt;.OrdinalIgnoreCase);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;DictionaryHelper&lt;/SPAN&gt;.ExtractRowValues(products, values, row, &lt;SPAN style="COLOR: yellow"&gt;true&lt;/SPAN&gt; &lt;SPAN style="COLOR: green"&gt;/* includeReadOnlyFields */&lt;/SPAN&gt;, &lt;SPAN style="COLOR: yellow"&gt;true&lt;/SPAN&gt; &lt;SPAN style="COLOR: green"&gt;/* includePrimaryKey */&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Update the product&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;QueryableExtensions&lt;/SPAN&gt;.UpdateProduct(values);&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; products.EditIndex = -1;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; DataBindProducts();&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;}&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: yellow"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: yellow"&gt;void&lt;/SPAN&gt; OnProductsRowDeleting(&lt;SPAN style="COLOR: yellow"&gt;object&lt;/SPAN&gt; sender, &lt;SPAN style="COLOR: #2b91af"&gt;GridViewDeleteEventArgs&lt;/SPAN&gt; e) {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;GridViewRow&lt;/SPAN&gt; row = products.Rows[e.RowIndex];&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;IOrderedDictionary&lt;/SPAN&gt; values = &lt;SPAN style="COLOR: yellow"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;OrderedDictionary&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;StringComparer&lt;/SPAN&gt;.OrdinalIgnoreCase);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;DictionaryHelper&lt;/SPAN&gt;.ExtractRowValues(products, values, row, &lt;SPAN style="COLOR: yellow"&gt;true&lt;/SPAN&gt; &lt;SPAN style="COLOR: green"&gt;/* includeReadOnlyFields */&lt;/SPAN&gt;, &lt;SPAN style="COLOR: yellow"&gt;true&lt;/SPAN&gt; &lt;SPAN style="COLOR: green"&gt;/* includePrimaryKey */&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Delete the product&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;QueryableExtensions&lt;/SPAN&gt;.DeleteProduct(values);&lt;/P&gt;
&lt;P style="MARGIN: 0px" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; products.EditIndex = -1;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; DataBindProducts();&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;}&lt;/P&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;These 2 methods are pretty similar in nature.First thing you may realize when examining the EventArgs in these methods are that all the dictionaries are &lt;B&gt;empty! &lt;/B&gt;(again, another weird design choice) There is some logic in the Databound controls that detects if it is bound to a Datasource control or Datasource. In the case of the GridView, it calls &lt;B&gt;ExtractRowValues &lt;/B&gt;to fill the dictionaries for update/delete if it is bound to a Datasource control. Since this function is protected the only other way to get the values from the row is to use find control and manually extract the data yourself, which can be a mess. I used private reflection in my sample to call the function and fill the dictionary (yes it's a little hacky &lt;B&gt;:)&lt;/B&gt;). 
&lt;P&gt;&lt;B&gt;Other random other pieces of code:&lt;BR&gt;&lt;/B&gt;&lt;BR&gt;The calls to &lt;B&gt;QueryableDataSourceHelper.SortBy, QueryableDataSourceHelper.UpdateProduct, QueryableDataSourceHelper.DeleteProduct,&lt;/B&gt; can be seen as specific implementations of a Datasource control’s &lt;B&gt;ExecuteDelete&lt;/B&gt;, &lt;B&gt;ExecuteInsert&lt;/B&gt;, and &lt;B&gt;ExecuteSelect&lt;/B&gt; (sorting handled here) methods. 
&lt;P&gt;What your Datasource does for you: 
&lt;UL&gt;
&lt;LI&gt;CRUD operations for a specific technology (SQL/XML/objects) 
&lt;LI&gt;Sorting and paging 
&lt;LI&gt;Conversion of values from controls/querystring/session…etc through parameters.&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;If you look at my &lt;A class="" href="http://weblogs.asp.net/davidfowler/archive/2008/10/14/data-controls-101.aspx" mce_href="http://weblogs.asp.net/davidfowler/archive/2008/10/14/data-controls-101.aspx"&gt;previous post&lt;/A&gt; and the amount of code I wrote to get it working compared to the above is mind boggling. 
&lt;P&gt;&lt;B&gt;DON'T TAKE YOUR DATASOURCE FOR GRANTED. &lt;/B&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;A class="" href="http://cid-d5fe29d177d33421.skydrive.live.com/self.aspx/Public/ManualGridview.zip" target=_blank mce_href="http://cid-d5fe29d177d33421.skydrive.live.com/self.aspx/Public/ManualGridview.zip"&gt;Here&lt;/A&gt; &lt;/STRONG&gt;is a link to the sample solution.&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6696001" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/davidfowler/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/davidfowler/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/davidfowler/archive/tags/GridView/default.aspx">GridView</category><category domain="http://weblogs.asp.net/davidfowler/archive/tags/DataSourceControls/default.aspx">DataSourceControls</category></item></channel></rss>