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 %> <%# ((MyClass)Container.DataItem).field4 %> </ItemTemplate> </asp:Repeater>
In this example, I have used class MyClass
. If you're binding against a...