.NET Tip of The Day
Learn one new .NET trick every day
Login or Join
.NET Tips & Tricks Community RSS

Those who prefer reading this site through RSS now can also subscribe for updates to .NET Tips & Tricks Community.

Use explicit casting instead of DataBinder.Eval

The DataBinder.Eval method uses .NET reflection to evaluate the arguments that are passed in and to return the results. Consider limiting the use of DataBinder.Eval during data binding operations in order to improve ASP.NET page performance.

Consider the following ItemTemplate element within a Repeater control using DataBinder.Eval:

   <ItemTemplate>

       <tr>

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

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

       </tr>

   </ItemTemplate>

Using explicit casting offers better performance by avoiding the cost of .NET reflection. Cast the Container.DataItem as a DataRowView:

   <ItemTemplate>

       <tr>

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

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

       </tr>

   </ItemTemplate>

 

2/12/2008
RSS .NET Tip of The Day
Subscribe to receive one tip from the .NET Tips and Tricks Community per day.
Previous Tips of The Day
The best of the .NET Tips & Tricks Community.
.NET Practitioners .NET Tips & Tricks Community
Every .NET practitioner has a trick up in their sleeve. This is the place to share it with other .NET people.
Submit a Tip
Discovered a new trick? Share it with others.
My Tips
Manage tips you authored.