Container.DataItem is an alias for a specific item in a bound list. The actual type of DataItem is determined by the type of the datasource. Since in this case the datasource is an array of strings, when Container.DataItem is evaluated at runtime, you get strings.
This works perfectly well, but using Container.DataItem is sometimes a pain because the expression syntax varies based on the type. For example, if you are binding to a collection of Customer objects instead of an array of strings, and you want to display the customer’s names in a repeater, you’d do this:
<%# ((Customer)Container.DataItem).CustomerName %>
In order to alleviate the confusing type-based syntax, the Databinder.Eval method was invented, so to bind to the Customer collection in the above example, you could do this instead:
<%# DataBinder.Eval(Container.DataItem, “CustomerName” %>
Databinder.Eval uses reflection to determine the type represented by Container.DataItem, and works out the syntax accordingly.
Subsequent revisions in the framework let you leave off the DataBinder part. If the data type to which you are binding is known, such as a row in a GridView Control, you can also omit the Container.DataItem call:
<%# Eval(“CustomerName”) %>
However, since in this case the repeater only binds to an array of strings, there is no benefit to using the Eval method here. By simply using Container.DataItem to bind to members of a string array, the syntax is simpler and you don’t take the hit from the use of reflection employed by Eval.
|