Introduction
The DataBinder.Eval
method is used to bind data to a control's template. DataBinder.Eval
casts Container.DataItem
to its specific type, like this:
<ItemTemplate>
<div>
<%# DataBinder.Eval(Container.DataItem,"UserName") %>
</div>
</ItemTemplate>
DataBinder.Eval
uses .NET reflection to cast Container.DataItem
to its specific type which results in a performance loss. So it is better to use explicit casting.
Option 1 - Cast the Container.DataItem as a DataRowView if the data source is a DataSet.
<ItemTemplate>
<div><%# ((DataRowView)Container.DataItem)["UserName"] %>
</div>
</ItemTemplate>
Option 2 for DataReader
<ItemTemplate>
<div>
<%# ((DbDataRecord)Container.DataItem)["UserName"] %>
</div>
</ItemTemplate>
Cast the Container.DataItem as a String if the data source is an Array or an ArrayList.
<ItemTemplate>
<div>
<%# ((String)Container.DataItem)["UserName"] %>
</div>
</ItemTemplate>