ASP.NET 2.0改善了模板中的数据绑定操作把v1.x中的数据绑定语法DataBinder.Eval(Container.DataItem, fieldname)简化为Eval(fieldname)。

Eval方法与DataBinder.Eval一样可以接受一个可选的格式化字符串参数。

缩短的Eval语法与DataBinder.Eval的不同点在于Eval会根据最近的容器对象

例如

DataListItem 的DataItem属性来自动地解析字段。

而DataBinder.Eval需要使用参数来指定容器。

由于这个原因Eval只能在数据绑定控件的模板中使用,而不能用于 Page页面层。当然ASP.NET 2.0页面中仍然支持DataBinder.Eval你可以在不支持简化的Eval语法的环境中使用它。

Asp.net中DataBinder.Eval用法的总结<%# Bind("Subject") %> //绑定字段

<%# Container.DataItemIndex + 1%> //实现自动编号

<%# ataBinder.Eval(Container.DataItem, "[n]") %> 通常使用的方法(这三个性能最好)

<%# DataBinder.Eval(Container.DataItem, "ColumnName") %>

<%# DataBinder.Eval(Container.DataItem, "ColumnName", null) %>

<%# DataBinder.Eval(Container, "DataItem.ColumnName", null) %>
其他用法
<%# ((DataRowView)Container.DataItem)["ColumnName"] %>
<%# ((DataRowView)Container.DataItem).Row["ColumnName"] %>
<%# ((DataRowView)Container.DataItem)["adtitle"] %>
<%# ((DataRowView)Container.DataItem)[n] %>
<%# ((DbDataRecord)Container.DataItem)[0] %>
<%# (((自定义类型)Container.DataItem)).属性.ToString() %>//如果属性为字符串类型就不用ToString()了 DataBinder.Eval用法范例

<%# DataBinder.Eval(Container.DataItem, "IntegerValue", "{0:c}") %>
格式化字符串参数是可选的。如果忽略参数DataBinder.Eval 返回对象类型的值

//显示二位小数
<%# DataBinder.Eval(Container.DataItem, "UnitPrice", "${0:F2}") %>

//{0:G}代表显示True或False
<ItemTemplate>
<asp:Image Width="12" Height="12" Border="0" runat="server"
AlternateText='<%# DataBinder.Eval(Container.DataItem, "Discontinued",
"{0:G}") %>'
ImageUrl='<%# DataBinder.Eval(Container.DataItem, "Discontinued",
"~/images/{0:G}.gif") %>' />
</ItemTemplate>

//转换类型
((string)DataBinder.Eval(Container,

"DataItem.P_SHIP_TIME_SBM8")).Substring(4,4)
{0:d} 日期只显示年月日
{0:yyyy-mm-dd} 按格式显示年月日
{0:c} 货币样式
<%#Container.DataItem("price","{0:#,##0.00}")%>
<%#
DataBinder.Eval(Container.DataItem,"Company_Ureg_Date","{0:yyyy-M-d}"
)%>
Specifier Type Format Output (Passed Double 1.42) Output
(Passed Int -12400)
c Currency {0:c} $1.42 -$12,400
d Decimal {0:d} System.FormatException -12400
e Scientific {0:e} 1.420000e+000 -1.240000e+004
f Fixed point {0:f} 1.42 -12400.00
g General {0:g} 1.42 -12400
n Number with commas for thousands {0:n} 1.42 -12,400
r Round trippable {0:r} 1.42 System.FormatException
x Hexadecimal {0:x4} System.FormatException cf90

{0:d} 日期只显示年月日
{0:yyyy-mm-dd} 按格式显示年月日

样式取决于 Web.config 中的设置
{0:c} 或 {0:0,000.00} 货币样式 标准英国货币样式
<system.web>
<globalization requestEncoding="utf-8" responseEncoding="utf-8"

culture="en-US" uiCulture="en-US" />
</system.web>
显示为 3,000.10

{0:c} 或 string.Format("{0:C}", price); 中国货币样式
<system.web>
<globalization requestEncoding="utf-8" responseEncoding="utf-8"
culture="zh-cn" uiCulture="zh-cn" />
</system.web>
显示为 3,000.10

