ASP.NET 2.0 Performance Inspection Questions - Data Binding

From Guidance Share
Revision as of 06:23, 21 December 2007 by JD (talk | contribs)
(diff) ←Older revision | Current revision (diff) | Newer revision→ (diff)
Jump to navigationJump to 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.


<%# DataBinder.Eval(Container.DataItem,"field1") %> <%# DataBinder.Eval(Container.DataItem,"field2") %> </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> <%# ((DataRowView)Container.DataItem)["field1"] %> <%# ((DataRowView)Container.DataItem)["field2"] %> </ItemTemplate> Cast the Container.DataItem as a String if the data source is an Array or an ArrayList. <ItemTemplate> <%# ((String)Container.DataItem)["field1"] %> <%# ((String)Container.DataItem)["field2"] %> </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