当前位置:编程学习 > C#/ASP.NET >>

asp.net Eval中的string.format

asp教程.net  eval中的string.format

 

<asp:datalist id= "datalist1 " repeatcolumns
= "5 " width= "600 " 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 ">

<%# 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

有一点大家应该知道,一个属性其实是由一对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 });
    }
}

补充:asp.net教程,.Net开发 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,