[翻译][.NET Tip of The Day]使用显式转换替代DataBinder.Eval
英文原文: http://dotnettipoftheday.org/tips/use-explicit-casting-instead-of-databinder.eval.aspx
使用显式转换替代DataBinder.Eval(Use explicit casting instead of DataBinder.Eval)
DataBinder.Eval 方法使用 .NET 反射(reflection) to evaluate the arguments that are passed in and to return the results. 考虑在数据绑定操作中尽量少地使用DataBinder.Eval, 以便提升 ASP.NET 页面性能.
考虑下面 Repeater 控件中 ItemTemplate 元素(element) 使用 DataBinder.Eval:
<ItemTemplate>
<tr>
<td><%# DataBinder.Eval(Container.DataItem, "field1") %></td>
<td><%# DataBinder.Eval(Container.DataItem, "field2") %></td>
</tr>
</ItemTemplate>
通过显式转换(explicit casting)来避免 .NET 反射的开销能得到更好的性能. 转换Container.DataItem 为 DataRowView:
<ItemTemplate>
<tr>
<td><%# ((DataRowView)Container.DataItem)["field1"] %></td>
<td><%# ((DataRowView)Container.DataItem)["field2"] %></td>
</tr>
</ItemTemplate>
2/12/2008
浙公网安备 33010602011771号