<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7557263183336868289</id><updated>2012-01-27T05:40:41.321-08:00</updated><category term='C#'/><category term='Performance Optimizations'/><category term='Visual Studio'/><category term='Motivational'/><category term='WCF'/><category term='Javascript'/><category term='Code Refactoring'/><category term='Architecting Web Applications'/><category term='AJAX'/><category term='XML'/><category term='Internet Explorer'/><category term='IIS7'/><category term='Security'/><category term='Algorithms'/><category term='WPF'/><category term='Silverlight'/><category term='ASP.NET'/><title type='text'>Yosif Yosifov</title><subtitle type='html'>Blogging about issues met in the every day development process - mainly about .NET, C#, ASP.NET and Microsoft technologies.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://yyosifov.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://yyosifov.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>cypressx</name><uri>http://www.blogger.com/profile/03595535035189960906</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>21</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7557263183336868289.post-8005191599316422841</id><published>2012-01-26T00:43:00.001-08:00</published><updated>2012-01-26T02:03:16.655-08:00</updated><title type='text'>Binding UI controls using Fluent API</title><content type='html'>Hi guys,&lt;br /&gt;&lt;br /&gt;I've been working on ASP.NET Web Forms application for a while and have been constantly writing Data Binding on all kinds of containers - repeaters, list views, grid views and etc. &lt;br /&gt;&lt;br /&gt;Let's imagine a very simple scenario. We have a Contact Class and Address Class. You get the Address of the Contact, using the Contact Id. In most cases, these objects will come from the databases, but for the purposes of this post, we don't need any complications. Therefore here are my classes:&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;namespace&lt;/span&gt; FluentBindingSample&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Data&lt;br /&gt;&lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;class&lt;/span&gt; Address&lt;br /&gt;    &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt; ContactId &lt;span style='color:#800080; '&gt;{&lt;/span&gt; get&lt;span style='color:#800080; '&gt;;&lt;/span&gt; set&lt;span style='color:#800080; '&gt;;&lt;/span&gt; &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;string&lt;/span&gt; Country &lt;span style='color:#800080; '&gt;{&lt;/span&gt; get&lt;span style='color:#800080; '&gt;;&lt;/span&gt; set&lt;span style='color:#800080; '&gt;;&lt;/span&gt; &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;string&lt;/span&gt; City &lt;span style='color:#800080; '&gt;{&lt;/span&gt; get&lt;span style='color:#800080; '&gt;;&lt;/span&gt; set&lt;span style='color:#800080; '&gt;;&lt;/span&gt; &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt; PostalCode &lt;span style='color:#800080; '&gt;{&lt;/span&gt; get&lt;span style='color:#800080; '&gt;;&lt;/span&gt; set&lt;span style='color:#800080; '&gt;;&lt;/span&gt; &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; Address&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;and &lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;namespace&lt;/span&gt; FluentBindingSample&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Data&lt;br /&gt;&lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;class&lt;/span&gt; Contact&lt;br /&gt;    &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt; ContactId &lt;span style='color:#800080; '&gt;{&lt;/span&gt; get&lt;span style='color:#800080; '&gt;;&lt;/span&gt; set&lt;span style='color:#800080; '&gt;;&lt;/span&gt; &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;string&lt;/span&gt; FirstName &lt;span style='color:#800080; '&gt;{&lt;/span&gt; get&lt;span style='color:#800080; '&gt;;&lt;/span&gt; set&lt;span style='color:#800080; '&gt;;&lt;/span&gt; &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;string&lt;/span&gt; LastName &lt;span style='color:#800080; '&gt;{&lt;/span&gt; get&lt;span style='color:#800080; '&gt;;&lt;/span&gt; set&lt;span style='color:#800080; '&gt;;&lt;/span&gt; &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; Contact&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now, here is a sample data provider, which will help us retrieve Contacts and retrieve data (currently, hardcoded):&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;using&lt;/span&gt; System&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;using&lt;/span&gt; System&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Collections&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Generic&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;using&lt;/span&gt; System&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Linq&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;using&lt;/span&gt; FluentBindingSample&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Data&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;namespace&lt;/span&gt; FluentBindingSample&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Model&lt;br /&gt;&lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;class&lt;/span&gt; SampleDataProvider&lt;br /&gt;    &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;static&lt;/span&gt; IEnumerable&lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt; Contact &lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt; GetSampleContacts&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;            yield &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;new&lt;/span&gt; Contact &lt;span style='color:#800080; '&gt;{&lt;/span&gt; ContactId &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#008c00; '&gt;1&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt; FirstName&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;Leo&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt; LastName&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;Messi&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt; &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;            yield &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;new&lt;/span&gt; Contact &lt;span style='color:#800080; '&gt;{&lt;/span&gt; ContactId &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#008c00; '&gt;2&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt; FirstName &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;Wayne&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt; LastName &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;Rooney&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt; &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;            yield &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;new&lt;/span&gt; Contact &lt;span style='color:#800080; '&gt;{&lt;/span&gt; ContactId &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#008c00; '&gt;3&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt; FirstName &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;Christiano&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt; LastName &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;Ronaldo&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt; &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;static&lt;/span&gt; Address GetAddressByContactId&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt; contactId&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;switch&lt;/span&gt; &lt;span style='color:#808030; '&gt;(&lt;/span&gt;contactId&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;                &lt;span style='color:#800000; font-weight:bold; '&gt;case&lt;/span&gt; &lt;span style='color:#008c00; '&gt;1&lt;/span&gt;&lt;span style='color:#808030; '&gt;:&lt;/span&gt;&lt;br /&gt;                    &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;new&lt;/span&gt; Address &lt;span style='color:#800080; '&gt;{&lt;/span&gt; Country &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;Argentina&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt; &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;                &lt;span style='color:#800000; font-weight:bold; '&gt;case&lt;/span&gt; &lt;span style='color:#008c00; '&gt;2&lt;/span&gt;&lt;span style='color:#808030; '&gt;:&lt;/span&gt;&lt;br /&gt;                    &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;new&lt;/span&gt; Address &lt;span style='color:#800080; '&gt;{&lt;/span&gt; Country &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;England&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt; &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;                &lt;span style='color:#800000; font-weight:bold; '&gt;case&lt;/span&gt; &lt;span style='color:#008c00; '&gt;3&lt;/span&gt;&lt;span style='color:#808030; '&gt;:&lt;/span&gt;&lt;br /&gt;                    &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;new&lt;/span&gt; Address &lt;span style='color:#800080; '&gt;{&lt;/span&gt; Country &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;Portugal&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt; &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;                &lt;span style='color:#800000; font-weight:bold; '&gt;default&lt;/span&gt;&lt;span style='color:#808030; '&gt;:&lt;/span&gt;&lt;br /&gt;                    &lt;span style='color:#800000; font-weight:bold; '&gt;throw&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;new&lt;/span&gt; NotSupportedException&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;We're ready with our sample Business Layer. Now, let's bind this data to a Repeater:&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;asp&lt;span style='color:#808030; '&gt;:&lt;/span&gt;Repeater ID&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;rptContats&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt; runat&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;server&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;HeaderTemplate&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;table&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;thead&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                    &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;tr&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                        &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;th&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                            Contact Id&lt;br /&gt;                        &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;th&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                        &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;th&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                            First Name&lt;br /&gt;                        &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;th&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                        &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;th&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                            Last Name&lt;br /&gt;                        &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;th&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                        &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;th&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                            Country&lt;br /&gt;                        &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;th&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                    &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;tr&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;thead&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;tbody&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;HeaderTemplate&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;ItemTemplate&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;tr&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;td&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                    &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;asp&lt;span style='color:#808030; '&gt;:&lt;/span&gt;Literal ID&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;ltrContactId&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt; runat&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;server&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt; &lt;span style='color:#808030; '&gt;/&lt;/span&gt;&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;td&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;td&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                    &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;asp&lt;span style='color:#808030; '&gt;:&lt;/span&gt;Literal ID&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;ltrFirstName&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt; runat&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;server&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt; &lt;span style='color:#808030; '&gt;/&lt;/span&gt;&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;td&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;td&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                    &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;asp&lt;span style='color:#808030; '&gt;:&lt;/span&gt;Literal ID&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;ltrLastName&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt; runat&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;server&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt; &lt;span style='color:#808030; '&gt;/&lt;/span&gt;&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;td&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;td&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                    &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;asp&lt;span style='color:#808030; '&gt;:&lt;/span&gt;Literal ID&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;ltrCountry&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt; runat&lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;server&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt; &lt;span style='color:#808030; '&gt;/&lt;/span&gt;&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;td&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;tr&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;ItemTemplate&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;FooterTemplate&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;tbody&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;table&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;FooterTemplate&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;/&lt;/span&gt;asp&lt;span style='color:#808030; '&gt;:&lt;/span&gt;Repeater&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;We've finally set up our sample - we've reached the main point of this article. What I have always found boring is implementing an ItemDataBound event, getting all controls, casting and initiating them. I think this is a common problem and the code looks like this:&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;private&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;void&lt;/span&gt; rptContats_ItemDataBound&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;object&lt;/span&gt; sender&lt;span style='color:#808030; '&gt;,&lt;/span&gt; RepeaterItemEventArgs e&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt; &lt;span style='color:#808030; '&gt;(&lt;/span&gt;e&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Item&lt;span style='color:#808030; '&gt;.&lt;/span&gt;ItemType &lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#808030; '&gt;=&lt;/span&gt; ListItemType&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Item || e&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Item&lt;span style='color:#808030; '&gt;.&lt;/span&gt;ItemType &lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#808030; '&gt;=&lt;/span&gt; ListItemType&lt;span style='color:#808030; '&gt;.&lt;/span&gt;AlternatingItem&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;                Contact contact &lt;span style='color:#808030; '&gt;=&lt;/span&gt; e&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Item&lt;span style='color:#808030; '&gt;.&lt;/span&gt;DataItem &lt;span style='color:#800000; font-weight:bold; '&gt;as&lt;/span&gt; Contact&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;                Literal ltrContactId &lt;span style='color:#808030; '&gt;=&lt;/span&gt; e&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Item&lt;span style='color:#808030; '&gt;.&lt;/span&gt;FindControl&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;ltrContactId&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;as&lt;/span&gt; Literal&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;                Literal ltrFirstName &lt;span style='color:#808030; '&gt;=&lt;/span&gt; e&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Item&lt;span style='color:#808030; '&gt;.&lt;/span&gt;FindControl&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;ltrFirstName&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;as&lt;/span&gt; Literal&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;                Literal ltrLastName &lt;span style='color:#808030; '&gt;=&lt;/span&gt; e&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Item&lt;span style='color:#808030; '&gt;.&lt;/span&gt;FindControl&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;ltrLastName&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;as&lt;/span&gt; Literal&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;                Literal ltrCountry &lt;span style='color:#808030; '&gt;=&lt;/span&gt; e&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Item&lt;span style='color:#808030; '&gt;.&lt;/span&gt;FindControl&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;ltrCountry&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;as&lt;/span&gt; Literal&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;                ltrContactId&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Text &lt;span style='color:#808030; '&gt;=&lt;/span&gt; contact&lt;span style='color:#808030; '&gt;.&lt;/span&gt;ContactId&lt;span style='color:#808030; '&gt;.&lt;/span&gt;ToString&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;                ltrFirstName&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Text &lt;span style='color:#808030; '&gt;=&lt;/span&gt; contact&lt;span style='color:#808030; '&gt;.&lt;/span&gt;FirstName&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;                ltrLastName&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Text &lt;span style='color:#808030; '&gt;=&lt;/span&gt; contact&lt;span style='color:#808030; '&gt;.&lt;/span&gt;LastName&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;                Address address &lt;span style='color:#808030; '&gt;=&lt;/span&gt; SampleDataProvider&lt;span style='color:#808030; '&gt;.&lt;/span&gt;GetAddressByContactId&lt;span style='color:#808030; '&gt;(&lt;/span&gt;contact&lt;span style='color:#808030; '&gt;.&lt;/span&gt;ContactId&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;                ltrCountry&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Text &lt;span style='color:#808030; '&gt;=&lt;/span&gt; address&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Country&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;You see, we get all controls. Cast them, initialize them. Get some other things, related to the object and bind them too.&lt;br /&gt;What are the problems of this approach - we do not have any code reusability. In case I want to show some Contact details in another Template Control I'll have to copy paste my whole code. The code is bloated - we say few things with much lines.&lt;br /&gt;How can we improve the current situation? What I've come to useful in my projects is to implement a &lt;a href="http://en.wikipedia.org/wiki/Fluent_interface"&gt;Fluent API&lt;/a&gt;. Let us see, how can Fluent API improve our code.&lt;br /&gt;We first have to create the Interface of the API, itself. For our Contact and Address classes, this can look like this:&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;using&lt;/span&gt; System&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;using&lt;/span&gt; System&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Linq&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;using&lt;/span&gt; System&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Web&lt;span style='color:#808030; '&gt;.&lt;/span&gt;UI&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;namespace&lt;/span&gt; FluentBindingSample&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Model&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Binders&lt;br /&gt;&lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;interface&lt;/span&gt; IContactBinder&lt;br /&gt;    &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;        IContactBinder WithTextControl&lt;span style='color:#808030; '&gt;(&lt;/span&gt;Control control&lt;span style='color:#808030; '&gt;,&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;string&lt;/span&gt; controlName&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        IContactBinder BindFirstName&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        IContactBinder BindLastName&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        IContactBinder BindContactId&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;and&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;using&lt;/span&gt; System&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Web&lt;span style='color:#808030; '&gt;.&lt;/span&gt;UI&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;namespace&lt;/span&gt; FluentBindingSample&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Model&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Binders&lt;br /&gt;&lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;interface&lt;/span&gt; IAddressBinder&lt;br /&gt;    &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;        IAddressBinder WithTextControl&lt;span style='color:#808030; '&gt;(&lt;/span&gt;Control control&lt;span style='color:#808030; '&gt;,&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;string&lt;/span&gt; controlName&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        IAddressBinder BindCountry&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The implementation of this Fluent APIs is quite straight forward, too:&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;using&lt;/span&gt; System&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;using&lt;/span&gt; System&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Linq&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;using&lt;/span&gt; System&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Web&lt;span style='color:#808030; '&gt;.&lt;/span&gt;UI&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;using&lt;/span&gt; FluentBindingSample&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Data&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;namespace&lt;/span&gt; FluentBindingSample&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Model&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Binders&lt;br /&gt;&lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;class&lt;/span&gt; ContactBinder &lt;span style='color:#808030; '&gt;:&lt;/span&gt; IContactBinder&lt;br /&gt;    &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;private&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;readonly&lt;/span&gt; Contact contact&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;private&lt;/span&gt; ITextControl textControl&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;private&lt;/span&gt; ContactBinder&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;br /&gt;        &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; ContactBinder&lt;span style='color:#808030; '&gt;(&lt;/span&gt;Contact contact&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt; &lt;span style='color:#808030; '&gt;(&lt;/span&gt;contact &lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;null&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;                &lt;span style='color:#800000; font-weight:bold; '&gt;throw&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;new&lt;/span&gt; ArgumentNullException&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;this&lt;/span&gt;&lt;span style='color:#808030; '&gt;.&lt;/span&gt;contact &lt;span style='color:#808030; '&gt;=&lt;/span&gt; contact&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; IContactBinder WithTextControl&lt;span style='color:#808030; '&gt;(&lt;/span&gt;Control control&lt;span style='color:#808030; '&gt;,&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;string&lt;/span&gt; controlName&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;            ITextControl textControl &lt;span style='color:#808030; '&gt;=&lt;/span&gt; control&lt;span style='color:#808030; '&gt;.&lt;/span&gt;FindControl&lt;span style='color:#808030; '&gt;(&lt;/span&gt;controlName&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;as&lt;/span&gt; ITextControl&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;this&lt;/span&gt;&lt;span style='color:#808030; '&gt;.&lt;/span&gt;textControl &lt;span style='color:#808030; '&gt;=&lt;/span&gt; textControl&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;this&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; IContactBinder BindFirstName&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;            textControl&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Text &lt;span style='color:#808030; '&gt;=&lt;/span&gt; contact&lt;span style='color:#808030; '&gt;.&lt;/span&gt;FirstName&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;this&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; IContactBinder BindLastName&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;            textControl&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Text &lt;span style='color:#808030; '&gt;=&lt;/span&gt; contact&lt;span style='color:#808030; '&gt;.&lt;/span&gt;LastName&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;this&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; IContactBinder BindContactId&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;            textControl&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Text &lt;span style='color:#808030; '&gt;=&lt;/span&gt; contact&lt;span style='color:#808030; '&gt;.&lt;/span&gt;FirstName&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;this&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;and&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;using&lt;/span&gt; System&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;using&lt;/span&gt; System&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Linq&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;using&lt;/span&gt; System&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Web&lt;span style='color:#808030; '&gt;.&lt;/span&gt;UI&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;using&lt;/span&gt; FluentBindingSample&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Data&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;namespace&lt;/span&gt; FluentBindingSample&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Model&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Binders&lt;br /&gt;&lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;class&lt;/span&gt; AddressBinder &lt;span style='color:#808030; '&gt;:&lt;/span&gt; IAddressBinder&lt;br /&gt;    &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;private&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;readonly&lt;/span&gt; Address address&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;private&lt;/span&gt; ITextControl textControl&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;private&lt;/span&gt; AddressBinder&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; AddressBinder&lt;span style='color:#808030; '&gt;(&lt;/span&gt;Address address&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt; &lt;span style='color:#808030; '&gt;(&lt;/span&gt;address &lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;null&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;                &lt;span style='color:#800000; font-weight:bold; '&gt;throw&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;new&lt;/span&gt; ArgumentNullException&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;this&lt;/span&gt;&lt;span style='color:#808030; '&gt;.&lt;/span&gt;address &lt;span style='color:#808030; '&gt;=&lt;/span&gt; address&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; IAddressBinder WithTextControl&lt;span style='color:#808030; '&gt;(&lt;/span&gt;Control control&lt;span style='color:#808030; '&gt;,&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;string&lt;/span&gt; controlName&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;            ITextControl textControl &lt;span style='color:#808030; '&gt;=&lt;/span&gt; control&lt;span style='color:#808030; '&gt;.&lt;/span&gt;FindControl&lt;span style='color:#808030; '&gt;(&lt;/span&gt;controlName&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;as&lt;/span&gt; ITextControl&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;this&lt;/span&gt;&lt;span style='color:#808030; '&gt;.&lt;/span&gt;textControl &lt;span style='color:#808030; '&gt;=&lt;/span&gt; textControl&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;this&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; IAddressBinder BindCountry&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;            textControl&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Text &lt;span style='color:#808030; '&gt;=&lt;/span&gt; address&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Country&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;this&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now we've come to the sweet part. Integrating the Fluent API in the Item Data Bound event:&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;private&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;void&lt;/span&gt; rptContats_ItemDataBound&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;object&lt;/span&gt; sender&lt;span style='color:#808030; '&gt;,&lt;/span&gt; RepeaterItemEventArgs e&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt; &lt;span style='color:#808030; '&gt;(&lt;/span&gt;e&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Item&lt;span style='color:#808030; '&gt;.&lt;/span&gt;ItemType &lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#808030; '&gt;=&lt;/span&gt; ListItemType&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Item || e&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Item&lt;span style='color:#808030; '&gt;.&lt;/span&gt;ItemType &lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#808030; '&gt;=&lt;/span&gt; ListItemType&lt;span style='color:#808030; '&gt;.&lt;/span&gt;AlternatingItem&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;                Contact contact &lt;span style='color:#808030; '&gt;=&lt;/span&gt; e&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Item&lt;span style='color:#808030; '&gt;.&lt;/span&gt;DataItem &lt;span style='color:#800000; font-weight:bold; '&gt;as&lt;/span&gt; Contact&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;                IContactBinder contactBinder &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;new&lt;/span&gt; ContactBinder&lt;span style='color:#808030; '&gt;(&lt;/span&gt;contact&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;                contactBinder&lt;span style='color:#808030; '&gt;.&lt;/span&gt;WithTextControl&lt;span style='color:#808030; '&gt;(&lt;/span&gt;e&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Item&lt;span style='color:#808030; '&gt;,&lt;/span&gt; &lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;ltrContactId&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;.&lt;/span&gt;BindContactId&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;                contactBinder&lt;span style='color:#808030; '&gt;.&lt;/span&gt;WithTextControl&lt;span style='color:#808030; '&gt;(&lt;/span&gt;e&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Item&lt;span style='color:#808030; '&gt;,&lt;/span&gt; &lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;ltrFirstName&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;.&lt;/span&gt;BindFirstName&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;                contactBinder&lt;span style='color:#808030; '&gt;.&lt;/span&gt;WithTextControl&lt;span style='color:#808030; '&gt;(&lt;/span&gt;e&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Item&lt;span style='color:#808030; '&gt;,&lt;/span&gt; &lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;ltrLastName&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;.&lt;/span&gt;BindLastName&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;                Address address &lt;span style='color:#808030; '&gt;=&lt;/span&gt; SampleDataProvider&lt;span style='color:#808030; '&gt;.&lt;/span&gt;GetAddressByContactId&lt;span style='color:#808030; '&gt;(&lt;/span&gt;contact&lt;span style='color:#808030; '&gt;.&lt;/span&gt;ContactId&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;                IAddressBinder addressBinder &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;new&lt;/span&gt; AddressBinder&lt;span style='color:#808030; '&gt;(&lt;/span&gt;address&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;                addressBinder&lt;span style='color:#808030; '&gt;.&lt;/span&gt;WithTextControl&lt;span style='color:#808030; '&gt;(&lt;/span&gt;e&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Item&lt;span style='color:#808030; '&gt;,&lt;/span&gt; &lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;ltrCountry&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;.&lt;/span&gt;BindCountry&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now - this is much shorter, more readable and reusable. If I need to bind such information elsewhere, I have the functionality to do it and can just reuse it.&lt;br /&gt;When can we use such approach? &lt;br /&gt;- If we want to reuse code.&lt;br /&gt;- If we want to encapsulate some logic - for instance formatting of the text, default values and etc.&lt;br /&gt;What's the cost? We have to create Fluent API for the objects - this means little more developer effort in the beginning, which can be paid off later. It all depends on the case. If you have complex objects and functionality with formatting, default values and etc, I think such approach will armor you better than the simpler way.&lt;br /&gt;&lt;br /&gt;The solution we outlined in this blog post can be downloaded from here:&lt;br /&gt;&lt;a href="https://sites.google.com/site/cypressx/FluentBindingSample.zip?attredirects=0&amp;d=1"&gt;Fluent Binding API Sample&lt;/a&gt;&lt;br /&gt;Hope you found some ideas for yourselves in this post. &lt;br /&gt;&lt;br /&gt;Happy Coding!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7557263183336868289-8005191599316422841?l=yyosifov.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://yyosifov.blogspot.com/feeds/8005191599316422841/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7557263183336868289&amp;postID=8005191599316422841' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/8005191599316422841'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/8005191599316422841'/><link rel='alternate' type='text/html' href='http://yyosifov.blogspot.com/2012/01/binding-ui-controls-using-fluent-api.html' title='Binding UI controls using Fluent API'/><author><name>cypressx</name><uri>http://www.blogger.com/profile/03595535035189960906</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7557263183336868289.post-7816131895158929347</id><published>2011-10-07T08:05:00.000-07:00</published><updated>2011-10-07T08:25:53.256-07:00</updated><title type='text'>ORA-12154: TNS:could not resolve the connect identifier specified in Visual Studio</title><content type='html'>&lt;span style="font-style:italic;"&gt;&lt;span style="font-weight:bold;"&gt;Hi guys&lt;/span&gt;&lt;/span&gt;,&lt;br /&gt;&lt;br /&gt;If you are trying to connect to an Oracle Database, through Server Explorer in Visual Studio, you have to ensure that:&lt;br /&gt;&lt;br /&gt;1. You have installed the Oracle Data Provider (downloadable from the Oracle Website)&lt;br /&gt;2. Have created the tnsnames.ora file, holding information about the databases (http://www.orafaq.com/wiki/Tnsnames.ora). This file should be created most often in the Network\Admin folder under the Oracle Provider. In my case: C:\app\user\product\11.2.0\client_1\Network\Admin&lt;br /&gt;&lt;br /&gt;After having these prerequisites, I still couldn't connect to the Oracle Database. The Visual Studio wizard told me: &lt;span style="font-weight:bold;"&gt;"ORA-12154: TNS:could not resolve the connect identifier specified"&lt;/span&gt;. What resolved this issue for me was to add the Environment Variable with name "TNS_ADMIN" and value the Path to the directory containing the "tnsnames.ora" file.&lt;br /&gt;Something like:&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;TNS_NAMES="C:\app\user\product\11.2.0\client_1\Network\Admin"&lt;/span&gt;&lt;br /&gt;Then just restart Visual Studio and you're free to go.&lt;br /&gt;&lt;br /&gt;Hope this helps someone out there :)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7557263183336868289-7816131895158929347?l=yyosifov.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://yyosifov.blogspot.com/feeds/7816131895158929347/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7557263183336868289&amp;postID=7816131895158929347' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/7816131895158929347'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/7816131895158929347'/><link rel='alternate' type='text/html' href='http://yyosifov.blogspot.com/2011/10/ora-12154-tnscould-not-resolve-connect.html' title='ORA-12154: TNS:could not resolve the connect identifier specified in Visual Studio'/><author><name>cypressx</name><uri>http://www.blogger.com/profile/03595535035189960906</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7557263183336868289.post-7234902463785018845</id><published>2011-07-20T13:10:00.000-07:00</published><updated>2011-07-19T15:17:25.850-07:00</updated><title type='text'>More on Template Design Pattern</title><content type='html'>&lt;span style="font-style:italic;"&gt;Hi guys,&lt;/span&gt;&lt;br /&gt;hope you liked the last article, which included a modification the Template method pattern.&lt;br /&gt;&lt;br /&gt;I would like to continue the same topic and expose another useful implementation of the pattern. It's a very basic example of OOP - nothing advanced.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style:italic;"&gt;What do we want solve&lt;/span&gt;&lt;br /&gt;In web applications you often have to expose your data through different formats - like XML, JSON or RSS. Every time, you need to get the Response of the page, clear it, fill it with contents and then end the Response.&lt;br /&gt;This can be an error prone information, as you have to take care of some details like "Flushing" the response before ending it. It would be nice, if you could skip these details after you've written and tested them once.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style:italic;"&gt;Response Template implementation&lt;/span&gt;&lt;br /&gt;My approach to this problem was to create a simple ResponseTemplate class, which is actually a plain implementation of the Template design pattern. Below is a sample implementation:&lt;br /&gt;&lt;br /&gt;&lt;pre  style="background:#ffffff;color:#000000;"&gt;&lt;span style=" font-weight:bold; color:#800000;" &gt;public&lt;/span&gt; &lt;span style=" font-weight:bold; color:#800000;" &gt;abstract&lt;/span&gt; &lt;span style=" font-weight:bold; color:#800000;" &gt;class&lt;/span&gt; ResponseTemplate&lt;br /&gt;&lt;span style=" ;color:#800080;" &gt;{&lt;/span&gt;&lt;br /&gt; &lt;span style=" font-weight:bold; color:#800000;" &gt;protected&lt;/span&gt; HttpResponse Response &lt;span style=" ;color:#800080;" &gt;{&lt;/span&gt; get&lt;span style=" ;color:#800080;" &gt;;&lt;/span&gt; &lt;span style=" font-weight:bold; color:#800000;" &gt;private&lt;/span&gt; set&lt;span style=" ;color:#800080;" &gt;;&lt;/span&gt; &lt;span style=" ;color:#800080;" &gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt; &lt;span style=" font-weight:bold; color:#800000;" &gt;public&lt;/span&gt; ResponseTemplate&lt;span style=" ;color:#808030;" &gt;(&lt;/span&gt;HttpResponse httpResponse&lt;span style=" ;color:#808030;" &gt;)&lt;/span&gt;&lt;br /&gt; &lt;span style=" ;color:#800080;" &gt;{&lt;/span&gt;&lt;br /&gt;     &lt;span style=" font-weight:bold; color:#800000;" &gt;this&lt;/span&gt;&lt;span style=" ;color:#808030;" &gt;.&lt;/span&gt;Response &lt;span style=" ;color:#808030;" &gt;=&lt;/span&gt; httpResponse&lt;span style=" ;color:#800080;" &gt;;&lt;/span&gt;&lt;br /&gt; &lt;span style=" ;color:#800080;" &gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt; &lt;span style=" font-weight:bold; color:#800000;" &gt;public&lt;/span&gt; &lt;span style=" font-weight:bold; color:#800000;" &gt;void&lt;/span&gt; WriteResponse&lt;span style=" ;color:#808030;" &gt;(&lt;/span&gt;&lt;span style=" ;color:#808030;" &gt;)&lt;/span&gt;&lt;br /&gt; &lt;span style=" ;color:#800080;" &gt;{&lt;/span&gt;&lt;br /&gt;     &lt;span style=" font-weight:bold; color:#800000;" &gt;this&lt;/span&gt;&lt;span style=" ;color:#808030;" &gt;.&lt;/span&gt;Response&lt;span style=" ;color:#808030;" &gt;.&lt;/span&gt;Clear&lt;span style=" ;color:#808030;" &gt;(&lt;/span&gt;&lt;span style=" ;color:#808030;" &gt;)&lt;/span&gt;&lt;span style=" ;color:#800080;" &gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;     SetContentType&lt;span style=" ;color:#808030;" &gt;(&lt;/span&gt;&lt;span style=" ;color:#808030;" &gt;)&lt;/span&gt;&lt;span style=" ;color:#800080;" &gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;     Write&lt;span style=" ;color:#808030;" &gt;(&lt;/span&gt;&lt;span style=" ;color:#808030;" &gt;)&lt;/span&gt;&lt;span style=" ;color:#800080;" &gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;     &lt;span style=" font-weight:bold; color:#800000;" &gt;this&lt;/span&gt;&lt;span style=" ;color:#808030;" &gt;.&lt;/span&gt;Response&lt;span style=" ;color:#808030;" &gt;.&lt;/span&gt;Flush&lt;span style=" ;color:#808030;" &gt;(&lt;/span&gt;&lt;span style=" ;color:#808030;" &gt;)&lt;/span&gt;&lt;span style=" ;color:#800080;" &gt;;&lt;/span&gt;&lt;br /&gt;     &lt;span style=" font-weight:bold; color:#800000;" &gt;this&lt;/span&gt;&lt;span style=" ;color:#808030;" &gt;.&lt;/span&gt;Response&lt;span style=" ;color:#808030;" &gt;.&lt;/span&gt;End&lt;span style=" ;color:#808030;" &gt;(&lt;/span&gt;&lt;span style=" ;color:#808030;" &gt;)&lt;/span&gt;&lt;span style=" ;color:#800080;" &gt;;&lt;/span&gt; &lt;br /&gt; &lt;span style=" ;color:#800080;" &gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt; &lt;span style=" font-weight:bold; color:#800000;" &gt;internal&lt;/span&gt; &lt;span style=" font-weight:bold; color:#800000;" &gt;abstract&lt;/span&gt; &lt;span style=" font-weight:bold; color:#800000;" &gt;void&lt;/span&gt; Write&lt;span style=" ;color:#808030;" &gt;(&lt;/span&gt;&lt;span style=" ;color:#808030;" &gt;)&lt;/span&gt;&lt;span style=" ;color:#800080;" &gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt; &lt;span style=" font-weight:bold; color:#800000;" &gt;internal&lt;/span&gt; &lt;span style=" font-weight:bold; color:#800000;" &gt;abstract&lt;/span&gt; &lt;span style=" font-weight:bold; color:#800000;" &gt;void&lt;/span&gt; SetContentType&lt;span style=" ;color:#808030;" &gt;(&lt;/span&gt;&lt;span style=" ;color:#808030;" &gt;)&lt;/span&gt;&lt;span style=" ;color:#800080;" &gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style=" ;color:#800080;" &gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Using this class, you forget about the details. You just pass the HttpResponse and implement the &lt;span style="font-style:italic;"&gt;SetContentType()&lt;/span&gt; and &lt;span style="font-style:italic;"&gt;Write()&lt;/span&gt; methods.&lt;br /&gt;&lt;br /&gt;Now if I would want to return an XML I would write a concrete implementation inheriting the ResponseTemplate class:&lt;br /&gt;&lt;pre  style="background:#ffffff;color:#000000;"&gt;&lt;span style=" font-weight:bold; color:#800000;" &gt;public&lt;/span&gt; &lt;span style=" font-weight:bold; color:#800000;" &gt;class&lt;/span&gt; XmlResponse &lt;span style=" ;color:#808030;" &gt;:&lt;/span&gt; ResponseTemplate&lt;br /&gt;&lt;span style=" ;color:#800080;" &gt;{&lt;/span&gt;&lt;br /&gt; &lt;span style=" font-weight:bold; color:#800000;" &gt;private&lt;/span&gt; &lt;span style=" font-weight:bold; color:#800000;" &gt;string&lt;/span&gt; XML &lt;span style=" ;color:#800080;" &gt;{&lt;/span&gt; get&lt;span style=" ;color:#800080;" &gt;;&lt;/span&gt; set&lt;span style=" ;color:#800080;" &gt;;&lt;/span&gt; &lt;span style=" ;color:#800080;" &gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt; &lt;span style=" font-weight:bold; color:#800000;" &gt;public&lt;/span&gt; XmlResponse&lt;span style=" ;color:#808030;" &gt;(&lt;/span&gt;HttpResponse httpResponse&lt;span style=" ;color:#808030;" &gt;,&lt;/span&gt; XDocument doc&lt;span style=" ;color:#808030;" &gt;)&lt;/span&gt;&lt;br /&gt;     &lt;span style=" ;color:#808030;" &gt;:&lt;/span&gt; &lt;span style=" font-weight:bold; color:#800000;" &gt;base&lt;/span&gt;&lt;span style=" ;color:#808030;" &gt;(&lt;/span&gt;httpResponse&lt;span style=" ;color:#808030;" &gt;)&lt;/span&gt;&lt;br /&gt; &lt;span style=" ;color:#800080;" &gt;{&lt;/span&gt;&lt;br /&gt;     &lt;span style=" font-weight:bold; color:#800000;" &gt;this&lt;/span&gt;&lt;span style=" ;color:#808030;" &gt;.&lt;/span&gt;XML &lt;span style=" ;color:#808030;" &gt;=&lt;/span&gt; doc&lt;span style=" ;color:#808030;" &gt;.&lt;/span&gt;ToString&lt;span style=" ;color:#808030;" &gt;(&lt;/span&gt;&lt;span style=" ;color:#808030;" &gt;)&lt;/span&gt;&lt;span style=" ;color:#800080;" &gt;;&lt;/span&gt;&lt;br /&gt; &lt;span style=" ;color:#800080;" &gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt; &lt;span style=" font-weight:bold; color:#800000;" &gt;public&lt;/span&gt; XmlResponse&lt;span style=" ;color:#808030;" &gt;(&lt;/span&gt;HttpResponse httpResponse&lt;span style=" ;color:#808030;" &gt;,&lt;/span&gt; &lt;span style=" font-weight:bold; color:#800000;" &gt;string&lt;/span&gt; xml&lt;span style=" ;color:#808030;" &gt;)&lt;/span&gt;&lt;br /&gt;     &lt;span style=" ;color:#808030;" &gt;:&lt;/span&gt; &lt;span style=" font-weight:bold; color:#800000;" &gt;base&lt;/span&gt;&lt;span style=" ;color:#808030;" &gt;(&lt;/span&gt;httpResponse&lt;span style=" ;color:#808030;" &gt;)&lt;/span&gt;&lt;br /&gt; &lt;span style=" ;color:#800080;" &gt;{&lt;/span&gt;&lt;br /&gt;     &lt;span style=" font-weight:bold; color:#800000;" &gt;this&lt;/span&gt;&lt;span style=" ;color:#808030;" &gt;.&lt;/span&gt;XML &lt;span style=" ;color:#808030;" &gt;=&lt;/span&gt; xml&lt;span style=" ;color:#800080;" &gt;;&lt;/span&gt;&lt;br /&gt; &lt;span style=" ;color:#800080;" &gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt; &lt;span style=" font-weight:bold; color:#800000;" &gt;internal&lt;/span&gt; &lt;span style=" font-weight:bold; color:#800000;" &gt;override&lt;/span&gt; &lt;span style=" font-weight:bold; color:#800000;" &gt;void&lt;/span&gt; Write&lt;span style=" ;color:#808030;" &gt;(&lt;/span&gt;&lt;span style=" ;color:#808030;" &gt;)&lt;/span&gt;&lt;br /&gt; &lt;span style=" ;color:#800080;" &gt;{&lt;/span&gt;&lt;br /&gt;     &lt;span style=" font-weight:bold; color:#800000;" &gt;base&lt;/span&gt;&lt;span style=" ;color:#808030;" &gt;.&lt;/span&gt;Response&lt;span style=" ;color:#808030;" &gt;.&lt;/span&gt;Write&lt;span style=" ;color:#808030;" &gt;(&lt;/span&gt;&lt;span style=" font-weight:bold; color:#800000;" &gt;this&lt;/span&gt;&lt;span style=" ;color:#808030;" &gt;.&lt;/span&gt;XML&lt;span style=" ;color:#808030;" &gt;)&lt;/span&gt;&lt;span style=" ;color:#800080;" &gt;;&lt;/span&gt;&lt;br /&gt; &lt;span style=" ;color:#800080;" &gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt; &lt;span style=" font-weight:bold; color:#800000;" &gt;internal&lt;/span&gt; &lt;span style=" font-weight:bold; color:#800000;" &gt;override&lt;/span&gt; &lt;span style=" font-weight:bold; color:#800000;" &gt;void&lt;/span&gt; SetContentType&lt;span style=" ;color:#808030;" &gt;(&lt;/span&gt;&lt;span style=" ;color:#808030;" &gt;)&lt;/span&gt;&lt;br /&gt; &lt;span style=" ;color:#800080;" &gt;{&lt;/span&gt;&lt;br /&gt;     &lt;span style=" font-weight:bold; color:#800000;" &gt;base&lt;/span&gt;&lt;span style=" ;color:#808030;" &gt;.&lt;/span&gt;Response&lt;span style=" ;color:#808030;" &gt;.&lt;/span&gt;ContentType &lt;span style=" ;color:#808030;" &gt;=&lt;/span&gt; ContentType&lt;span style=" ;color:#808030;" &gt;.&lt;/span&gt;Xml&lt;span style=" ;color:#800080;" &gt;;&lt;/span&gt;&lt;br /&gt; &lt;span style=" ;color:#800080;" &gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style=" ;color:#800080;" &gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;In the end, these are all just plain and very simple OOP principles. The main purpose why I continue making them look harder by calling them things like "Template Design Pattern" is that this facilitates your communication with the other developers. But, this is a whole different topic, worth writing some day.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style:italic;"&gt;Happy coding!&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7557263183336868289-7234902463785018845?l=yyosifov.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://yyosifov.blogspot.com/feeds/7234902463785018845/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7557263183336868289&amp;postID=7234902463785018845' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/7234902463785018845'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/7234902463785018845'/><link rel='alternate' type='text/html' href='http://yyosifov.blogspot.com/2011/05/more-on-template-design-pattern.html' title='More on Template Design Pattern'/><author><name>cypressx</name><uri>http://www.blogger.com/profile/03595535035189960906</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7557263183336868289.post-5287700097192102162</id><published>2011-05-19T11:19:00.000-07:00</published><updated>2011-07-19T15:06:27.872-07:00</updated><title type='text'>Try/Catch refactoring</title><content type='html'>&lt;span style="font-style:italic;"&gt;Hi guys&lt;/span&gt;,&lt;br /&gt;every now and then I enjoy learning and inventing some new tiny tricks and tips. They make my life easier and my code tidier. Today I would like to share some small refactoring, which I find neat and use all the time.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style:italic;"&gt;Original Code&lt;/span&gt;&lt;br /&gt;Most probably, you often have to write repetitive code like the one below:&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;try&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#696969; '&gt;// Perform operations&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;catch&lt;/span&gt; &lt;span style='color:#808030; '&gt;(&lt;/span&gt;System&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Exception ex&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#696969; '&gt;// Log Exception&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This is pretty common in windows applications/services. What I don't like is repetitive writing it.&lt;br /&gt;So, how can we refactor it? We can review our list of Design Patterns and observe that what we can do is implement something like the &lt;a href="http://en.wikipedia.org/wiki/Template_method_pattern"&gt;Template method pattern&lt;/a&gt;. In C# we can use lambda expressions and anonymous methods to implement a lightweight version of a modification of this pattern.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style:italic;"&gt;Template method pattern modificatoin&lt;/span&gt;&lt;br /&gt;Using the Template method pattern idea, we have to wrap our executing code with try/catch statements. We can create a class we can use to actually "inject" our operation. Something like this one:&lt;br /&gt;&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;static&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;class&lt;/span&gt; SafeCodeExecutor&lt;br /&gt;&lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;static&lt;/span&gt; T Execute&lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;T&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;Func&lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;T&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt; operation&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style='color:#800000; font-weight:bold; '&gt;try&lt;/span&gt;&lt;br /&gt;       &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;           &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; operation&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;       &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;       &lt;span style='color:#800000; font-weight:bold; '&gt;catch&lt;/span&gt; &lt;span style='color:#808030; '&gt;(&lt;/span&gt;System&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Exception ex&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;       &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;           &lt;span style='color:#696969; '&gt;// Log Exception&lt;/span&gt;&lt;br /&gt;       &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;       &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;default&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;T&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now, we can freely use our SafeCodeExecutor throughout our project. Sample usage would be:&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;string&lt;/span&gt; designPatternName &lt;span style='color:#808030; '&gt;=&lt;/span&gt; SafeCodeExecutor&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Execute&lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;string&lt;/span&gt;&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#808030; '&gt;=&lt;/span&gt;&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt; &lt;br /&gt;    &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; &lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;Template method pattern&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt; &lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Of course, you can free use a method inside the Execute&lt;&gt;. &lt;br /&gt;&lt;br /&gt;You can build more Execute template methods which fit your needs - for instance taking Action&lt;T&gt; with some parameters and etc (for instance: &lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;static&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;void&lt;/span&gt; Execute&lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;T&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt;&lt;span style='color:#808030; '&gt;(&lt;/span&gt;Action&lt;span style='color:#808030; '&gt;&amp;lt;&lt;/span&gt;T&lt;span style='color:#808030; '&gt;&gt;&lt;/span&gt; operation&lt;span style='color:#808030; '&gt;,&lt;/span&gt; T item&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;). It's all up to you.&lt;br /&gt;&lt;br /&gt;Hope you liked this little trick!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7557263183336868289-5287700097192102162?l=yyosifov.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://yyosifov.blogspot.com/feeds/5287700097192102162/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7557263183336868289&amp;postID=5287700097192102162' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/5287700097192102162'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/5287700097192102162'/><link rel='alternate' type='text/html' href='http://yyosifov.blogspot.com/2011/05/trycatch-refactoring.html' title='Try/Catch refactoring'/><author><name>cypressx</name><uri>http://www.blogger.com/profile/03595535035189960906</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7557263183336868289.post-1494248893722186873</id><published>2011-02-28T20:00:00.000-08:00</published><updated>2011-03-01T00:46:08.674-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Security'/><category scheme='http://www.blogger.com/atom/ns#' term='Algorithms'/><title type='text'>FIPS compliant algorithms</title><content type='html'>Hi guys!&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Distributed software&lt;/span&gt;&lt;br /&gt;If you're building and distributing a software/product, you must be aware that some of the Encryption algorithms of the .NET Framework are considered "FIPS approved" (Federal Investigation Processing Standards) and others aren't.&lt;br /&gt;&lt;br /&gt;What problems can cause this? Well, if you're using MD5 in your distribution and your customer is running your software on a machine configured to ensure FIPS, your software won't be working.&lt;br /&gt;&lt;br /&gt;For instance, in Windows there is a setting called - "System cryptography: Use FIPS compliant algorithms for encryption, hashing, and signing". Enabling it, ensures that all applications on the PC are using algorithms which are approved by FIPS.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;FIPS approved algorithms list&lt;/span&gt;&lt;br /&gt;It's a good idea, to always check the Encryption algorithms you're using against the list of approved FIPS algorithms:&lt;br /&gt;&lt;a href="https://wiki.mozilla.org/FIPS_Validation"&gt;https://wiki.mozilla.org/FIPS_Validation&lt;br /&gt;&lt;/a&gt;&lt;br /&gt;This way you'll ensure your software robustness and reliability.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;More resources&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://csrc.nist.gov/groups/STM/cmvp/standards.html#02"&gt;http://csrc.nist.gov/groups/STM/cmvp/standards.html#02&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://technet.microsoft.com/en-us/library/cc780081%28WS.10%29.aspx"&gt;http://technet.microsoft.com/en-us/library/cc780081%28WS.10%29.aspx&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7557263183336868289-1494248893722186873?l=yyosifov.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://yyosifov.blogspot.com/feeds/1494248893722186873/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7557263183336868289&amp;postID=1494248893722186873' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/1494248893722186873'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/1494248893722186873'/><link rel='alternate' type='text/html' href='http://yyosifov.blogspot.com/2011/02/fips-compliant-algorithms.html' title='FIPS compliant algorithms'/><author><name>cypressx</name><uri>http://www.blogger.com/profile/03595535035189960906</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7557263183336868289.post-4910237843097831457</id><published>2011-02-21T14:15:00.000-08:00</published><updated>2011-02-21T15:21:07.943-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Code Refactoring'/><category scheme='http://www.blogger.com/atom/ns#' term='Motivational'/><title type='text'>Little victories make up the big victory</title><content type='html'>Hi guys!&lt;br /&gt;&lt;br /&gt;This will be my first blog post, not directly related to problems, code snippets and etc. I hope I'll write more like these in the future!&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Code refactoring&lt;/span&gt;&lt;br /&gt;In our daily work we frequently do refactoring. Sometimes we do some pieces of code refactoring, which we enjoy as we see the code becoming better and better and we feel happy and satisfied about that. But sometimes, we have to refactor something bigger, some spaghetti code with much legacy in it.&lt;br /&gt;I've encountered such case recently. I struggled with it for few days and was highly disappointed by my performance.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Don't underestimate your tasks&lt;/span&gt;&lt;br /&gt;I was frustrated as nothing was working. I've taken a wrong decision to cut-off the whole project I wanted to refactor and rewrite it. Thinking it will be an easy job to do, I didn't pay much attention, neither estimated accurately all the functionality I had to cover. &lt;br /&gt;&lt;br /&gt;What happened was that it turned out to be tougher than expected. I had erased everything old, the whole solution couldn't be build. I couldn't even cover all the functionality, yet. This all happened for few days in which I've been feeling miserable. It was a dead-end thing and I haven't felt that way for some time. I was close to resigning from this task, as the deadline was coming closer and I was becoming more and more heartless. But admitting that you have failed with a task is something hard to live with (at least for me).&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Don't lose hope, change directions&lt;/span&gt;&lt;br /&gt;Then, I decided to change completely my plan I've firstly tried to follow. I commented most of the code I haven't refactored yet and struggled with building the other code. I've spent few hours on this, but finally built the project (&lt;span style="font-style:italic;"&gt;Victory 1&lt;/span&gt;). I've started testing and bug fixing (&lt;span style="font-style:italic;"&gt;Victory 2..N&lt;/span&gt;). It was such a relief. This whole system was alive and beating again!&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Get motivated, get the work done&lt;/span&gt;&lt;br /&gt;I saw a light in the dark - this whole thing might start working. Fully motivated, I continued iteratively refactoring the remaining part of the project. This took me some time, but in the end, everything was fine and I was satisfied with my work.&lt;br /&gt;&lt;br /&gt;The moral of this story is, that when firstly I got my project built successfully this was actually my first victory - and it felt great. When I started testing, bug-fixing and improving my code I achieved my next victories. From this time on the job was easy to do.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Small victories, make up the big victory&lt;/span&gt;&lt;br /&gt;Sometimes, you need to get to the small victories as fast as possible, to have the power and motivation to continue until you win the battle.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Learn from your mistakes and don't repeat them&lt;/span&gt;&lt;br /&gt;Next time, I'll certainly pay more attention to the details, estimate them and try to iteratively do things, not taking them all at once.&lt;br /&gt;&lt;br /&gt;Keep tuned, for more stories of failures and successes!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7557263183336868289-4910237843097831457?l=yyosifov.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://yyosifov.blogspot.com/feeds/4910237843097831457/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7557263183336868289&amp;postID=4910237843097831457' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/4910237843097831457'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/4910237843097831457'/><link rel='alternate' type='text/html' href='http://yyosifov.blogspot.com/2011/02/little-victories-make-up-big-victory.html' title='Little victories make up the big victory'/><author><name>cypressx</name><uri>http://www.blogger.com/profile/03595535035189960906</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7557263183336868289.post-191366079296859821</id><published>2011-01-16T09:21:00.000-08:00</published><updated>2011-01-16T10:14:59.967-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Security'/><category scheme='http://www.blogger.com/atom/ns#' term='ASP.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='Algorithms'/><title type='text'>Securing your application's configuration data</title><content type='html'>&lt;span style="font-style:italic;"&gt;&lt;span style="font-weight:bold;"&gt;Hi guys!&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;We all add our passwords, connection strings and etc in our config files. But how secure are they? I'll focus on Web Applications.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;&lt;span style="font-style:italic;"&gt;Basic security&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Internet Information Services (IIS) - the most popular server for ASP.NET Applications is by default set to reject all requests to files with extensions .config. So, any file on your web server, named as *.config is safe and cannot be viewed.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;&lt;span style="font-style:italic;"&gt;Next steps&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;However, let's consider another scenario. What if we want to protect ourselves from everyone who actually have access to our web servers? We can use 3 approaches - it's only up to you to choose how to combine them:&lt;br /&gt;&lt;br /&gt;1. &lt;span style="font-style:italic;"&gt;Built-in web.config encryption&lt;/span&gt; - By default ASP.NET provides as with the functionality to encrypt the whole web.config or a configuration section in it. After we encrypt it the IIS server automatically decrypts it when starts reading the web.config file. Encrypting is simple and straightforward:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;aspnet_regiis -pef&lt;/span&gt; ConfigurationSectionName&lt;br /&gt;&lt;br /&gt;Example is explained here: &lt;a href="Encrypting Config Section"&gt;http://odetocode.com/blogs/scott/archive/2006/01/08/encrypting-custom-configuration-sections.aspx&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;2. &lt;span style="font-style:italic;"&gt;Custom encryption&lt;/span&gt; - In most cases, we're collaborating with other people when working on projects and our source is saved into a Source Control. If we want to hide some sensitive information, we can't actually check-in an encrypted config section, because we won't be able to change the settings there anymore. &lt;br /&gt;&lt;br /&gt;What we can do now is implement a custom Encryption and Decryption algorithm. Then we'll be able to save an encrypted version of the password in the config file and decrypt it before we start using it. We use plenty of approaches for doing this - use standard algorithm like Rijndael or write a custom encryption/decryption function. Someone can still decrypt the password, but it's not in plain text anymore.&lt;br /&gt;&lt;br /&gt;3. &lt;span style="font-style:italic;"&gt;Operating System access control usage&lt;/span&gt; - We can go one step further - we can physically deny access to the configuration files for anyone except the Application Pool user of our Web Application. This way no one logged on the machine won't be able to see the contents of these files. &lt;br /&gt;&lt;br /&gt;This security is useful, when there are people having access to the server - they can log in or browse it through network share. Restricting the permissions only to the Application Pool User will prevent everyone else from reading the contents of these files.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;&lt;span style="font-style:italic;"&gt;In Conclusion&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Security is an important part of our applications. All ways to add security to your application in this article are quite simple. You can combine any set of them, until you're satisfied with the level of security or meet your project's security requirements. &lt;br /&gt;&lt;br /&gt;You can leave a comment, if you have some additions to this list of security actions.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7557263183336868289-191366079296859821?l=yyosifov.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://yyosifov.blogspot.com/feeds/191366079296859821/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7557263183336868289&amp;postID=191366079296859821' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/191366079296859821'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/191366079296859821'/><link rel='alternate' type='text/html' href='http://yyosifov.blogspot.com/2011/01/securing-your-applications.html' title='Securing your application&apos;s configuration data'/><author><name>cypressx</name><uri>http://www.blogger.com/profile/03595535035189960906</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7557263183336868289.post-1368979743710277254</id><published>2010-10-30T10:57:00.000-07:00</published><updated>2010-10-30T12:12:56.519-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='Performance Optimizations'/><category scheme='http://www.blogger.com/atom/ns#' term='Algorithms'/><title type='text'>Bitmasks demystified</title><content type='html'>&lt;span style="font-weight:bold;"&gt;&lt;span style="font-style:italic;"&gt;Hi guys!&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Using bitmasks, has been always a powerful technique for storing small set of different flags in only one variable. A flag can represent anything, which we may want to check whether is ON or OFF.&lt;br /&gt;&lt;br/&gt;&lt;span style="font-weight:bold;"&gt;&lt;span style="font-style:italic;"&gt;What is a bitmask?&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Bitmask only sounds scary and complex, but it actually is something very simple. It's only the name of the representation of one number in Binary Notation. For instance, let's look at a few representations:&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;&lt;span style="font-weight:bold;"&gt;00010&lt;/span&gt; is the number 2&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;span style="font-weight:bold;"&gt;00100&lt;/span&gt; is the number 4&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;span style="font-weight:bold;"&gt;00101&lt;/span&gt; is the number 5&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;Still, we don't see the number 5 as "00101", but we can use &lt;a href="http://msdn.microsoft.com/en-us/library/6a71f45d%28VS.71%29.aspx"&gt;Bitwise Operators&lt;/a&gt; to check whether on some position of the binary representation of the number there is 0 or 1.&lt;br /&gt;&lt;br/&gt;&lt;span style="font-weight:bold;"&gt;&lt;span style="font-style:italic;"&gt;What can we use a bitmask for?&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;The bitmask can be used to store some states. In C#, there is the &lt;a href="http://msdn.microsoft.com/en-us/library/system.flagsattribute.aspx"&gt;FlagsAttribute&lt;/a&gt; which actually states, that the enum we're creating will be used for bit manipulation. One of the most popular enums, which is decorated with the FlagsAttribute is the &lt;a href="http://msdn.microsoft.com/en-us/library/system.reflection.bindingflags.aspx"&gt;BindingFlags&lt;/a&gt; enum.&lt;br /&gt;So, if we want to store small number of flags, we can use the bitmask.&lt;br /&gt;&lt;br/&gt;&lt;span style="font-weight:bold;"&gt;&lt;span style="font-style:italic;"&gt;How does the bitmask work?&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;By setting as number values of our Enum members the powers of the 2, we end up having an enum collection like the one below:&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#808030; '&gt;[&lt;/span&gt;Flags&lt;span style='color:#808030; '&gt;]&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;enum&lt;/span&gt; CustomFileMode &lt;span style='color:#808030; '&gt;:&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;    Create &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#008c00; '&gt;1&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;br /&gt;    Open &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#008c00; '&gt;2&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;br /&gt;    Append &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#008c00; '&gt;4&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;br /&gt;    Truncate &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#008c00; '&gt;8&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The bit representation of the same collection is as follows:&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;Create - 0001&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Open - 0010&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Append - 0100&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Truncate - 1000&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;Now, if we have the number 5 (0101), using the Bitwise Operation &amp;, we can check whether any of the Enum members is set on. How do we perform the check? We evaluate the Flag &amp; Bitmask statement. This will give us a positive result, if the Flag is set as On in the Bitmask. (i.e. it's 1 in the bitmask's binary representation).&lt;br /&gt;&lt;br/&gt;&lt;br /&gt;When checking for Truncate flag, we evaluate the (CustomFileMode.Truncate &amp; 5) statement and see that the result is (1000 &amp; 0101 = 0000), so the Truncate flag is not turned on. (because, the result is zero)&lt;br /&gt;&lt;br/&gt;&lt;br /&gt;If we check for the Append flag, we evaluate the (CustomFileMode.Append &amp; 5) statement and the result is (0100 &amp; 0101 = 0100), which is greater than zero - so the Flag CustomFileMode.Append is turned on.&lt;br /&gt;&lt;br/&gt;&lt;br /&gt;As we have selected only numbers which are a power of the 2, we don't have any duplicate values in binary representation, as a power of the 2 is always having just one 1 in it's binary representation.&lt;br/&gt;&lt;br /&gt;How do we set a flag? We use the bitwise operator | (OR). Let's start with an empty number 0 and we want to set the flags Append and Create. What we can do is:&lt;br /&gt;&lt;br /&gt;0000 | Append (0100) = 0100&lt;br /&gt;0100 | Create (0001) = 0101&lt;br /&gt;&lt;br /&gt;We now see, that in the result 0101, we have both Append and Create flags set.&lt;br /&gt; &lt;br /&gt;&lt;span style="font-weight:bold;"&gt;&lt;span style="font-style:italic;"&gt;How do we use a  Bitmask in C#&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;We'll examine one simple example, which outlines how easy it is to create a bitmask in C#. The steps to follow are: &lt;br /&gt;1. Create a Enum which derives from an integer class&lt;br /&gt;2. Decorate the Enum with FlagsAttribute&lt;br /&gt;3. Create the values of the Enum, using only powers of the 2&lt;br /&gt;4. Write your own business logic for checking the Flags in a bitmask using the Enum&lt;br /&gt;Here is a sample Console Application code, which you can play with to explore the Bitmasks:&lt;br /&gt;&lt;pre style='color:#000000;background:#ffffff;'&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;using&lt;/span&gt; System&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;using&lt;/span&gt; System&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Collections&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Generic&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;namespace&lt;/span&gt; BitmaskExample&lt;br /&gt;&lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;class&lt;/span&gt; Program&lt;br /&gt;    &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;static&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;void&lt;/span&gt; Main&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; font-weight:bold; '&gt;string&lt;/span&gt;&lt;span style='color:#808030; '&gt;[&lt;/span&gt;&lt;span style='color:#808030; '&gt;]&lt;/span&gt; args&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#696969; '&gt;// Create a new CustomFileMode. We set the Open, Create and Truncate operations on.&lt;/span&gt;&lt;br /&gt;            CustomFileMode fileModes &lt;span style='color:#808030; '&gt;=&lt;/span&gt; CustomFileMode&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Open | CustomFileMode&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Create | CustomFileMode&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Truncate&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt; &lt;span style='color:#808030; '&gt;(&lt;/span&gt;IsFlagSet&lt;span style='color:#808030; '&gt;(&lt;/span&gt;fileModes&lt;span style='color:#808030; '&gt;,&lt;/span&gt; CustomFileMode&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Append&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;                Console&lt;span style='color:#808030; '&gt;.&lt;/span&gt;WriteLine&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;Append flag is set.&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt; &lt;span style='color:#808030; '&gt;(&lt;/span&gt;IsFlagSet&lt;span style='color:#808030; '&gt;(&lt;/span&gt;fileModes&lt;span style='color:#808030; '&gt;,&lt;/span&gt; CustomFileMode&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Open&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;                Console&lt;span style='color:#808030; '&gt;.&lt;/span&gt;WriteLine&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;Open flag is set.&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt; &lt;span style='color:#808030; '&gt;(&lt;/span&gt;IsFlagSet&lt;span style='color:#808030; '&gt;(&lt;/span&gt;fileModes&lt;span style='color:#808030; '&gt;,&lt;/span&gt; CustomFileMode&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Create&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;                Console&lt;span style='color:#808030; '&gt;.&lt;/span&gt;WriteLine&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;Create flag is set.&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;if&lt;/span&gt; &lt;span style='color:#808030; '&gt;(&lt;/span&gt;IsFlagSet&lt;span style='color:#808030; '&gt;(&lt;/span&gt;fileModes&lt;span style='color:#808030; '&gt;,&lt;/span&gt; CustomFileMode&lt;span style='color:#808030; '&gt;.&lt;/span&gt;Truncate&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;                Console&lt;span style='color:#808030; '&gt;.&lt;/span&gt;WriteLine&lt;span style='color:#808030; '&gt;(&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#0000e6; '&gt;Truncate flag is set.&lt;/span&gt;&lt;span style='color:#800000; '&gt;"&lt;/span&gt;&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style='color:#696969; '&gt;/// &amp;lt;summary&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#696969; '&gt;/// Checks if the flag is turned on in the bitmask&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#696969; '&gt;/// &amp;lt;/summary&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#696969; '&gt;/// &amp;lt;param name="bitmask"&gt;The bitmask - consisting of all the Flags turned on&amp;lt;/param&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#696969; '&gt;/// &amp;lt;param name="flag"&gt;The flag we're checking for&amp;lt;/param&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#696969; '&gt;/// &amp;lt;returns&gt;True if the flags is on, otherwise returns false.&amp;lt;/returns&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800000; font-weight:bold; '&gt;static&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;bool&lt;/span&gt; IsFlagSet&lt;span style='color:#808030; '&gt;(&lt;/span&gt;CustomFileMode bitmask&lt;span style='color:#808030; '&gt;,&lt;/span&gt; CustomFileMode flag&lt;span style='color:#808030; '&gt;)&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;            &lt;span style='color:#800000; font-weight:bold; '&gt;return&lt;/span&gt; &lt;span style='color:#808030; '&gt;(&lt;/span&gt;bitmask &amp;amp; flag&lt;span style='color:#808030; '&gt;)&lt;/span&gt; &lt;span style='color:#808030; '&gt;!&lt;/span&gt;&lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#008c00; '&gt;0&lt;/span&gt;&lt;span style='color:#800080; '&gt;;&lt;/span&gt;&lt;br /&gt;        &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style='color:#696969; '&gt;/// &amp;lt;summary&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#696969; '&gt;/// The CustomFileMode is an Enum, which states are with values the power of 2.&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#696969; '&gt;/// &amp;lt;/summary&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#808030; '&gt;[&lt;/span&gt;Flags&lt;span style='color:#808030; '&gt;]&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800000; font-weight:bold; '&gt;public&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;enum&lt;/span&gt; CustomFileMode &lt;span style='color:#808030; '&gt;:&lt;/span&gt; &lt;span style='color:#800000; font-weight:bold; '&gt;int&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800080; '&gt;{&lt;/span&gt;&lt;br /&gt;        Create &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#008c00; '&gt;1&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;br /&gt;        Open &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#008c00; '&gt;2&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;br /&gt;        Append &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#008c00; '&gt;4&lt;/span&gt;&lt;span style='color:#808030; '&gt;,&lt;/span&gt;&lt;br /&gt;        Truncate &lt;span style='color:#808030; '&gt;=&lt;/span&gt; &lt;span style='color:#008c00; '&gt;8&lt;/span&gt;&lt;br /&gt;    &lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style='color:#800080; '&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;&lt;span style="font-style:italic;"&gt;In Conclusion&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;I hope, this post sheds more light for you on the Bitmask concept. Your questions and suggestions are as always welcome!&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;&lt;span style="font-style:italic;"&gt;Tip&lt;/span&gt;&lt;/span&gt;&lt;br/&gt; You can choose a .NET type to inherit your Enum colleciton, based on how many different flags you need to set and check. The range of values, that can be stored depends on the number of bits in the type. Below are the 3 most common types: &lt;br /&gt;&lt;span style="font-weight:bold;"&gt;short (8 bits)&lt;/span&gt; - up to 7 different flags.&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;int (32 bits)&lt;/span&gt; - up to 31 different flags.&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;long  (64 bits)&lt;/span&gt; - up to 63 different flags.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7557263183336868289-1368979743710277254?l=yyosifov.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://yyosifov.blogspot.com/feeds/1368979743710277254/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7557263183336868289&amp;postID=1368979743710277254' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/1368979743710277254'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/1368979743710277254'/><link rel='alternate' type='text/html' href='http://yyosifov.blogspot.com/2010/10/bitmasks-demystified.html' title='Bitmasks demystified'/><author><name>cypressx</name><uri>http://www.blogger.com/profile/03595535035189960906</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7557263183336868289.post-2734772079214939106</id><published>2010-06-01T12:56:00.000-07:00</published><updated>2010-06-01T13:40:37.119-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ASP.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='Algorithms'/><title type='text'>ASP.NET FindControl recursively - a simple iterative approach</title><content type='html'>Hi guys!&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&lt;span style="font-style: italic;"&gt;The problem:&lt;br/&gt;&lt;/span&gt;&lt;/span&gt;A common problem most people face when working with ASP.NET pages is the &lt;a href="http://msdn.microsoft.com/en-us/library/486wc64h.aspx"&gt;FindControl(string id)&lt;/a&gt; method. It works great, but it does only what it's supposed to - it searches for the control in some naming container. What if you have multiple naming containers nested? In this case, the FindControl method is not working anymore.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&lt;span style="font-style: italic;"&gt;The idea of the solution:&lt;br/&gt;&lt;/span&gt;&lt;/span&gt;The idea is very simple, because you have only one choice - search through the whole container hierarchy until you find the control you are searching for or you have traversed all the controls.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&lt;span style="font-style: italic;"&gt;Recursive solution:&lt;br/&gt;&lt;/span&gt;&lt;/span&gt;There are many places, where the recursive solution is explained, for instance: &lt;a href="http://www.west-wind.com/WebLog/posts/5127.aspx"&gt;ASP.NET 2.0 MasterPages and FindControl()&lt;/a&gt; is probably the most famous one.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&lt;span style="font-style: italic;"&gt;Simple iterative approach:&lt;br/&gt;&lt;/span&gt;&lt;/span&gt;Playing with algorithms is always fun. The recursive solution is nothing more than an implementation of a standard traversal algorithm - &lt;a href="http://en.wikipedia.org/wiki/Depth-first_search"&gt;Depth First Search Algorithm&lt;/a&gt;. It's simple, easy and working. But what if we want to use an iterative solution? Then the answer is just to implement the &lt;a href="http://en.wikipedia.org/wiki/Breadth-first_search"&gt;Breadth First Search Algorithm&lt;/a&gt;. A sample implementation follows below:&lt;br /&gt;&lt;br/&gt;&lt;br /&gt;&lt;code&gt;&lt;span style="font-family:Courier New;font-size:85%;color:black;"&gt;  &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;Control&lt;/span&gt; FindControlById(&lt;span style="color:#0000ff;"&gt;Control&lt;/span&gt; containerControl, &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;string&lt;/span&gt; controlId)&lt;br /&gt;  {&lt;br /&gt;    &amp;nbsp;&amp;nbsp;Queue&lt;&lt;span style="color:#0000ff;"&gt;Control&lt;/span&gt;&gt; queue = &lt;span style="color:#0000ff;"&gt;new&lt;/span&gt; Queue&lt;&lt;span style="color:#0000ff;"&gt;Control&lt;/span&gt;&gt;();&lt;br /&gt;    &amp;nbsp;&amp;nbsp;queue.Enqueue(containerControl);&lt;br /&gt;&lt;br /&gt;    &amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;while&lt;/span&gt;(queue.Count &gt; 0)&lt;br /&gt;    &amp;nbsp;&amp;nbsp;{&lt;br /&gt;      &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;Control&lt;/span&gt; currentControl = queue.Dequeue();&lt;br /&gt;&lt;br /&gt;      &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;(currentControl.ID == controlId)&lt;br /&gt;      &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;        &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; currentControl;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;      &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;foreach&lt;/span&gt;(&lt;span style="color:#0000ff;"&gt;Control&lt;/span&gt; childControl &lt;span style="color:#0000ff;"&gt;in&lt;/span&gt; currentControl.Controls)&lt;br /&gt;      &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;      &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;queue.Enqueue(childControl);&lt;br /&gt;      &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;    &amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;    &amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;;&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;&lt;span style="font-style:italic;"&gt;Happy coding!&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7557263183336868289-2734772079214939106?l=yyosifov.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://yyosifov.blogspot.com/feeds/2734772079214939106/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7557263183336868289&amp;postID=2734772079214939106' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/2734772079214939106'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/2734772079214939106'/><link rel='alternate' type='text/html' href='http://yyosifov.blogspot.com/2010/06/aspnet-findcontrol-recursively-simple.html' title='ASP.NET FindControl recursively - a simple iterative approach'/><author><name>cypressx</name><uri>http://www.blogger.com/profile/03595535035189960906</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7557263183336868289.post-6674982484448603879</id><published>2010-02-05T12:58:00.000-08:00</published><updated>2010-02-05T13:51:24.103-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='IIS7'/><category scheme='http://www.blogger.com/atom/ns#' term='Silverlight'/><category scheme='http://www.blogger.com/atom/ns#' term='Performance Optimizations'/><category scheme='http://www.blogger.com/atom/ns#' term='WCF'/><title type='text'>Applying GZIP Compression to WCF Services</title><content type='html'>Hi guys!&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&lt;span style="font-style: italic;"&gt;Tweaking the web application's performance&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Improving the web application's performance is getting more and more important these days. Recently, I have researched the approaches to gzip(compress) your WCF Service responses, which reduces the responses size in common 5 to 1 ratio. This results in faster transfer of the responses and less bandwidth. For instance, if you had responses with size 30kb, they will become 6kb when compressed, without losing anything. Sounds great, doesn't it?&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&lt;span style="font-style: italic;"&gt;3 easy ways to enable GZIP compression on your WCF Service:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&lt;span style="font-style: italic;"&gt;1. Using IIS7&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;In case you are using IIS7, enabling the GZIP compression for the server's responses comes on the fly: &lt;a href="http://blogs.iis.net/ksingla/archive/2006/06/13/changes-to-compression-in-iis7.aspx"&gt;http://blogs.iis.net/ksingla/archive/2006/06/13/changes-to-compression-in-iis7.aspx&lt;/a&gt;&lt;br /&gt;As you see, using IIS7 to compress the WCF responses is easy and straight-forward, but not everyone is running IIS7 on their production servers. So, let's think of other options, not including the usage of IIS7.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&lt;span style="font-style: italic;"&gt;2. Writing a custom message encoder&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Fortunately, Microsoft have covered this case, too. You can use their WCF sample for Custom Message Encoder: &lt;a href="http://msdn.microsoft.com/en-us/library/ms751458.aspx"&gt;http://msdn.microsoft.com/en-us/library/ms751458.aspx&lt;/a&gt; and use it in your WCF Services. You have to read and understand the sample, then modify it for your needs.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&lt;span style="font-style: italic;"&gt;3. Using Telerik RadCompression&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;In case, you are finding the Custom Message Encoder sample too complicated or you are trying to use your WCF Service with Silverlight(which currently supports only basic/binary bindings), the easiest way to compress the responses, without having hard time with making things work is using the Telerik's RadCompression: &lt;a href="http://www.telerik.com/help/aspnet-ajax/radcompression.html"&gt;http://www.telerik.com/help/aspnet-ajax/radcompression.html&lt;/a&gt;. Using the RadCompression, you just define the RadCompression HttpModule in your WCF Service's web.config and it instantly starts working, even when communicating with Silverlight.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;&lt;span style="font-style: italic;"&gt;In conclusion&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;As I had to apply gzip compression for a WCF Service, communicating with a Silverlight Application (using a Custom Binary Encoding - &lt;a href="http://blogs.msdn.com/silverlightws/archive/2009/03/20/what-s-new-with-web-services-in-silverlight-3-beta.aspx"&gt;http://blogs.msdn.com/silverlightws/archive/2009/03/20/what-s-new-with-web-services-in-silverlight-3-beta.aspx&lt;/a&gt;), without using an IIS7, I had hard time trying to modify the Custom Message Encoder Sample to work in my case, so I went using the Telerik's RadCompression module, which is very fast and easy to setup.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;&lt;span style="font-style:italic;"&gt;Happy optimizing!&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7557263183336868289-6674982484448603879?l=yyosifov.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://yyosifov.blogspot.com/feeds/6674982484448603879/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7557263183336868289&amp;postID=6674982484448603879' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/6674982484448603879'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/6674982484448603879'/><link rel='alternate' type='text/html' href='http://yyosifov.blogspot.com/2010/02/applying-gzip-compression-to-wcf.html' title='Applying GZIP Compression to WCF Services'/><author><name>cypressx</name><uri>http://www.blogger.com/profile/03595535035189960906</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7557263183336868289.post-2265216714513516415</id><published>2009-07-10T00:43:00.000-07:00</published><updated>2009-07-10T00:58:51.655-07:00</updated><title type='text'>Using TFS SDK on 64-bit machine</title><content type='html'>Hi, guys!&lt;br /&gt;&lt;br /&gt;&lt;em&gt;&lt;strong&gt;TFS SDK&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;Using the TFS SDK is pretty easy. You read some articles and documentation from the msdn: &lt;a href="http://msdn.microsoft.com/en-us/library/bb130146(VS.80).aspx"&gt;Team Foundation Server SDK&lt;/a&gt;. Then just add the appropriate assemblies (for example Microsoft.TeamFoundation.Client and Microsoft.TreamFoundation.WorkItemTracking.Client) and enjoy writing TFS accessing applications. &lt;br /&gt;&lt;br /&gt;&lt;em&gt;&lt;strong&gt;What's the problem with 64-bit machine?&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;Creating and building the project is easy. Some problems occur when you try to run it, though. I encountered error like this one:&lt;br /&gt;&lt;br /&gt;"Could not load file or assembly 'Microsoft.TeamFoundation.Client, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified."&lt;br /&gt;&lt;br /&gt;The problem is that it appears that the TFS SDK is only &lt;em&gt;&lt;strong&gt;x86 (32-bit)&lt;/strong&gt;&lt;/em&gt;.&lt;br /&gt;&lt;br /&gt;&lt;em&gt;&lt;strong&gt;The solution&lt;/strong&gt;&lt;/em&gt; &lt;br /&gt;What you have to do is just change the Platform target to x86 in the Project/Properties/Build tab in your Visual Studio, and everything will work again for you. It sounds very easy, but I wasted few hours to figure this out.&lt;br /&gt;&lt;br /&gt;&lt;em&gt;&lt;strong&gt;Happy Coding!&lt;/strong&gt;&lt;/em&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7557263183336868289-2265216714513516415?l=yyosifov.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://yyosifov.blogspot.com/feeds/2265216714513516415/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7557263183336868289&amp;postID=2265216714513516415' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/2265216714513516415'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/2265216714513516415'/><link rel='alternate' type='text/html' href='http://yyosifov.blogspot.com/2009/07/using-tfs-sdk-on-64-bit-machine.html' title='Using TFS SDK on 64-bit machine'/><author><name>cypressx</name><uri>http://www.blogger.com/profile/03595535035189960906</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7557263183336868289.post-3138995143830580128</id><published>2009-06-01T06:46:00.000-07:00</published><updated>2009-06-01T07:12:04.763-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Architecting Web Applications'/><category scheme='http://www.blogger.com/atom/ns#' term='ASP.NET'/><title type='text'>Creating a useful BasePage</title><content type='html'>&lt;strong&gt;&lt;em&gt;When people start a new project,&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;creating a Base Page for all other pages is a must! I found a very interesting, yet simple and concrete article about the Base Page and some of the useful methods, which can be included there. This Base Page is great for avoiding code duplication and has a good level of error handling.&lt;br /&gt;&lt;br /&gt;You can read the article here:&lt;a href="http://www.devexpertise.com/2009/03/25/aspnet-tiptrick-use-a-base-page-class-for-all-application-pages/"&gt; ASP.NET Tip/Trick: Use a Base Page Class for All Application Pages&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;em&gt;&lt;strong&gt;Page Refresh Handling&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;&lt;strong&gt;&lt;em&gt;&lt;/em&gt;&lt;/strong&gt;What I find useful adding to this Base Page, is a functionality for recognizing whether there is a &lt;em&gt;Page Refresh&lt;/em&gt; occured (when the user presses F5 or Refresh button), because as you know, the Page Refresh actually does again what the last page post back was - all the events and stuff.&lt;br /&gt;For example, when you click the &lt;em&gt;button next&lt;/em&gt; of some results set you expect to move to the next page. What happens if you hit refresh after the rebinding? It actually executes again all the events from the previous post back, so it will move you to the next page, which you didn't want to. What is worse, it can break your application, because there can be no more results and no 'next' page. One way to handle this situation is to write some code in the Base Page so that you can track whether a Page Refresh occured and the other most common way is to stop/invalidate the page after refresh.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;em&gt;In conclusion, &lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;em&gt;&lt;/em&gt;&lt;/strong&gt;I must remind that creating a common &lt;em&gt;Base User Control&lt;/em&gt; is also useful and should be used as a best practice, too !&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7557263183336868289-3138995143830580128?l=yyosifov.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://yyosifov.blogspot.com/feeds/3138995143830580128/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7557263183336868289&amp;postID=3138995143830580128' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/3138995143830580128'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/3138995143830580128'/><link rel='alternate' type='text/html' href='http://yyosifov.blogspot.com/2009/06/creating-useful-basepage.html' title='Creating a useful BasePage'/><author><name>cypressx</name><uri>http://www.blogger.com/profile/03595535035189960906</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7557263183336868289.post-3832330283182707130</id><published>2009-04-21T05:40:00.000-07:00</published><updated>2009-04-21T05:57:22.033-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='Visual Studio'/><title type='text'>Visual Studio 2008 Keybinding Reference Poster</title><content type='html'>&lt;em&gt;&lt;strong&gt;Hi, guys !&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;&lt;br /&gt;This can sound like a promoting post for some of you, but I recently found this official Visual Studio 2008 Keybinding Reference Poster, which can be downloaded freely from &lt;a href="http://www.microsoft.com/downloads/thankyou.aspx?familyId=e5f902a8-5bb5-4cc6-907e-472809749973&amp;displaylang=en"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;I find it very educative,useful and quite inspiring for myself - hanging there on the wall just in front of me. No more wondering how the Format Document or Format Selection key combination was.&lt;br /&gt;&lt;br /&gt;&lt;em&gt;&lt;strong&gt;Luckily&lt;/strong&gt;&lt;/em&gt;,&lt;br /&gt;Microsoft have created such Reference Posters for other tools and technologies too, so I will be enjoying most of them soon.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;em&gt;More Reference Posters&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;&lt;a href="http://www.microsoft.com/downloads/details.aspx?familyid=7B645F3A-6D22-4548-A0D8-C2A27E1917F8&amp;displaylang=en"&gt;.NET Framework 3.5 Common Namespaces and Types Poster&lt;/a&gt;&lt;a href="http://www.microsoft.com/downloads/details.aspx?displaylang=en&amp;FamilyID=811d8ad6-8d48-4684-b08c-686462d58a56#filelist"&gt;&lt;br /&gt;Microsoft® Silverlight™ 1.1 Alpha Developer Reference Poster&lt;br /&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7557263183336868289-3832330283182707130?l=yyosifov.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://yyosifov.blogspot.com/feeds/3832330283182707130/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7557263183336868289&amp;postID=3832330283182707130' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/3832330283182707130'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/3832330283182707130'/><link rel='alternate' type='text/html' href='http://yyosifov.blogspot.com/2009/04/visual-studio-2008-keybinding-reference.html' title='Visual Studio 2008 Keybinding Reference Poster'/><author><name>cypressx</name><uri>http://www.blogger.com/profile/03595535035189960906</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7557263183336868289.post-3665966549235084643</id><published>2009-03-24T04:10:00.001-07:00</published><updated>2009-04-14T04:22:08.105-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Javascript'/><category scheme='http://www.blogger.com/atom/ns#' term='Internet Explorer'/><title type='text'>getElementById compared in IE6, IE7 and IE8</title><content type='html'>Hi guys!&lt;br /&gt;&lt;br /&gt;These days I was enjoying the new &lt;strong&gt;&lt;em&gt;Internet Explorer 8&lt;/em&gt;&lt;/strong&gt; and were observing the differences with the previous versions of the browser.&lt;br /&gt;&lt;br /&gt;I encountered a problem connected with the &lt;em&gt;&lt;strong&gt;getElementById&lt;/strong&gt;&lt;/em&gt; method. It was working OK in IE6 and IE7, and now in IE8 it suddenly stopped working. &lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;em&gt;How does getElementById works in IE6/IE7&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;In IE6 and IE7, the method getElementById would get an element provided either the ID or the Name attribute of the element. What is more, it matches it case-insensitevily.&lt;br /&gt;&lt;br/&gt;&lt;br /&gt;&lt;strong&gt;&lt;em&gt;In Comparison&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;In IE8 it does exactly what it says and what it should do - it gets the element only by the ID attribute and it matches case-sensitively.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;em&gt;Conclusion&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;The reason why the old code wasn't working in IE8 was because someone has used the getElementById to get an element on the page provided it's Name. Which apparently won't work in IE8.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;em&gt;More on this&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;More on this, &lt;a href="http://msdn.microsoft.com/en-us/library/ms536437.aspx"&gt;Here&lt;/a&gt; &lt;em&gt;(Check the Remarks paragraph)&lt;/em&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7557263183336868289-3665966549235084643?l=yyosifov.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://yyosifov.blogspot.com/feeds/3665966549235084643/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7557263183336868289&amp;postID=3665966549235084643' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/3665966549235084643'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/3665966549235084643'/><link rel='alternate' type='text/html' href='http://yyosifov.blogspot.com/2009/03/getelementbyid-compared-in-ie6-ie7-and.html' title='getElementById compared in IE6, IE7 and IE8'/><author><name>cypressx</name><uri>http://www.blogger.com/profile/03595535035189960906</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7557263183336868289.post-6571076501211057385</id><published>2009-03-20T05:31:00.000-07:00</published><updated>2009-04-14T04:22:40.226-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='AJAX'/><category scheme='http://www.blogger.com/atom/ns#' term='ASP.NET'/><title type='text'>asp:FileUpload control in Modal Popup Extender</title><content type='html'>Hello guys !&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;em&gt;Let's go straight to the issue this time. &lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;I have a Modal Popup Extender and want to use the standard FileUpload control in it. What I mean by saying the "standard FileUpload" control is that I will use the asp:FileUpload control and not the AJAX-enabled one which is in the AJAX Control Toolkit.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;em&gt;What's the issue/problem?&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;The problem is that the AJAX makes partial updates and doesn't perform a full postback. The FileUpload control itself works with full postback - in the postback the File which you uploaded is attached in the Request.Files collection. So, when you are not performing a full postback, the file is not uploaded in the Request.Files collection and you cannot get the file to use it - for saving in the database for example.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;em&gt;The solution&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;The Script Manager has one very useful method - RegisterPostBackControl ( http://msdn.microsoft.com/en-us/library/system.web.ui.scriptmanager.registerpostbackcontrol.aspx ). It basically registers a control, which should implicitly do a full postback. In my case, it was enough just to register the FileUpload holding control to perform a full postback and I had the File uploaded into the Request.Files and ready for use.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;em&gt;Conclusion&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;The issue and the solution were pretty straight-forward this time. But it can become trickier, if you don't know the RegisterPostBackControl method.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7557263183336868289-6571076501211057385?l=yyosifov.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://yyosifov.blogspot.com/feeds/6571076501211057385/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7557263183336868289&amp;postID=6571076501211057385' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/6571076501211057385'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/6571076501211057385'/><link rel='alternate' type='text/html' href='http://yyosifov.blogspot.com/2009/03/aspfileupload-control-in-modal-popup.html' title='asp:FileUpload control in Modal Popup Extender'/><author><name>cypressx</name><uri>http://www.blogger.com/profile/03595535035189960906</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7557263183336868289.post-685526524557163949</id><published>2009-03-04T05:19:00.000-08:00</published><updated>2009-04-14T04:23:13.529-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='AJAX'/><category scheme='http://www.blogger.com/atom/ns#' term='ASP.NET'/><title type='text'>ASP.NET Validator in Modal Popup Extender</title><content type='html'>&lt;strong&gt;&lt;em&gt;Yesterday,&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;I had an interesting issue, which I want to share with you.&lt;br /&gt;&lt;br /&gt;&lt;em&gt;&lt;strong&gt;The Case&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;I had a RequiredFieldValidator in a Modal Popup Extender. After closing the popup, everything in my web page freezed. For example, clicking a LinkButton/Button wouldn't do a postback, everything just stopped working.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;em&gt;The Problem&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;After some kind of debugging, I figured out the problem. As you know, the modal popup extender works like this:&lt;br /&gt;1.) When you invoke the .Show() method of the Modal Popup Extender, the AJAX.NET takes the PopupControl you want to Show as a popup and brings it to the front.&lt;br /&gt;2.) When you invoke the .Hide() method or close the Modal Popup Extender from a button inside it, it again takes the PopupControl and hides it in the page.&lt;br /&gt;So, the problem I encountered was, that when the user entered some invalid data and presses the cancel button(to close the ModalPopupExtender and to cancel the changes), the AJAX.NET hided the PopupControl in the page. The problem was, that the RequiredFieldValidator was yielding that there is invalid data, so it was blocking all the postbacks. The cute thing is, that you can't actually see the RequiredFieldValidator, because it's in the Modal Popup which you don't see, too.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;em&gt;The Solution&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;The simplest thing to do, was to clear all the Validators from the ModalPopupExtender, when I hide it. This is not a neat solution for me, but I don't know any better for now.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;em&gt;Conclusion&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;Probably, people encounter such a 'problem' on rare occasions, but I found it useful for myself, and will be aware of this.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7557263183336868289-685526524557163949?l=yyosifov.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://yyosifov.blogspot.com/feeds/685526524557163949/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7557263183336868289&amp;postID=685526524557163949' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/685526524557163949'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/685526524557163949'/><link rel='alternate' type='text/html' href='http://yyosifov.blogspot.com/2009/03/aspnet-validator-in-modal-popup.html' title='ASP.NET Validator in Modal Popup Extender'/><author><name>cypressx</name><uri>http://www.blogger.com/profile/03595535035189960906</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7557263183336868289.post-5123637333292708333</id><published>2009-02-18T01:23:00.000-08:00</published><updated>2009-04-14T04:23:26.226-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='XML'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='Visual Studio'/><title type='text'>XML comment contains invalid XML: Whitespace is not allowed at this location</title><content type='html'>Today, I encountered this error: "XML comment contains invalid XML: Whitespace is not allowed at this location".&lt;br /&gt;&lt;br /&gt;The problem occured in a code similar to this one:&lt;br /&gt;&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Calculates the area &amp; the surface&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;public double CalculateAreaAndSurface()&lt;br /&gt;&lt;br /&gt;* Please, note that this above is just a sample code to illustrate the error, not a meaningful function or well written.&lt;br /&gt;&lt;br /&gt;So, when I started using this function, I called it like this ObjectInstance.CalculateAreaAndSurface(), and the Visual Studio(2005 or 2008) didn't show me the comment I have written, but the error instead: "XML comment contains invalid XML: Whitespace is not allowed at this location".&lt;br /&gt;&lt;br /&gt;After a small research, I figured out that the problem in the comment was that there is the character "&amp;" in it, which should be replaced with the escaped version "&amp; amp;". Some other characters like &lt; and &gt; are in this list of forbidden characters and should be escaped too.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7557263183336868289-5123637333292708333?l=yyosifov.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://yyosifov.blogspot.com/feeds/5123637333292708333/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7557263183336868289&amp;postID=5123637333292708333' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/5123637333292708333'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/5123637333292708333'/><link rel='alternate' type='text/html' href='http://yyosifov.blogspot.com/2009/02/xml-comment-contains-invalid-xml.html' title='XML comment contains invalid XML: Whitespace is not allowed at this location'/><author><name>cypressx</name><uri>http://www.blogger.com/profile/03595535035189960906</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7557263183336868289.post-8989836580522014561</id><published>2009-01-29T06:09:00.000-08:00</published><updated>2009-04-14T04:23:37.805-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='WPF'/><title type='text'>WPF - Is it my preferred choice for graphics ?</title><content type='html'>Hi guys!&lt;br /&gt;&lt;br /&gt; &lt;span style="font-weight:bold;"&gt;I want to share with you some experience with WPF&lt;/span&gt;&lt;br /&gt; Recently, I started doing a very simple Graphics project and encountered some difficulties. I wanted to share with you the thing which frustrated me mostly.&lt;br /&gt;&lt;br /&gt; &lt;span style="font-weight:bold;"&gt;What is the most simple thing we should do when using Graphics?&lt;/span&gt;&lt;br /&gt;For me, this is to load a simple image and manipulate it. This was pretty easy with GDI+ and System.Drawing classes like Image and Bitmap. You had everything needed - you could load a Bitmap simple like this Image.FromImage(path_to_file) and you could as easily manipulate the Bitmap's pixels - Bitmap.SetPixel(x, y, color). I of course wanted to use the same 'tricks' in WPF, but couldn't do it. After some research, I figured out why:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style:italic;"&gt;"GDI+ was a bit different in the sense that the Bitmap object was always cached in memory, so having a SetPixel method made sense. However, this may not be the case in WPF. For applications that do not wish to load the entire image in memory, WPF provides a way to load the image such that it is read from disk on a per-demand basis. In this case, a SetPixel equivalent does not make much sense and would not work. Thus we have chosen to not include this functionality for V1 and will consider such an API for future version."&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;In Conclusion&lt;/span&gt;&lt;br /&gt;Firstly, I couldn't believe this. I could have gone with using Windows Forms Project from the very beginning, but was excited to try the WPF. &lt;br /&gt;I wonder if WPF is grown enough to be used in real projects... ? I will appreciate any professional opinions on the theme.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7557263183336868289-8989836580522014561?l=yyosifov.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://yyosifov.blogspot.com/feeds/8989836580522014561/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7557263183336868289&amp;postID=8989836580522014561' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/8989836580522014561'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/8989836580522014561'/><link rel='alternate' type='text/html' href='http://yyosifov.blogspot.com/2009/01/wpf-is-it-my-preferred-choice-for.html' title='WPF - Is it my preferred choice for graphics ?'/><author><name>cypressx</name><uri>http://www.blogger.com/profile/03595535035189960906</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7557263183336868289.post-8280301035922820470</id><published>2008-10-17T00:08:00.000-07:00</published><updated>2009-04-14T04:24:18.862-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='AJAX'/><category scheme='http://www.blogger.com/atom/ns#' term='Internet Explorer'/><title type='text'>DOCTYPE element, why do we need it ?</title><content type='html'>Hey guys !&lt;br /&gt;&lt;br/&gt;&lt;br /&gt;  I wanna share with you some things about this DOCTYPE element which is included at the top of the html page.&lt;br /&gt;&lt;br/&gt;&lt;br /&gt;  &lt;b&gt;Firstly&lt;/b&gt;, if we are using the Microsoft's AJAX Toolkit, we should ensure that we have a DOCTYPE element included, because some of our controls can freeze and not work. In my case, I was using a ModalPopupExtender which appeared in the upper left corner of the window and everything freezes (for comparision it's expected to be showed in the center of the screen ). For bad, browsers are different and in Firefox this ModalPopupExtender will work without problems, but there's no chance to work in IE. So if something with your AJAX Toolkit is behaving strange the first thing to check is the DOCTYPE element :)&lt;br /&gt;&lt;br/&gt;&lt;br /&gt;  &lt;b&gt;Another&lt;/b&gt; thing which is notable to not work when the DOCTYPE element is missing is the CSS. Some properties like font-size can stop working, or again behave strange .. what is more it will depend on the browser, some things will work on Firefox and won't work in IE .. so it becomes even harder to locate the problem.&lt;br /&gt;&lt;br/&gt;&lt;br /&gt;  &lt;b&gt;To conclude&lt;/b&gt;, if you have strange problems with AjaxToolkit or CSS, firstly ensure to check whether there is a DOCTYPE element in the html. Yes, I know that when Visual Studio creates a page it includes a DOCTYPE element, but in my case someone has removed this DOCTYPE for some reason .. and I had hard times figuring out why the ajaxcontrol toolkit controls was acting so strange.&lt;br /&gt;&lt;br/&gt;&lt;br /&gt;&lt;b&gt;More information&lt;/b&gt; about the doctype and the browser modes:&lt;br /&gt;&lt;a href="http://en.wikipedia.org/wiki/Quirks_mode"&gt;http://en.wikipedia.org/wiki/Quirks_mode&lt;/a&gt; -- quirks mode is the mode the browser enters, when you don't specify a DOCTYPE in all the browsers, when we're talking about the Transitional DTD. (You can check this by looking up in the table for HTML 4.01 spec. and Transitional DTD)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7557263183336868289-8280301035922820470?l=yyosifov.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://yyosifov.blogspot.com/feeds/8280301035922820470/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7557263183336868289&amp;postID=8280301035922820470' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/8280301035922820470'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/8280301035922820470'/><link rel='alternate' type='text/html' href='http://yyosifov.blogspot.com/2008/10/doctype-element-why-do-we-need-it.html' title='DOCTYPE element, why do we need it ?'/><author><name>cypressx</name><uri>http://www.blogger.com/profile/03595535035189960906</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7557263183336868289.post-3376612160530630807</id><published>2008-08-14T00:46:00.000-07:00</published><updated>2009-04-14T04:24:30.013-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ASP.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>Persisting dynamically built GridView's state and data</title><content type='html'>As I promised, there will be one more post about creating dynamically a GridView.&lt;br /&gt;Let’s now see what happens on a Post Back ? &lt;br /&gt;Our GridView created dynamically with template fields is lost. As we know the GridView has a property called “EnableViewState”. If we set this property to “true” we can expect our GridView and it’s data to be persisted after the post back. But that’s not the case. The “EnableViewState” itself saves only the data in the cells of the GridView to the ViewState, so we will again see nothing. What we have to do  (something we should do to every control we have somehow constructed dynamically) is to build the GridView over and over again every time the Page is sent back to the server. We have to do this on an event where we still have the ability to change the Page’s layout and controls included in it. &lt;br /&gt;For example if we are creating an UserControl we should reconstruct the GridView on it’s Init event  ( http://msdn.microsoft.com/en-us/library/system.web.ui.control.init.aspx ). If we are doing this in a Web Page we should place our code in the PreInit event ( http://msdn.microsoft.com/en-us/library/system.web.ui.page.preinit.aspx ).&lt;br /&gt;Now we have accomplished our task to save the GridView after Post Back. Unfortunately, creating dynamically controls needs much more coding, than creating them statically.&lt;br /&gt;The other thing I would like to share is very simple. It answers the question ‘How do we handle the edit of such a custom grid ?’&lt;br /&gt;We will create our own column with Edit/Update/Cancel link buttons. We place a code like this in the GridView control:&lt;br /&gt;&lt;pre class="coloredcode"&gt;&amp;lt;&lt;span class="tag"&gt;asp:TemplateField&lt;/span&gt;&lt;span class="attr"&gt; ShowHeader=&lt;/span&gt;&lt;span class="attrv"&gt;"False"&lt;/span&gt;&amp;gt;&lt;br /&gt;     &amp;lt;&lt;span class="tag"&gt;ItemTemplate&lt;/span&gt;&amp;gt;&lt;br /&gt;         &amp;lt;&lt;span class="tag"&gt;asp:LinkButton&lt;/span&gt;&lt;span class="attr"&gt; ID=&lt;/span&gt;&lt;span class="attrv"&gt;"lbEdit"&lt;/span&gt;&lt;span class="attr"&gt; CommandName=&lt;/span&gt;&lt;span class="attrv"&gt;"Edit"&lt;/span&gt;&lt;span class="attr"&gt; Text=&lt;/span&gt;&lt;span class="attrv"&gt;"Edit"&lt;/span&gt;&lt;span class="attr"&gt; runat=&lt;/span&gt;&lt;span class="attrv"&gt;"server"&lt;/span&gt; /&amp;gt;&lt;br /&gt;&lt;br /&gt;     &amp;lt;/&lt;span class="tag"&gt;ItemTemplate&lt;/span&gt;&amp;gt;&lt;br /&gt;     &amp;lt;&lt;span class="tag"&gt;EditItemTemplate&lt;/span&gt;&amp;gt;&lt;br /&gt;         &amp;lt;&lt;span class="tag"&gt;asp:LinkButton&lt;/span&gt;&lt;span class="attr"&gt; ID=&lt;/span&gt;&lt;span class="attrv"&gt;"lbUpdate"&lt;/span&gt;&lt;span class="attr"&gt; CommandName=&lt;/span&gt;&lt;span class="attrv"&gt;"Update"&lt;/span&gt;&lt;span class="attr"&gt; Text=&lt;/span&gt;&lt;span class="attrv"&gt;"Update"&lt;/span&gt;&lt;span class="attr"&gt; runat=&lt;/span&gt;&lt;span class="attrv"&gt;"server"&lt;/span&gt; /&amp;gt;&lt;br /&gt;&lt;br /&gt;         &amp;lt;&lt;span class="tag"&gt;asp:LinkButton&lt;/span&gt;&lt;span class="attr"&gt; ID=&lt;/span&gt;&lt;span class="attrv"&gt;"lbCancel"&lt;/span&gt;&lt;span class="attr"&gt; CommandName=&lt;/span&gt;&lt;span class="attrv"&gt;"Cancel"&lt;/span&gt;&lt;span class="attr"&gt; Text=&lt;/span&gt;&lt;span class="attrv"&gt;"Cancel"&lt;/span&gt;&lt;span class="attr"&gt; runat=&lt;/span&gt;&lt;span class="attrv"&gt;"server"&lt;/span&gt; /&amp;gt;&lt;br /&gt;&lt;br /&gt;     &amp;lt;/&lt;span class="tag"&gt;EditItemTemplate&lt;/span&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;span class="tag"&gt;asp:TemplateField&lt;/span&gt;&amp;gt;&lt;/pre&gt;&lt;br /&gt;Please, note that we could have just set the property of the GridView “AutoGenerateEditButton” to true to let the framework generate such a column for us.&lt;br /&gt;No we have to handle the events for Update, Cancel, Edit adding code again in the GridView for the events, like this:&lt;br /&gt;&lt;pre class='coloredcode'&gt;OnRowEditing=&lt;span class='st'&gt;"gv_RowEditing"&lt;/span&gt;&lt;br /&gt;OnRowCancelingEdit=&lt;span class='st'&gt;"gv_RowCancelingEdit"&lt;/span&gt;&lt;br /&gt;OnRowUpdating=&lt;span class='st'&gt;"gv_RowUpdating"&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Now the only thing left is to implement the above methods in the code behind to handle the raised events. Sample code goes below: &lt;br /&gt;&lt;pre class='coloredcode'&gt;&lt;span class='kwd'&gt;protected void&lt;/span&gt; gv_RowEditing(&lt;span class='kwd'&gt;object&lt;/span&gt; sender, GridViewEditEventArgs e)&lt;br /&gt;        {&lt;br /&gt;            gv.EditIndex = e.NewEditIndex; &lt;span class='cmt'&gt;// set the edit index, which causes&lt;br /&gt;                                           // the GridView to enter it’s edit&lt;br /&gt;                                           // mode&lt;br /&gt;            // Rebind data here&lt;/span&gt;&lt;br /&gt;        }&lt;br /&gt;        &lt;span class='kwd'&gt;protected void&lt;/span&gt; gv_RowCancelingEdit(&lt;span class='kwd'&gt;object&lt;/span&gt; sender, GridViewCancelEditEventArgs e)&lt;br /&gt;        {&lt;br /&gt;            gv.EditIndex = -1;             &lt;span class='cmt'&gt;// Clear the edit index, and return&lt;br /&gt;                                           // to change the mode again.                                 &lt;br /&gt;            // Rebind data here&lt;/span&gt;&lt;br /&gt;        }&lt;br /&gt;        &lt;span class='kwd'&gt;protected void&lt;/span&gt; gv_RowUpdating(&lt;span class='kwd'&gt;object&lt;/span&gt; sender, GridViewUpdateEventArgs e)&lt;br /&gt;        {&lt;br /&gt;            DataTable dt = TableGV;         &lt;span class='cmt'&gt;// This is my object behind the                        &lt;br /&gt;                                            // GridView which holds it’s data.&lt;br /&gt;                                            // And I want it updated.&lt;/span&gt;&lt;br /&gt;           GridViewRow row = gv.Rows[e.RowIndex];&lt;br /&gt;            &lt;span class='kwd'&gt;for&lt;/span&gt; (&lt;span class='kwd'&gt;int&lt;/span&gt; i = 0; i &lt; dt.Columns.Count; i++)&lt;br /&gt;            {&lt;br /&gt;                dt.Rows[gv.EditIndex][i] = ((TextBox)(row.Cells[i + 1].Controls[0])).Text.Trim(); &lt;span class='cmt'&gt;// Get the data from the GridViewRow&lt;/span&gt;&lt;br /&gt;            }&lt;br /&gt;            gv.EditIndex = -1; &lt;span class='cmt'&gt;// Again change the mode&lt;br /&gt;            // Rebind data here&lt;/span&gt;&lt;br /&gt;        }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;That’s all, we now have a complete editable custom dynamically created GridView Control.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7557263183336868289-3376612160530630807?l=yyosifov.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://yyosifov.blogspot.com/feeds/3376612160530630807/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7557263183336868289&amp;postID=3376612160530630807' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/3376612160530630807'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/3376612160530630807'/><link rel='alternate' type='text/html' href='http://yyosifov.blogspot.com/2008/08/persisting-dynamically-built-gridviews.html' title='Persisting dynamically built GridView&apos;s state and data'/><author><name>cypressx</name><uri>http://www.blogger.com/profile/03595535035189960906</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7557263183336868289.post-2197448342643207017</id><published>2008-07-24T06:49:00.000-07:00</published><updated>2008-07-25T01:19:03.479-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ASP.NET'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>How to build a Grid View dynamically ?</title><content type='html'>Today I will show you something pretty standard - how to build a asp:GridView when you don't know in advance how it will be formatted, what Columns should be bounded...&lt;br /&gt;We have to build the GridView’s structure – it’s Columns. We can see that GridView.Columns.Add method takes a DataControlField as a parameter, so our attention should go to Controls inheriting DataControlField so that we can add them to the Columns collection.&lt;br /&gt;&lt;br /&gt;There are two kinds of columns we can use - a BoundField and a TemplateField.&lt;br /&gt;&lt;br /&gt;The first one – a BoundField , is easy to create and use. We can set properties like DataField,HeaderText, HeaderStyle, ReadOnly and so on. But we can’t set a template. This control should be used when adding a very simple row, which won’t be edited or no complex formatting will be applied to it. It’s the easiest way to add a new column, but it’s not flexible enough for all we can need.  So, BoundField should be used when we don’t want anything complex from the Column.&lt;br /&gt;&lt;br /&gt;The TemplateField on the other hand is powerful. &lt;br /&gt;&lt;br /&gt;&lt;pre class='coloredcode'&gt;&lt;span class='kwd'&gt;namespace&lt;/span&gt; System.Web.UI.WebControls&lt;br /&gt;{&lt;br /&gt;    &lt;span class='kwd'&gt;public class&lt;/span&gt; TemplateField : DataControlField&lt;br /&gt;    {&lt;br /&gt;        &lt;span class='kwd'&gt;public&lt;/span&gt; TemplateField();&lt;br /&gt;&lt;br /&gt;        &lt;span class='kwd'&gt;public virtual&lt;/span&gt; ITemplate AlternatingItemTemplate { &lt;span class='kwd'&gt;get&lt;/span&gt;; &lt;span class='kwd'&gt;set&lt;/span&gt;; }&lt;br /&gt;        &lt;span class='kwd'&gt;public virtual bool&lt;/span&gt; ConvertEmptyStringToNull { &lt;span class='kwd'&gt;get&lt;/span&gt;; &lt;span class='kwd'&gt;set&lt;/span&gt;; }&lt;br /&gt;        &lt;span class='kwd'&gt;public virtual&lt;/span&gt; ITemplate EditItemTemplate { &lt;span class='kwd'&gt;get&lt;/span&gt;; &lt;span class='kwd'&gt;set&lt;/span&gt;; }&lt;br /&gt;        &lt;span class='kwd'&gt;public virtual&lt;/span&gt; ITemplate FooterTemplate { &lt;span class='kwd'&gt;get&lt;/span&gt;; &lt;span class='kwd'&gt;set&lt;/span&gt;; }&lt;br /&gt;        &lt;span class='kwd'&gt;public virtual&lt;/span&gt; ITemplate HeaderTemplate { &lt;span class='kwd'&gt;get&lt;/span&gt;; &lt;span class='kwd'&gt;set&lt;/span&gt;; }&lt;br /&gt;        &lt;span class='kwd'&gt;public virtual&lt;/span&gt; ITemplate InsertItemTemplate { &lt;span class='kwd'&gt;get&lt;/span&gt;; &lt;span class='kwd'&gt;set&lt;/span&gt;; }&lt;br /&gt;        &lt;span class='kwd'&gt;public virtual&lt;/span&gt; ITemplate ItemTemplate { &lt;span class='kwd'&gt;get&lt;/span&gt;; &lt;span class='kwd'&gt;set&lt;/span&gt;; }&lt;br /&gt;        &lt;span class='kwd'&gt;protected override&lt;/span&gt; DataControlField CreateField();&lt;br /&gt;        &lt;span class='kwd'&gt;public override void&lt;/span&gt; ExtractValuesFromCell(IOrderedDictionary dictionary, DataControlFieldCell cell, DataControlRowState rowState, &lt;span class='kwd'&gt;bool&lt;/span&gt; includeReadOnly);&lt;br /&gt;        &lt;span class='kwd'&gt;public override void&lt;/span&gt; InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, &lt;span class='kwd'&gt;int&lt;/span&gt; rowIndex);&lt;br /&gt;        &lt;span class='kwd'&gt;public override void&lt;/span&gt; ValidateSupportsCallback();&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;We can see from it’s class that it contains Properties for all the possible Templates (EditItemTemplate, ItemTemplate, InsertItemTemplate and etc.) .  All templates should implement the ITemplate Interface , which is simple enough – it contains only one method – InstantiateIn(Control container)&lt;br /&gt;&lt;br /&gt;&lt;pre class='coloredcode'&gt;&lt;span class='kwd'&gt;namespace&lt;/span&gt; System.Web.UI&lt;br /&gt;{&lt;br /&gt;    &lt;span class='kwd'&gt;public interface&lt;/span&gt; ITemplate&lt;br /&gt;    {&lt;br /&gt;        &lt;span class='kwd'&gt;void&lt;/span&gt; InstantiateIn(Control container);&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;In this method we should add whatever we like in the container. This method is called for every cell of the GridView, so the container is actually a DataControlField (which is not so important for now, except the fact that should be noted – the DataControlField is not the NamingContainer, but the GridViewRow is).&lt;br /&gt;Now, for example , let’s create a class for ItemTemplate. Here is a sample code for an ItemTemplate:&lt;br /&gt;&lt;pre class='coloredcode'&gt;&lt;span class='kwd'&gt;public class&lt;/span&gt; CustomTemplateField : ITemplate&lt;br /&gt;{&lt;br /&gt;    &lt;span class='kwd'&gt;public&lt;/span&gt; CustomTemplateField()&lt;br /&gt;    {&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span class='kwd'&gt;public void&lt;/span&gt; InstantiateIn(Control container)&lt;br /&gt;    {&lt;br /&gt;        Label label = &lt;span class='kwd'&gt;new&lt;/span&gt; Label(); &lt;br /&gt;        label.DataBinding += &lt;span class='kwd'&gt;new&lt;/span&gt; EventHandler(label_DataBinding); &lt;span class='cmt'&gt;// use the DataBinding event to&lt;br /&gt;                                                                  // set the data to the label&lt;/span&gt;&lt;br /&gt;        container.Controls.Add(label);                            &lt;span class='cmt'&gt;// Simply add the control to the Controls&lt;br /&gt;                                                                  // collection of the container&lt;/span&gt;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span class='kwd'&gt;void&lt;/span&gt; label_DataBinding(&lt;span class='kwd'&gt;object&lt;/span&gt; sender, EventArgs e)&lt;br /&gt;    {&lt;br /&gt;        Label lbl = sender &lt;span class='kwd'&gt;as&lt;/span&gt; Label; &lt;span class='cmt'&gt;// Get the label&lt;/span&gt;&lt;br /&gt;        GridViewRow gvr = lbl.NamingContainer &lt;span class='kwd'&gt;as&lt;/span&gt; GridViewRow; &lt;span class='cmt'&gt;// And get it's naming container&lt;/span&gt;&lt;br /&gt;        &lt;span class='kwd'&gt;string&lt;/span&gt; text = DataBinder.Eval(gvr.DataItem, &lt;span class='st'&gt;"Name"&lt;/span&gt;).ToString(); &lt;span class='cmt'&gt;// We can now use the DataItem&lt;br /&gt;                                                                        // to bind some data to our&lt;br /&gt;                                                                        // Controls in the cell&lt;/span&gt;&lt;br /&gt;        lbl.Text = text; &lt;span class='cmt'&gt;// we can of course here perform some formatting&lt;/span&gt;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Any other Template’s implementation is similar to the above’s one. If we want to provide edit functionality, we should make an EditItemTemplate, for example like this one:&lt;br /&gt;&lt;pre class='coloredcode'&gt;&lt;span class='kwd'&gt;public class&lt;/span&gt; EditItemTemplate : ITemplate&lt;br /&gt;{&lt;br /&gt;    &lt;span class='kwd'&gt;public&lt;/span&gt; EditItemTemplate()&lt;br /&gt;    {&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span class='kwd'&gt;public void&lt;/span&gt; InstantiateIn(Control container)&lt;br /&gt;    {&lt;br /&gt;        TextBox txt = &lt;span class='kwd'&gt;new&lt;/span&gt; TextBox();&lt;br /&gt;        txt.ID = &lt;span class='st'&gt;"TextBoxID"&lt;/span&gt;;&lt;br /&gt;        txt.DataBinding += &lt;span class='kwd'&gt;new&lt;/span&gt; EventHandler(txt_DataBinding);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span class='kwd'&gt;void&lt;/span&gt; txt_DataBinding(&lt;span class='kwd'&gt;object&lt;/span&gt; sender, EventArgs e)&lt;br /&gt;    {&lt;br /&gt;        TextBox tb = sender &lt;span class='kwd'&gt;as&lt;/span&gt; TextBox;&lt;br /&gt;        GridViewRow gvr = sender &lt;span class='kwd'&gt;as&lt;/span&gt; GridViewRow;&lt;br /&gt;        tb.Text = DataBinder.Eval(gvr.DataItem, &lt;span class='st'&gt;"Name"&lt;/span&gt;).ToString();&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;So it’s pretty easy to create dynamically a GridView with TemplateFields. And that’s all we needed to build dynamically a GridView !&lt;br /&gt;&lt;br /&gt;&lt;em&gt;What's next ?&lt;/em&gt;&lt;br /&gt;In the next post, I will show you how we can handle the edit operation on such GridView and how to use it's ViewState.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7557263183336868289-2197448342643207017?l=yyosifov.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://yyosifov.blogspot.com/feeds/2197448342643207017/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7557263183336868289&amp;postID=2197448342643207017' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/2197448342643207017'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7557263183336868289/posts/default/2197448342643207017'/><link rel='alternate' type='text/html' href='http://yyosifov.blogspot.com/2008/07/just-test-using-system-namespace-test.html' title='How to build a Grid View dynamically ?'/><author><name>cypressx</name><uri>http://www.blogger.com/profile/03595535035189960906</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry></feed>
