DataBinder.Eval 方法 (Object, String, String)用法

來源:互聯網
上載者:User

DataBinder.Eval 它帶有三個參數:資料項目的命名容器、資料欄位名稱和格式化字串。

在模板列表如DataList、DataGrid、或 Repeater,命名容器總是Container.DataItem。

(Msdn)
DataBinder.Eval 方法 (Object, String, String)
在運行時計算資料繫結運算式,並將結果格式化為要在請求瀏覽器中顯示的文本。
命名空間:System.Web.UI
參數container

運算式根據其進行計算的對象引用。此標識符必須是以頁的指定語言表示的有效物件識別碼。

expression

從 container 到要放置在繫結控制項屬性中的公用屬性值的導航路徑。此路徑必須是以點分隔的屬性或欄位名稱字串,如 C# 中的 "Tables[0].DefaultView.[0].Price" 或 Visual Basic 中的 "Tables(0).DefaultView.(0).Price"。

format

.NET Framework 格式字串,類似於String.Format所用的字串,可以將Object(是資料繫結運算式的計算結果)轉換為可由請求瀏覽器顯示的String。

傳回值

String,它是資料繫結運算式的計算和向字串類型轉換的結果。

格式化字串參數是可選的。如果忽略參數,DataBinder.Eval 返回物件類型的值

//顯示二位小數
//<%# DataBinder.Eval(Container.DataItem, "UnitPrice", "${0:F2}") %>

註:

  DataBinder是System.Web裡面的一個靜態類,它提供了Eval方法用於簡化資料繫結運算式的編寫,但是它使用的方式是通過 Reflection等開銷比較大的方法來達到易用性,因此其效能並不是最好的。
而Container則根本不是任何一個靜態對象或方法,它是 ASP.NET頁面編譯器在資料繫結事件處理常式內部聲明的局部變數,其類型是可以進行資料繫結的控制項的資料容器類型(如在Repeater內部的資料繫結容器叫RepeaterItem),在這些容器類中基本都有DataItem屬性,因此你可以寫Container.DataItem,這個屬性返回的是你正在被繫結資料源中的那個資料項目。如果你的資料來源是DataTable,則這個資料項目的類型實際是DataRowView。

//{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

轉:http://blog.csdn.net/guye99/archive/2006/04/30/698323.aspx

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.