Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Alan Northam



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.

    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.

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.


    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. 


Published Oct 08 2007, 10:14 AM by anortham
Filed under: , ,
Attachment: DataBinding.zip

Comments

anortham said:

Bill,

Thanks for the tip!

# October 8, 2007 8:41 PM

t0p said:

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!

# February 7, 2008 5:27 PM

t0p said:

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

Sorry.

# February 8, 2008 6:39 AM

databinding in asp net using c said:

Pingback from  databinding in asp net using c

# June 25, 2008 3:58 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

Our Sponsors

Proudly Partnered With