O'Reilly logo

ASP.NET Site Performance Secrets by Matt Perdeck

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

DataBinder.Eval

Controls such as GridView and Repeater work with templates. A popular way to refer to fields within templates uses DataBinder.Eval, as shown in the following code:

<asp:Repeater ID="rptrEval" runat="server">
<ItemTemplate>
<%# Eval("field1")%>
<%# Eval("field2")%>
<%# Eval("field3")%>
<%# Eval("field4")%>
</ItemTemplate>
</asp:Repeater>

However, this uses reflection to arrive at the value of the field. If you have 10 rows with four fields each, you execute Eval 40 times.

A much faster way is to refer directly to the class that contains the field:

<asp:Repeater ID="rptrCast" runat="server"> <ItemTemplate> <%# ((MyClass)Container.DataItem).field1 %> <%# ((MyClass)Container.DataItem).field2 %> <%# ((MyClass)Container.DataItem).field3 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required