zoukankan      html  css  js  c++  java
  • 格式化数据和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} 货币样式
  • 相关阅读:
    Cyber Security
    Cyber Security
    Cyber Security
    Cyber Security
    Balanced Number HDU
    Round Numbers POJ
    Bomb HDU
    不要62 HDU
    Making the Grade POJ
    You Are the One HDU
  • 原文地址:https://www.cnblogs.com/songxiii/p/2088751.html
Copyright © 2011-2022 走看看