SYSK 117: Are You Using Eval or Bind for your ASPX Data Binding?

Many ASP.NET 1.0 and 1.1 developers have been using DataBinder.Eval method to evaluate binding expressions at run time (late-bound data) and optionally format the result as a string.  While, personally, I prefer early binding due to performance benefits, some developers use DataBinder.Eval to avoid casting.  For example, in a templated list like DataList, DataGrid, or Repeater control the code might look like this:


Early binding: 

<%# string.Format("{0:c}", ((DataRowView) Container.DataItem["SomeValue"])); %>


Late binding:

<%# DataBinder.Eval(Container.DataItem, "SomeValue", "{0:c}"); %>



If you’re using DataBinder.Eval, you may be interested in the new feature in ASP.NET 2.0 – the Bind method.  Eval method is one directional; I think of it as read-only.  The Bind method is bi-directional, and it should be used when data can be modified via UI.


In ASP.NET, data-bound controls such as the GridView, DetailsView, and FormView controls can automatically use the update, delete, and insert operations of a data source control. For example, if you have defined SQL Select, Insert, Delete, and Update statements for your data source control, using Bind in a GridView, DetailsView, or FormView control template enables the control to extract values from child controls in the template and pass them to the data source control. The data source control in turn performs the appropriate command for the database. For this reason, the Bind function is used inside the EditItemTemplate or InsertItemTemplate of a data-bound control.




Comments (2)

  1. Ron says:

    I think you have your early binding and late binding reversed.

  2. irenake says:

    I sure do (corrected now).  Thanks for pointing this out!

Skip to main content