Priyang Patel’s Weblog

Use explicit casting instead of DataBinder.Eval

Posted in Visual Studio Tips and Tricks by priyangpatel on March 19, 2008

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:



<td><%# DataBinder.Eval(Container.DataItem, “field1”) %><!–td>

<td><%# DataBinder.Eval(Container.DataItem, “field2”) %><!–td>



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



<td><%# ((DataRowView)Container.DataItem)[“field1”] %><!–td>

<td><%# ((DataRowView)Container.DataItem)[“field2”] %><!–td>




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: