数据格式化Eval(" ")和DataBinder.Eval(Container.DataItem," ")的区别及用法
ASP.NET 2.0改善了末班中的数据绑定操作,把V1.X中的数据绑定语法DataBinder.Eval(Container.DataItem,fieldname)简化为Eval(filedname)。Eval方法与DataBinder.Eval一样可以接受一个可选的格式化字符串参数。缩短的Eval语法与DataBinder.Eval的不同点在于,Eval会根据最近的容器对象(例如DataListItem)的DataItem属性来自动地解析字段,而DataBinder.Eval需要使用参数来指定容器。由于这个原因,Eval只能在数据绑定控件的模板中使用,而不能用于Page(页面)层。当然,ASP.NET 2.0页面中仍然支持DataBinder.Eval,你可以再不支持简化的Eval语法环境中使用它。
ASP.NET中DataBinder.Eval用法的总结:
(1).<%#Bind("Subject")%> //绑定字段
(2).<%#Container.DataItemIndex+1%> //实现自动编号
(3).<%#DataBinder.Eval(Container.DataItem,"[n]")%> //通常使用的方法
通常使用的方法(这三个性能最好)
(1).<%#DataBinder.Eval(Container.DataItem,"ColumnName")%>
(2).<%#DataBinder.Eval(Container.DataItem,"ColumnName",null)%>
(3).<%#DataBinder.Eval(Container,"DataItem.ColumnName",nul)%> //注意第二条和第三条的区别
其他用法
(1).<%#((DataRowView)Container.DataItem)[''ColumnName"]%>
(2).<%#((DataRowView)Container.DataItem).Row["ColumnName"]%>
(3).<%#((DataRowView)Container.DataItem)["adtitle"]%>
(4).<%#((DataRowView)Container.DataItem)[n]%>
(5).<%#((DbDataRecord)Container.DataItem)[0]%>
(6).<%#(((自定义类型)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
需要实践一下。
格式化数据和DataBinder.Eval用法范例
DataBinder.Eval 它带有三个参数:数据项的命名容器、数据字段名称和格式化字符串。 在模板列表如DataList、DataGrid、或 Repeater,命名容器总是Container.DataItem。 Page 是另一个可以被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>
转换类型
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
-------------------------------------------------
一、DataBinder.Eval的基本格式 在绑定数据时经常会用到这个句程序:或者 今天又学到一种,而且微软也说这种方法的效率要比以上两种高。 很有用的,这样可以在前台页面做好多事情了。 还要记住要这样用必须要在前台页面导入名称空间System.Data,否则会生成错误信息。 这种用法其实和是一个道理。 Text='' 这样的方法是最快的 Text='' 也可以绑定方法,但方法要是public的 Text='' 还可以连接多个字段 关键是Container这个东西,它比较神秘。它的名称空间是System.ComponentModel。对于它我还需要进一步理解。 二、DataBinder.Eval实现判断选择 cs里定义DGFormatSex方法 protected string DGFormatSex(string xb) { if(xb == "1") return "男"; else return "女"; } DataBinder.Eval用法范例 DataBinder.Eval用法范例 //显示二位小数 // //{0:G}代表显示True或False // // // //转换类型 ((string)DataBinder.Eval(Container, "DataItem.P_SHIP_TIME_SBM8")).Substring(4,4) {0:d} 日期只显示年月日 {0:yyyy-mm-dd} 按格式显示年月日 {0:c} 货币样式
浙公网安备 33010602011771号