.NET Tip of The Day
Learn one new .NET trick every day
Быстрое пополнение счета телефона      Login or Join

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

Comments:

Does this also perform better than:

Eval("field1")

Grant 2/13/2008 8:02:06 AM

Yes, it does. The Eval method calls the DataBinder.Eval method.

kostya.ly 2/13/2008 11:19:56 PM

Pity it doesn't just call ((DataRowView)Continer.DataItem).

As far as i'm concerned, i'm not giving you anymore information by going:

Eval("field1") v.s. ((DataRowView)Container.DataItem)["field1"]

So why should one take a performance knock?
Anyway, such is life

Grant Merwitz 2/14/2008 8:59:27 AM

Just to save some time... Typo...

"Continer" should be "Container"...
Which we all knew, but ject in case... ;)

Jeff Spicolie 2/14/2008 8:46:20 PM

lol, man I fought the urge to correct that one :)

Grant 2/15/2008 8:02:07 AM

And it is corrected now! :)
Jeff, thanks for pointing this out.

kostya.ly 2/15/2008 5:27:17 PM

Great tip.

To avoid databinder.eval which has the Reflection overhead to type the data, we could strongly type the Container.DataItem in VB.NET as follows

<%# (CType(Container.DataItem, System.Data.DataRowView)("fieldname")) %>

In case of date formats,

<%# (CType(Container.DataItem, System.Data.DataRowView)("datecolumn")).ToShortDateString() %>

The above code is tested in ASP.NET 1.1.

Also came across this article,
http://weblogs.asp.net/rajbk/archive/2004/07/20/what-s-the-deal-with-databinder-eval-and-container-dataitem.aspx?CommentPosted=true#commentmessage

http://weblogs.asp.net/jgalloway/archive/2005/09/20/425687.aspx

Srikanth 2/20/2008 9:45:37 PM

Aren't you now coupling the data layer to the view layer, by requiring the developer to know in advance what type to cast to ? If there are changes to the back-end, you now have to go back and re-factor the view?

Gary 8/27/2008 9:14:45 PM

2 Gary: IMHO it is ok that presentation layer is aware of which type data that comes from data layer is. Very ofthen you need this to format the data appropriately.

kostya.ly 8/28/2008 7:25:39 PM

It appears that in ASP.NET 3.5, your able to use the Container.DataItem with inferred type.

<asp:Button runat="server" CommandArgument='<% Container.DataItem.AgentID%>' CommandName="View" Text="View" CssClass="Button" />

I even get Intellisense for the available members in Container.Dataitem. I'm using a LINQDataSource as the containing GridView's datasource.

Patrick 3/5/2009 2:56:33 PM

Name
URL
E-mail
Provide your e-mail address to receive notification about new comments.
Message
HTML tags are not supported.
Please add 6 and 8 and type the answer here:
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.