//一、DataBinder.Eval的基本格式
//在綁定數(shù)據(jù)時經(jīng)常會用到這個句程序:<%...#DataBinder.eval_r(Container.DataItem,"xxxx")%>或者<%...#DataBinder.eval_r(Container,"DataItem.xxxx")%>
//今天又學(xué)到一種,而且微軟也說這種方法的效率要比以上兩種高。
<%...#((DataRowView)Container.DataItem)["xxxx"]%>
//很有用的,這樣可以在前臺頁面做好多事情了。
//還要記住要這樣用必須要在前臺頁面導(dǎo)入名稱空間System.Data,否則會生成錯誤信息。
<%...@ Import namespace="System.Data"%>
//這種用法其實和<%...#((DictionaryEntry)Container.DataItem).Key%>是一個道理。
Text='<%...# DataBinder.eval_r(Container.DataItem,"字段") %>'
//這樣的方法是最快的
Text='<%...# GetPrice() %>'
//也可以綁定方法,但方法要是public的
Text='<%...# "CarDetails.aspx?CarID=" +DataBinder.eval_r(Container.DataItem, "CarID")%>'
//還可以連接多個字段
//關(guān)鍵是Container這個東西,它比較神秘。它的名稱空間是System.ComponentModel。
//二、DataBinder.Eval實現(xiàn)判斷選擇
<asp:TemplateColumnHeaderText="性別">
<ItemTemplate>
<%...#DGFormatSex(Convert.ToString(DataBinder.eval_r(Container.DataItem,"xb")))%>
</ItemTemplate>
</asp:TemplateColumn>
//cs里定義DGFormatSex方法
protected string DGFormatSex(string xb)
{
if(xb == "1")
return "男";
else
return "女";
}
Asp.net框架提供了一個靜態(tài)方法DataBinder.Eval,可以計算后期數(shù)據(jù)綁定表達式的值,并可以將結(jié)果任意格式化為字符串。DataBinder.Eval是很方便的,他排除了許多開發(fā)人員必須作的(通過強制改變值的類型來得到預(yù)期的數(shù)據(jù)類型)顯式轉(zhuǎn)換。尤其是在帶有模板列表的數(shù)據(jù)綁定控件中,因為經(jīng)常需要顯式轉(zhuǎn)換數(shù)據(jù)行和數(shù)據(jù)字段,所以它特別有用。
仔細看下面的代碼,整數(shù)將被顯示為貨幣型字符串。使用標準的asp.net數(shù)據(jù)綁定語法,為了得到數(shù)據(jù)字段IntegerValue,你必須首先顯式轉(zhuǎn)換數(shù)據(jù)行的類型,然后作為String.Format方法的參數(shù)才能得到結(jié)果
<%# String.Format("{0:c}",((DataRowView)Container.DataItem)["IntegerValue"])%>
這樣的語法實在錯綜復(fù)雜的難以記憶。比較而言,DataBinder.Eval就很簡單了。它帶有三個參數(shù):數(shù)據(jù)項的命名容器、數(shù)據(jù)字段名稱和格式化字符串。在模板列表如DataList、DataGrid、或Repeater,命名容器總是Container.DataItem。 Page是另一個可以被DataBinder.Eval使用的命名容器。
<%# DataBinder.eval_r(Container.DataItem,"IntegerValue", "{0:c}") %>
格式化字符串參數(shù)是可選的。如果忽略參數(shù),DataBinder.Eval 返回對象類型的值,就象下面的代碼這樣:
<%# (bool)DataBinder.eval_r(Container.DataItem,"BoolValue") %>
很重要的一點需要注意:由于受后期綁定影響,DataBinder.Eval與標準的數(shù)據(jù)綁定語法相比,在執(zhí)行效率上有明顯的差別。因此有選擇地使用DataBinder.Eval,特別是不需要對字符串進行格式化的時候。
范例
//顯示二位小數(shù)
<%# DataBinder.eval_r(Container.DataItem,"UnitPrice", "${0:F2}") %>
//{0:G}代表顯示True或False
<ItemTemplate>
<asp:Image Width="12" Height="12" Border="0"runat="server"
AlternateText='<%#DataBinder.eval_r(Container.DataItem, "Discontinued", "{0:G}")%>'
ImageUrl='<%# DataBinder.eval_r(Container.DataItem,"Discontinued", "~/images/{0:G}.gif") %>'/>
</ItemTemplate>
//轉(zhuǎn)換類型
((string)DataBinder.eval_r(Container,"DataItem.P_SHIP_TIME_SBM8")).Substring(4,4)
{0:d} 日期只顯示年月日
{0:yyyy-mm-dd} 按格式顯示年月日
{0:c} 貨幣樣式
讀取數(shù)據(jù)時如何轉(zhuǎn)換回車符<%# DataBinder.eval_r(Container.DataItem,"User_Content")%>
<%#DataBinder.eval_r(Container.DataItem,"User_Content").ToString().Replace("","你要轉(zhuǎn)換的符號")%>
轉(zhuǎn)換類型
SpecifierTypeFormat Output (Passed Double1.42) Output (Passed Int -12400)
cCurrency{0:c}$1.42-$12,400
dDecimal{0:d}System.FormatException -12400
eScientific{0:e}1.420000e+000-1.240000e+004
f Fixedpoint{0:f}1.42-12400.00
gGeneral{0:g}1.42-12400
n Number with commas forthousands {0:n}1.42-12,400
r Roundtrippable{0:r}1.42System.FormatException
xHexadecimal{0:x4}System.FormatException cf90
{0:d} 日期只顯示年月日
{0:yyyy-mm-dd} 按格式顯示年月日
樣式取決于 Web.config 中的設(shè)置
{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的基本格式 在綁定數(shù)據(jù)時經(jīng)常會用到這個句程序:或者今天又學(xué)到一種,而且微軟也說這種方法的效率要比以上兩種高。 很有用的,這樣可以在前臺頁面做好多事情了。還要記住要這樣用必須要在前臺頁面導(dǎo)入名稱空間System.Data,否則會生成錯誤信息。 這種用法其實和是一個道理。 Text=''這樣的方法是最快的 Text='' 也可以綁定方法,但方法要是public的 Text='' 還可以連接多個字段關(guān)鍵是Container這個東西,它比較神秘。它的名稱空間是System.ComponentModel。對于它我還需要進一步理解。二、DataBinder.Eval實現(xiàn)判斷選擇 cs里定義DGFormatSex方法 protected stringDGFormatSex(string xb) { if(xb == "1") return "男"; else return "女";} DataBinder.Eval用法范例 DataBinder.Eval用法范例 //顯示二位小數(shù) ////{0:G}代表顯示True或False // // // //轉(zhuǎn)換類型((string)DataBinder.eval_r(Container,"DataItem.P_SHIP_TIME_SBM8")).Substring(4,4) {0:d} 日期只顯示年月日{(diào)0:yyyy-mm-dd} 按格式顯示年月日 {0:c} 貨幣樣式
愛華網(wǎng)


