关闭

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

885人阅读 评论(0) 收藏 举报
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

Comments:
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

0
0

猜你在找
【套餐】Hadoop生态系统零基础入门
【套餐】嵌入式Linux C编程基础
【套餐】2017软考系统集成项目——任铄
【套餐】Android 5.x顶级视频课程——李宁
【套餐】深度学习入门视频课程——唐宇迪
【直播】广义线性模型及其应用——李科
【直播】从0到1 区块链的概念到实践
【直播】计算机视觉原理及实战——屈教授
【直播】机器学习之凸优化——马博士
【直播】机器学习&数据挖掘7周实训--韦玮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:47966次
    • 积分:599
    • 等级:
    • 排名:千里之外
    • 原创:11篇
    • 转载:21篇
    • 译文:0篇
    • 评论:3条
    最新评论
    Valuable Website