DataBinder.Eval()格式化绑定的相关补充

数据绑定DataBinder.Eval方法
Text='<%# DataBinder.Eval(Container.DataItem, "字段") %>'

第一种用法也是最常用的用法,估计相关的资料都比较常见;一般在.aspx文件中使用 1<%# DataBinder.Eval(Container.DataItem,"yourColname")#>

第二种是直接在DataGird的ItemBound事件里使用DataBinder.Eval().这种用法十分灵活,如果你需要对目标数据进行一些加工处理,这种方法应该能够满足。当然,也可以使用第一种方法实现类似的功能,不过,相比较而言,还是会麻烦些。

以下是关键的例子代码: 1DataGird1_ItemBound(,)2{3 if(e.Item.ItemType==ListItemType.Item)4 {5 e.Item.Cells[1].Text=DataBinder.Eval(e.Item.DataItem,"yourColname);6 }7
}


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

posted @ 2010-11-03 20:45  zhdonghu  阅读(226)  评论(0编辑  收藏  举报