Rakesh Rajan's blog

Thoughts on .NET, software and a few trivial things...

Data binding syntaxes

Background
DataBinder.Eval and the standard explicit casting of Container.DataItem are the two syntaxes you could use while performing data binding.  Some developers are stringent on performance (they use only the standard syntax), some are concerned about maintenance (nothing but DataBinder.Eval), while some are least bothered about the syntax they use.  I have seen code filled with DataBinder.Eval where the standard Container.DataItem syntax would have been a perfect fit.  So I thought I would write a little bit on how to select the appropriate data binding syntax.


There are two data binding syntaxes you could employ when you use repeated value data binding:

  • the standard syntax with explicit casting of Container.DataItem
  • the DataBinder.Eval syntax

The following two expressions perform the same binding (they both bind to the 'City' column of the current data item), but uses the different syntaxes mentioned above.

  1. <%# ((DataRowView)Container.DataItem)["City"] %>
  2. <%# DataBinder.Eval(Container.DataItem, "City") %>

The problem with the first syntax (the standard data binding syntax) is that if the data source changes, we need to change the cast too.  Say, if the data source in the above example changes to a data reader, then we need to change the syntax to:

<%# ((DbDataRecord)Container.DataItem)["City"] %>

In other words, the standard data binding syntax is 'strongly typed'.  


DataBinder.Eval was provided as a helper method to simplify the standard data binding syntax.  Instead of you specifying the syntax (with the data item type etc.), DataBinder.Eval figures out the syntax on its own and in addition, formats the result and returns a string.

The bad thing about DataBinder.Eval is that since it uses late-bound reflection to figure out the data types of the source specified, it comes with a performance hit.

So how do we decide which data binding syntax to use?

Use DataBinder.Eval if...

  • You are into control development.  When you develop controls, you are not sure what kind of data source types your control is going to be bound to.  Moreover, you also wouldn't like to force your clients to use a specific data source type.
  • You don't mind a performance hit.
  • You are too lazy to go to every page and change the standard syntax whenever the data source type changes :)

Use the standard syntax if...

  • You want to squeeze every bit of performance out of your pages.
  • You know what the data source type of a control is, and you are also sure that the data source type won't change
  • You don't mind doing a CStr or a ToString wherever required

Both approaches have their own good and bad sides.  I hope the points above would help you decide on which one to select.

Posted: Apr 10 2005, 04:05 PM by rakeshrajan | with 5 comment(s)
Filed under:

Comments

rakeshrajan said:

Good write up!
# April 17, 2005 10:38 PM

rakeshrajan said:

Good article thanks!
# May 7, 2005 4:04 AM

TrackBack said:

Data binding syntaxesooeess
# July 22, 2005 3:44 PM

rakeshrajan said:

Hey, nice article. I was needing some one to clarify this for me. Thanks.
# August 25, 2005 12:20 AM

rakeshrajan said:

Hi Edwin,

Glad it helped!

Thanks,
Rakesh Rajan
# August 26, 2005 8:34 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)