Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Databinding performance tip in ASP.NET

    In my last post I mentioned the use of C style casting when databinding in ASP.NET.  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). 

    In my example I'm using a DataList and by default, Visual Studio will bind the data to your template controls using the "Eval" method.

<asp:Label ID="FirstNameLabel" runat="server" Text='<%# Eval("FirstName") %>'></asp:Label>

    This method uses reflection and can be considerably slower depending on the complexity of your template and the number of properties you are binding.  The attached code uses a very simple DataList with simple data.  To see the difference in performance you'll need to increase the size of the data using the drop down list at the top.

    Compare the performance using the "Eval" method to using the C style cast to access your object's properties.

<asp:Label ID="FirstNameLabel" runat="server" Text='<%# ((Person)DataBinder.GetDataItem(Container)).FirstName %>'></asp:Label>

EDIT:  As Bill pointed out in the comments, the call to DataBinder.GetDataItem() isn't necessary as you have access through the container already.  I've updated the attached test code to include all three methods.  These last two being equal in performance in my testing.


<asp:Label ID="FirstNameLabel" runat="server" Text='<%# ((Person)Container.DataItem).FirstName %>'></asp:Label>

    In my testing this method (the last two methods, actually) was (were) 3 - 4 times faster.  As I said, this depends on the complexity of your control. 


Posted 10-08-2007 10:14 AM by anortham
Filed under: , ,
Attachment: DataBinding.zip

[Advertisement]

Comments

Bill wrote re: Databinding performance tip in ASP.NET
on 10-08-2007 5:56 PM

That is still unnecessarily complex. DataBinder.GetDataItem will do a check to see if the item implements IDataItemContainer and then return the DataItem and if not then use reflection to try to call a DataItem property anyways.

I personally like to use:

((Person)Container.DataItem).FirstName

or

((Container.DataItem as Person) ?? (Person)new DefaultPerson()).FirstName

where DefaultPerson inherits from Person and provides a suitable default implementation.

anortham wrote re: Databinding performance tip in ASP.NET
on 10-08-2007 8:41 PM

Bill,

Thanks for the tip!

t0p wrote re: Databinding performance tip in ASP.NET
on 02-07-2008 5:27 PM

Hi Alan,

I have been playing with nHibernate myself for some time now..

I browsed through your code and I must say I like it :)

Thanks for your effort and ideas as I am sure it will be a great help for many!

t0p wrote re: Databinding performance tip in ASP.NET
on 02-08-2008 6:39 AM

Oops my last comment should go to the post regarding nHibernate devlicio.us/.../custom-profile-provider-with-nhibernate.aspx

Sorry.

databinding in asp net using c wrote databinding in asp net using c
on 06-25-2008 3:58 AM

Pingback from  databinding in asp net using c

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)