The DataBinder.Eval method uses reflection to evaluate the
arguments that are passed in and to return the results. If you have a table
that has 100 rows and 10 columns, you call DataBinder.Eval 1,000 times if you
use DataBinder.Eval on each column. Your choice to use DataBinder.Eval is
multiplied 1,000 times in this scenario. Limiting the use of DataBinder.Eval
during data binding operations significantly improves page performance.
Consider the following ItemTemplate element within a Repeater control using
DataBinder.Eval.
Listing 11
<ItemTemplate>
<tr>
<td><%# DataBinder.Eval(Container.DataItem,"field1") %></td>
<td><%# DataBinder.Eval(Container.DataItem,"field2") %></td>
</tr>
</ItemTemplate>
There are alternatives to using DataBinder.Eval in this
scenario. The alternatives include the following:
Use explicit casting. Using explicit casting offers better
performance by avoiding the cost of reflection. Cast the Container.DataItem as
a DataRowView.
Listing 12
<ItemTemplate>
<tr>
<td><%# ((DataRowView)Container.DataItem)["field1"] %></td>
<td><%# ((DataRowView)Container.DataItem)["field2"] %></td>
</tr>
</ItemTemplate>
You can gain even better performance with explicit casting
if you use a DataReader to bind your control and use the specialized methods to
retrieve your data. Cast the Container.DataItem as a DbDataRecord.
Listing 13
<ItemTemplate>
<tr>
<td><%# ((DbDataRecord)Container.DataItem).GetString(0) %></td>
<td><%# ((DbDataRecord)Container.DataItem).GetInt(1) %></td>
</tr>
</ItemTemplate>