欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

深入理解Asp.net中DataBinder.Eval的用法总结

程序员文章站 2024-03-31 16:14:10
<%# bind("subject") %> //绑定字段<%# container.dataitemindex + 1%> //实现自动编号<...

<%# bind("subject") %> //绑定字段
<%# container.dataitemindex + 1%> //实现自动编号
<%# databinder.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