<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://devlicio.us/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Alan Northam : Databinding</title><link>http://devlicio.us/blogs/alan_northam/archive/tags/Databinding/default.aspx</link><description>Tags: Databinding</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>Databinding performance tip in ASP.NET</title><link>http://devlicio.us/blogs/alan_northam/archive/2007/10/08/databinding-performance-tip-in-asp-net.aspx</link><pubDate>Mon, 08 Oct 2007 15:14:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:38637</guid><dc:creator>anortham</dc:creator><slash:comments>5</slash:comments><description>&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; In my &lt;a href="http://devlicio.us/blogs/alan_northam/archive/2007/10/07/casting-options-in-c.aspx" target="_blank"&gt;last post&lt;/a&gt; I mentioned the use of C style casting when databinding in ASP.NET.&amp;nbsp;&amp;nbsp;That got me thinking about the performance differences between the two main ways to bind data in controls with custom templates (GridView, DataList, Repeater, etc).&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; In my example I&amp;#39;m using a DataList and by default, Visual Studio will bind the data to your template controls using the &amp;quot;Eval&amp;quot; method.&lt;/p&gt;
&lt;p&gt;&amp;lt;asp:Label ID=&amp;quot;FirstNameLabel&amp;quot; runat=&amp;quot;server&amp;quot; Text=&amp;#39;&amp;lt;%# Eval(&amp;quot;FirstName&amp;quot;) %&amp;gt;&amp;#39;&amp;gt;&amp;lt;/asp:Label&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; This method uses reflection and can be considerably slower depending on the complexity of your template and the number of properties you are binding.&amp;nbsp; The attached code uses a very simple DataList with simple data.&amp;nbsp; To see the difference in performance you&amp;#39;ll need to increase the size of the data using the&amp;nbsp;drop down list&amp;nbsp;at the top.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Compare the performance using&amp;nbsp;the &amp;quot;Eval&amp;quot;&amp;nbsp;method to using the C style cast to access your object&amp;#39;s properties.&lt;/p&gt;
&lt;p&gt;&amp;lt;asp:Label ID=&amp;quot;FirstNameLabel&amp;quot; runat=&amp;quot;server&amp;quot; Text=&amp;#39;&amp;lt;%# ((Person)DataBinder.GetDataItem(Container)).FirstName %&amp;gt;&amp;#39;&amp;gt;&amp;lt;/asp:Label&amp;gt; &lt;/p&gt;
&lt;p&gt;EDIT:&amp;nbsp; As Bill pointed out in the comments, the call to DataBinder.GetDataItem() isn&amp;#39;t necessary as you have access through the container already.&amp;nbsp; I&amp;#39;ve updated the attached test code to include all three methods.&amp;nbsp; These last two being equal in performance in my testing.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&amp;lt;asp:Label ID=&amp;quot;FirstNameLabel&amp;quot; runat=&amp;quot;server&amp;quot; Text=&amp;#39;&amp;lt;%# ((Person)Container.DataItem).FirstName %&amp;gt;&amp;#39;&amp;gt;&amp;lt;/asp:Label&amp;gt; &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; In my testing this method (the last two methods, actually)&amp;nbsp;was (were)&amp;nbsp;3 - 4 times faster.&amp;nbsp; As I said, this depends on the complexity of your control.&amp;nbsp; &lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=38637" width="1" height="1"&gt;</description><enclosure url="http://devlicio.us" length="35842" type="application/x-zip-compressed" /><category domain="http://devlicio.us/blogs/alan_northam/archive/tags/Casting/default.aspx">Casting</category><category domain="http://devlicio.us/blogs/alan_northam/archive/tags/Databinding/default.aspx">Databinding</category><category domain="http://devlicio.us/blogs/alan_northam/archive/tags/ASP.NET/default.aspx">ASP.NET</category></item></channel></rss>