【轉載】一些資料格式化-Eval( ” “)和DataBinder.Eval(Container.DataItem, ” “)的區別及用法&Da

來源:互聯網
上載者:User

 

一些資料格式化-Eval( " ")和DataBinder.Eval(Container.DataItem, " ")的區別及用法

 

ASP.NET 2.0改善了模板中的資料繫結操作,把v1.x中的資料繫結文法DataBinder.Eval(Container.DataItem, fieldname)簡化為Eval(fieldname)。Eval方法與DataBinder.Eval一樣可以接受一個可選的格式化字串參數。縮短的Eval文法與DataBinder.Eval的不同點在於,Eval會根據最近的容器物件(例如DataListItem)的DataItem屬性來自動地解析欄位,而DataBinder.Eval需要使用參數來指定容器。由於這個原因,Eval只能在資料繫結控制項的模板中使用,而不能用於 Page(頁面)層。當然,ASP.NET 2.0頁面中仍然支援DataBinder.Eval,你可以在不支援簡化的Eval文法的環境中使用它。

Asp.net中DataBinder.Eval用法的總結 
<%# 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

需要實踐一下。

 

 

格式化資料和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='' 也可以Binder 方法,但方法要是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} 貨幣樣式

 

 

 

DataFormatString使用筆記

 

DataFormatString 屬性文法如下:
DataFormatString=”{0:格式字串}”
我們知道在DataFormatString 中的 {0} 表示資料本身,而在冒號後面的格式字串代表所們希望資料顯示的格式;另外在指定的格式符號後可以指定小數所要顯示的位元。例如原來的資料為「1.56」,若格式設定為 {0:N1},則輸出為「1.5」。其常用的數值格式如下表所示:
格式字串 輸入 結果
“{0:C}” 12345.6789 $12,345.68
“{0:C}” -12345.6789 ($12,345.68)
“{0:D}” 12345 12345
“{0:D8}” 12345 00012345
“{0:E}” 12345.6789 1234568E+004
“{0:E10}” 12345.6789 1.2345678900E+004
“{0:F}” 12345.6789 12345.68
“{0:F0}” 12345.6789 12346
“{0:G}” 12345.6789 12345.6789
“{0:G7}” 123456789 1.234568E8
“{0:N}” 12345.6789 12,345.68
“{0:N4}” 123456789 123,456,789.0000
“Total: {0:C}” 12345.6789 Total: $12345.68
其常用的日期格式如下表所示:
格式 說明 輸出格式
d 精簡日期格式 MM/dd/yyyy
D 詳細日期格式 dddd, MMMM dd, yyyy
f 完整格式 (long date + short time) dddd, MMMM dd, yyyy HH:mm
F
完整日期時間格式
(long date + long time)
dddd, MMMM dd, yyyy HH:mm:ss
g 一般格式 (short date + short time) MM/dd/yyyy HH:mm
G 一般格式 (short date + long time) MM/dd/yyyy HH:mm:ss
m,M 月日格式 MMMM dd
s 適中日期時間格式 yyyy-MM-dd HH:mm:ss
t 精簡時間格式 HH:mm
T 詳細時間格式 HH:mm:ss

 

 

 

字元格式設定化

儲存在資料庫裡的是日期時間,但在頁面中只顯示日期,之前是在查詢時就做了處理,用access裡面的mid( format(renewTime,'yyyy-mm-dd'),6,5)把日期取出來再顯示。今天才看到datalist和gridview裡面可以用DataFormatString來進行格式化,使資料按指定的格式顯示。

我的例子:

            <asp:DataList ID="dlistNews" runat="server"  >
            <ItemTemplate>
          <a title=<%#DataBinder.Eval(Container.DataItem,"title")%> target="_blank" href='sysbin/news/news_show.aspx?id=<%#DataBinder.Eval(Container.DataItem,"id")%>'>
          <%#GetSubString(DataBinder.Eval(Container.DataItem,"title").ToString(),24)%></a>
       (<%# DataBinder.Eval(Container.DataItem,"showtime")%>)

<%# DataBinder.Eval(Container.DataItem, "renewtime", "{0:d}")%> 或

<%# DataBinder.Eval(Container.DataItem, "renewtime", "{0:yyyy-MM-dd}")%>
              </span>
            </ItemTemplate>
        </asp:DataList>

如果用bind()來取值,同樣把指定格式的字串放在後面

<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("AddinTime", "{0:yyyy-mm-dd}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

控制數字:DataFormatString = "{0:F}",是預設格式,顯示兩位小數,如果需要顯示的小數位元為其他

值,DataFormatString = "{0:Fn}"即可.

具體用法:

DataFormatString="{0:格式字串}"

在DataFormatString 中的 {0} 表示資料本身,而在冒號後面的格式字串代表所們希望資料顯示的格式;

數字、貨幣格式:
在指定的格式符號後可以指定小數所要顯示的位元。例如原來的資料為「1.56」,若格式設定為 {0:N1},則輸

出為「1.5」。其常用的數值格式如下表所示:

格式字串 輸入 結果
"{0:C}" 12345.6789 $12,345.68
"{0:C}" -12345.6789 ($12,345.68)
"{0:D}" 12345 12345
"{0:D8}" 12345 00012345
"{0:E}" 12345.6789 1234568E+004
"{0:E10}" 12345.6789 1.2345678900E+004
"{0:F}" 12345.6789 12345.68
"{0:F0}" 12345.6789 12346
"{0:G}" 12345.6789 12345.6789
"{0:G7}" 123456789 1.234568E8
"{0:N}" 12345.6789 12,345.68
"{0:N4}" 123456789 123,456,789.0000
"Total: {0:C}" 12345.6789 Total: $12345.68

常用的日期時間格式:

格式 說明 輸出格式
d 精簡日期格式 MM/dd/yyyy
D 詳細日期格式 dddd, MMMM dd, yyyy
f 完整格式 (long date + short time) dddd, MMMM dd, yyyy HH:mm
F
完整日期時間格式
(long date + long time)
dddd, MMMM dd, yyyy HH:mm:ss
g 一般格式 (short date + short time) MM/dd/yyyy HH:mm
G 一般格式 (short date + long time) MM/dd/yyyy HH:mm:ss
m,M 月日格式 MMMM dd
s 適中日期時間格式 yyyy-MM-dd HH:mm:ss
t 精簡時間格式 HH:mm
T 詳細時間格式 HH:mm:ss

注意:如果用DataFormatString格式化GridView ,
同時設定HtmlEncode = false,才能夠使DataFormatString生效.
 <asp:BoundField HeaderText="總計" DataField="Total" DataFormatString="{0:C}" HtmlEncode="False">

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.