asp.net Eval中的string.format

作者:袖梨 2022-06-25

asp教程.net eval中的string.format

<asp:datalist id= "datalist1 " repeatcolumns
= "5 " runat= "server " datasourceid
= "objectdatasource1 ">
 <itemtemplate>
  <asp:hyperlink id= "hyperlink1 " runat
= "server " navigateurl= '<%# eval( "photoid ",
"photoformviewplain.aspx?id={0} ") %> '>
  <asp:image id= "image1 " runat
= "server " imageurl= '<%# eval( "filename ",
"images/thumbs/{0} ") %> ' /></asp:hyperlink>
  <asp:label id= "captionlabel " runat= "server " text
= '<%# eval( "caption ") %> ' />
 </itemtemplate>
</asp:datalist><br />
<asp:objectdatasource id= "objectdatasource1 " runat
= "server " typename
= "datacomponenttableadapters.photostableadapter
" selectmethod= "getphotosforalbum ">

//绑定字段
//实现自动编号

通常使用的方法(这三个性能最好)



其他用法





//如果属性为字符串类型就不用tostring()了
databinder.eval用法范例

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

//显示二位小数

//{0:g}代表显示true或false

alternatetext=''
imageurl='' />

//转换类型
((string)databinder.eval(container, "dataitem.p_ship_time_sbm8")).substring(4,4)
{0:d} 日期只显示年月日
{0:yyyy-mm-dd} 按格式显示年月日
{0:c} 货币样式


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} 货币样式 标准英国货币样式



显示为 £3,000.10

{0:c} 或 string.format("{0:c}", price); 中国货币样式



显示为 ¥3,000.10

{0:c} 或 string.format("{0:c}", price); 美国货币样式



显示为 $3,000.10

有一点大家应该知道,一个属性其实是由一对get/set方法组成(当然可能缺少其中一个)。而获取了一个属性的propertyinfo对象之后,可以通过它的getsetmethod方法来获取它的设置方法。接下来的工作,不就可以完全交给《方法的直接调用,反射调用与……lambda表达式调用》一文里的dynamicmethodexecutor了吗?因此为dynamicpropertyaccessor添加一个setvalue方法也很简单:

public class dynamicpropertyaccessor
{
...
private dynamicmethodexecutor m_dynamicsetter;

...

public dynamicpropertyaccessor(propertyinfo propertyinfo)
{
...

methodinfo setmethod = propertyinfo.getsetmethod();
if (setmethod != null)
{
this.m_dynamicsetter = new dynamicmethodexecutor(setmethod);
}
}

...

public void setvalue(object o, object value)
{
if (this.m_dynamicsetter == null)
{
throw new notsupportedexception("cannot set the property.");
}

this.m_dynamicsetter.execute(o, new object[] { value });
}
}

相关文章

精彩推荐