{0:c} 或 string.Format("{0:C}", price); 美国货币样式
<system.web>
<globalization requestEncoding="utf-8" responseEncoding="utf-8" />
</system.web>
显示为 $3,000.10

Eval()和DataBinder Eval(Container DataItem,)的区别及用法的更多相关文章

  1. Eval与DataBinder.Eval的区别

    DataBinder.Eval的基本格式 DataBinder.Eval(Container.DataItem,"XXX","{0}") <%# Data ...

  2. ASP.NET中的Eval与DataBinder.Eval()方法

    1.bind是一种双向数据绑定,有数据源时才会有改变. 2..net1.x版本中有DataBinder(Container.DataItem,"数据项")  单向数据绑定 .net ...

  3. DataBinder.Eval的正确使用

    本文介绍下,asp.net编程中有关DataBinder.Eval的用法,学习下asp.net DataBinder.Eval的用法,有需要的朋友参考下. 代码示例 :<%# Bind(&quo ...

  4. (转)ASP.NET-关于Container dataitem 与 eval方法介绍

    Container是一个数据容器,代表集合类或者dataview中的一行,而Container.dataitem代表该行的数据:所有的container   被存 放在是一个栈堆stack中,自动的将 ...

  5. Asp服务器控件(HyperLink、Button) 绑定后台参数 DataBinder.Eval

    HyperLink动态绑定参数 <asp:HyperLink id="MbCenterHLnk" runat="server" Text='会员中心' T ...

  6. DataBinder.Eval值的判断

    原文发布时间为:2009-04-10 -- 来源于本人的百度文章 [由搬家工具导入] 问:如何对<%# DataBinder.Eval(Container.DataItem,"Ly_R ...

  7. 数据绑定以及Container.DataItem几种方式与用法分析

    灵活的运用数据绑定操作        绑定到简单属性:<%#UserName%>        绑定到集合:<asp:ListBox id="ListBox1" ...

  8. [ASP.NET] 数据绑定以及Container.DataItem的具体分析 [转]

    灵活的运用数据绑定操作        绑定到简单属性:<%#UserName%>        绑定到集合:<asp:ListBox id="ListBox1" ...

  9. 数据绑定以及Container.DataItem几种方式与使用方法分析

    灵活的运用数据绑定操作        绑定到简单属性:<%#UserName%>        绑定到集合:<asp:ListBox id="ListBox1" ...

随机推荐

  1. ☀【JS】Code

    1 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf- ...

  2. 使用 ASR 和 Azure Pack 为 IaaS 工作负荷提供托受管 DR

    Ashish Gangwar 云 + Enterprise项目经理 几周前,我们宣布了在 Azure Site Recovery 中提供一些新功能,这些新功能适用于不同场景,可以让服务提供商在 A ...

  3. 从零开始学习jQuery (六) AJAX快餐

    一.摘要 本系列文章将带您进入jQuery的精彩世界, 其中有很多作者具体的使用经验和解决方案,  即使你会使用jQuery也能在阅读中发现些许秘籍. 本篇文章讲解如何使用jQuery方便快捷的实现A ...

  4. 跟我学机器视觉-HALCON学习例程中文详解-开关引脚测量

    跟我学机器视觉-HALCON学习例程中文详解-开关引脚测量 This example program demonstrates the basic usage of a measure object. ...

  5. 【JS】Intermediate2:Events and Callbacks

    event-driven :waiting for and reacting to events 2.page loads, user interacts (clicks, hovers, chang ...

  6. fcntl记录锁实例

    fcntl 函数是一个相当常用的对文件进行加锁操作的函数. 文件锁包括强制锁.建议锁.记录锁, 不过一般系统和内核都是用的强制锁. 以下为记录锁的例子:------------------------ ...

  7. vue相关

    勾三股四的vue+webpack实战:http://jiongks.name/blog/just... 用Vue构建一个Notes App:https://coligo.io/learn-vuex-. ...

  8. 如何给Windows添加自动启动的程序

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:如何给Windows添加自动启动的程序.

  9. 跟我学SpringMVC目录汇总贴、PDF下载、源码下载

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  10. jQuery Mobile方向感应事件

    在现在的智能手机中,都有对方向变换的自动感知功能,比如当手机方向从水平方向切换到垂直方向时,则会触发该事件.在jQuery Mobile中,可以通过orientationchange事件进行绑定,并且 ...