What's the deal with Databinder.Eval and Container.DataItem?

转载 2006年06月20日 17:37:00
The databinding expression <%# some 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:

VB/C# <%# Container.DataItem %>


Field from DataView:

VB      <%# Container.DataItem("EmployeeName") %>

C#      <%# ((DataRowView)Container.DataItem)["EmployeeName"] %>


Property from a collection of objects:

VB      <%# Container.DataItem.CustomerName %>

C#      <%# ((Customer)Container.DataItem).CustomerName %>


Non-String property from a collection of objects:

VB      <%# CStr(Container.DataItem.CustomerID) %>

C#      <%# ((Customer)Container.DataItem).CustomerID.ToString() %>



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.



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:


<a href='<%# "default.aspx?CategoryId=" + Cstr(Databinder.Eval(Container.DataItem, "ID"))%>'>

could be simplified to:

<a href='<%#  Databinder.Eval(Container.DataItem,"ID","default.aspx?CategoryId={0}" ) %>'>


Wrapping DataBinder.Eval in CStr() is unnecessary as the compiler will wrap the statement with a Convert.ToString like this:

control1.SetDataBoundString(0, Convert.ToString(DataBinder.Eval(item1.DataItem, "ID")));


Best of all, the Databinder.Eval syntax is the same for VB and C#.

Susan Warren
11/30/2000 1:48:00 PM

Scott Galloway
Worth mentioning the performance cost of this, in my own tests I've found around 20% performance loss in using DataBinder.Eval versus explicit casting. This can make a difference when binding large sets. My original post on this is here: http://www.mostlylucid.co.uk/archive/2003/12/09/664.aspx


1.DataBinder.Eval用法 //显示二位小数 //{0:G}代表显示True或False
  • shuo785869451
  • shuo785869451
  • 2013年11月29日 17:28
  • 745

与'>以及DataBinder.Eval(Container, DataItem,"id")的区别

直接用Eval()方式是利用了反射机制,而在ASP.NET 1.0中,只能使用DataBinder.Eval()的方式。 是只读的方式访问数据 是以读写的方式 DataBinder.Eval(Cont...
  • nanyida0416sushe
  • nanyida0416sushe
  • 2010年05月17日 11:38
  • 2655


DataGrid控件,在ItemTemplate显示数据时, DataBinder.eval_r(Container.DataItem,"Name")和Container.DataItem("Nam...
  • woshixuye
  • woshixuye
  • 2012年01月31日 19:31
  • 1117


  • steven_chr
  • steven_chr
  • 2006年06月20日 17:13
  • 1451

ASP.NET-关于Container dataitem 与 eval方法介绍

Container是一个数据容器,代表集合类或者dataview中的一行,而Container。dataitem代表该行的数据;所有的container   被存 放在是一个栈堆stack中,自...
  • qiushengb
  • qiushengb
  • 2014年05月18日 18:49
  • 2081

<%# DataBinder.eval_r(Container.DataItem,"shipname")

  • zhangqiang0921
  • zhangqiang0921
  • 2010年10月22日 16:54
  • 1539


灵活的运用数据绑定操作        绑定到简单属性:        绑定到集合: runat="server">        绑定到表达式:        绑定到方法返回值:        绑定到...
  • jelink
  • jelink
  • 2006年08月25日 18:05
  • 14455


  • wengsunjian
  • wengsunjian
  • 2007年01月06日 11:27
  • 732

What’s The Deal With The Git Index?

一篇讲解Git Index究竟是什么,有什么作用的文章。 原文链接:http://www.gitguys.com/topics/whats-the-deal-with-the-git-index/ ...
  • yanxiangtianji
  • yanxiangtianji
  • 2015年11月10日 11:08
  • 433

$ What's the deal with the dollar signs?

* "${xxx}" is typically an expression in the syntax/semantics supported    by the JSP Standard Tag ...
  • lord_is_layuping
  • lord_is_layuping
  • 2012年01月30日 15:09
  • 463
您举报文章:What's the deal with Databinder.Eval and Container.DataItem?