2009/7/26

DataBinder.Eval 用法

//Bind欄位//自動編號
通常使用的方法(這三個效能最好)
其他用法//如果屬性為字串類型就不用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

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

RefBIND("Budget","{0:N0}")

沒有留言:

張貼留言