ASP.NET 2.0 Performance Inspection Questions - Data Binding

From Guidance Share

Jump to: navigation, search

- J.D. Meier, Srinath Vasireddy, Ashish Babbar, Rico Mariani, and Alex Mackman


Use the following review questions to review your code's use of data binding:


Do you use Page.DataBind?

Avoid calling Page.DataBind and bind each control individually to optimize your data binding. Calling Page.DataBind recursively calls DataBind on each control on the page.


Do you use DataBinder.Eval?

DataBinder.Eval uses reflection, which affects performance. In most cases DataBinder.Eval is called many times from within a page, so implementing alternative methods provides a good opportunity to improve performance.

Avoid the following approach.


  <ItemTemplate>
    <tr>
      <td><%# DataBinder.Eval(Container.DataItem,"field1") %></td>
      <td><%# DataBinder.Eval(Container.DataItem,"field2") %></td>
    </tr>
  </ItemTemplate>


Use explicit casting. It offers better performance by avoiding the cost of reflection. Cast the Container.DataItem as a DataRowView if the data source is a DataSet.


  <ItemTemplate>
    <tr>
      <td><%# ((DataRowView)Container.DataItem)["field1"] %></td>
      <td><%# ((DataRowView)Container.DataItem)["field2"] %></td>
    </tr>
  </ItemTemplate>


Cast the Container.DataItem as a String if the data source is an Array or an ArrayList.


  <ItemTemplate>
    <tr>
      <td><%# ((String)Container.DataItem)["field1"] %></td>
      <td><%# ((String)Container.DataItem)["field2"] %></td>
    </tr>
  </ItemTemplate>


Related Items

For more information about the questions and issues raised in this section, see "Databinding" in ASP.NET 2.0 Performance Guidelines - Data Binding

Personal tools