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 ...

Get ASP.NET Site Performance Secrets now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.