Databinder.eval Container.Dataitem

The databinding expression is evaluated in the language of the page (VB, C#, etc.)  This can have a big impact on the current syntax, so be very careful when you are looking at docs for the language you are using.


Container.DataItem is a runtime alias for the DataItem for this specific item in the bound list.  For a grid which displays 10 rows of data, this is one row from the datasource.  The actual type of DataItem is determined by the type of the datasource.  For example, if the datasource is a Dataview, the type of DataItem is DataRowView.  If the type of the datasource is an array of strings, the type of DataItem is String.  If the datasource is a collection of strongly-typed objects (for example "Employees" objects), the type of DataItem is Employees.


Each of these cases requires a slightly different databinding expression, with further differences between VB and C#.  In every case, you want the databinding expression to produce a string that can be displayed in the page.


Here are some examples:


Array of Strings:



Field from DataView:




Property from a collection of objects:




Non-String property from a collection of objects:





As you can see the syntax is tricky, especially for C#, which requires explicit casting. So we've provided a DataBinder.Eval() helper method that figures out the syntax for you and formats the result as a string. It's really convenient, with a couple of caveats: it's late bound (uses reflection at runtime to figure out the data types), and it only supports basic data types in the fields: string, int, datetime, etc.

You can use Eval instead of DataBinder.Eval in 2.0


DataBinder.Eval takes 2 or 3 arguments.  The first arg is the data object to bind to.  In the case of DataGrid, DataList and Repeater, Container.DataItem is the single row.  The second arg the string name of the field from the data object you which to display.  DataBinder.Eval uses these two pieces of information to work out the rest of the expression syntax.


An optional third argument is a .NET formatting string.  DataBinder.Eval will handle a single replacement for {0} only.  So the example below:


Gebruik van eval in combinatie met methods die een parameter meekrijgen is als volgt:

'<%# myHelper(DataBinder.Eval(Container, "DataItem.MyDateField"))%>'


Wil je gewoon de data weergeven als je de column name niet weet, doe je als volgt:

<%# container.dataitem %>




Saving your comment....

Naam is verplicht!
Email is verplicht!
Opmerking is verplicht!