您的位置:首页 > 移动开发 > Objective-C

DataBinder.Eval 方法 (Object, String, String)用法

2008-01-08 21:57 477 查看
DataBinder.Eval 它带有三个参数:数据项的命名容器、数据字段名称和格式化字符串。

在模板列表如DataList、DataGrid、或 Repeater,命名容器总是Container.DataItem。

(Msdn)

DataBinder.Eval 方法 (Object, String, String)

在运行时计算数据绑定表达式,并将结果格式化为要在请求浏览器中显示的文本。

命名空间:System.Web.UI

参数

container

表达式根据其进行计算的对象引用。此标识符必须是以页的指定语言表示的有效对象标识符。

expression
从 container 到要放置在绑定控件属性中的公共属性值的导航路径。此路径必须是以点分隔的属性或字段名称字符串,如 C# 中的 "Tables[0].DefaultView.[0].Price" 或 Visual Basic 中的 "Tables(0).DefaultView.(0).Price"。

format
.NET Framework 格式字符串,类似于String.Format所用的字符串,可以将Object(是数据绑定表达式的计算结果)转换为可由请求浏览器显示的String。

返回值

String,它是数据绑定表达式的计算和向字符串类型转换的结果。

格式化字符串参数是可选的。如果忽略参数,DataBinder.Eval 返回对象类型的值

//显示二位小数

//<%# DataBinder.Eval(Container.DataItem, "UnitPrice", "${0:F2}") %>

注:

  DataBinder是System.Web里面的一个静态类,它提供了Eval方法用于简化数据绑定表达式的编写,但是它使用的方式是通过 Reflection等开销比较大的方法来达到易用性,因此其性能并不是最好的。

而Container则根本不是任何一个静态的对象或方法,它是 ASP.NET页面编译器在数据绑定事件处理程序内部声明的局部变量,其类型是可以进行数据绑定的控件的数据容器类型(如在Repeater内部的数据绑定容器叫RepeaterItem),在这些容器类中基本都有DataItem属性,因此你可以写Container.DataItem,这个属性返回的是你正在被绑定的数据源中的那个数据项。如果你的数据源是DataTable,则这个数据项的类型实际是DataRowView。

//{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>

转换类型

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

轉:http://blog.csdn.net/guye99/archive/2006/04/30/698323.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: