eval()方法在運行時使用反射執行後期綁定計算,所以與標準的asp.net數據綁定方法bind相比,會致使性能明顯降低。它通常用在綁定時須要格式化字符串的狀況下。多數狀況儘可能少用此方法 web
Eval 方法是靜態(只讀)方法,該方法採用數據字段的值做爲參數並將其做爲字符串返回。Bind 方法支持讀/寫功能,能夠檢索數據綁定控件的值並將任何更改提交回數據庫。 數據庫
asp.net中的Bind和Eval:使用 Eval 方法 asp.net
Eval 方法可計算數據綁定控件(如 GridView、DetailsView 和 FormView 控件)的模板中的後期綁定數據表達式。在運行時,Eval 方法調用 DataBinder 對象的 Eval 方法,同時引用命名容器的當前數據項。命名容器一般是包含完整記錄的數據綁定控件的最小組成部分,如 GridView 控件中的一行。所以,只能對數據綁定控件的模板內的綁定使用 Eval 方法。 函數
Eval 方法以數據字段的名稱做爲參數,從數據源的當前記錄返回一個包含該字段值的字符串。能夠提供第二個參數來指定返回字符串的格式,該參數爲可選參數。字符串格式參數使用爲 String 類的 Format 方法定義的語法。 asp.net中的Bind和Eval:使用 Bind 方法性能
Bind 方法與 Eval 方法有一些類似之處,但也存在很大的差別。雖然能夠像使用 Eval 方法同樣使用 Bind 方法來檢索數據綁定字段的值,但當數據能夠被修改時,仍是要使用 Bind 方法。ui
在 asp.net 中,數據綁定控件(如 GridView、DetailsView 和 FormView 控件)可自動使用數據源控件的更新、刪除和插入操做。例如,若是已爲數據源控件定義了 SQL Select、Insert、Delete 和 Update 語句,則經過使用 GridView、DetailsView 或spa
FormView 控件模板中的 Bind 方法,就能夠使控件從模板中的子控件中提取值,並將這些值傳遞給數據源控件。而後數據源控件將執行適當的數據庫命令。出於這個緣由,在數據綁定控件的 EditItemTemplate 或 InsertItemTemplate 中要使用 Bind 函數。.net
Bind 方法一般與輸入控件一塊兒使用,例如由編輯模式中的 GridView 行所呈現的 TextBox 控件。當數據綁定控件將這些輸入控件做爲自身呈現的一部分建立時,該方法即可提取輸入值。orm
Bind 方法採用數據字段的名稱做爲參數,從而與綁定屬性關聯,以下面的示例所示:server
< EditItemTemplate>
< table>
< tr>
< td align=right>
< b>Employee ID:< /b>
< /td>
< td>
< %# Eval("EmployeeID") %>
< /td> < /tr> < tr> < td align=right>
< b>First Name:< /b> < /td> < td>
< asp:TextBox ID="EditFirstNameTextBox" RunAt="Server" Text='< %# Bind("FirstName") %>' />
< /td> < /tr> < tr> < td align=right>
< b>Last Name:< /b> < /td> < td>
< asp:TextBox ID="EditLastNameTextBox" RunAt="Server" Text='< %# Bind("LastName") %>' />
< /td> < /tr> < tr> < td colspan="2"> < asp:LinkButton ID="UpdateButton" RunAt="server" Text="Update" CommandName="Update" />
< asp:LinkButton ID="CancelUpdateButton" RunAt="server" Text="Cancel" CommandName="Cancel" /> < /td> < /tr> < /table> < /EditItemTemplate> 單擊行的 Update 按鈕時,使用 Bind 語法綁定的每一個控件屬性值都會被提取出來,並傳遞給數據源控件以執行更新操做。
asp.net中的Bind和Eval:使用 DataBinder.Eval
asp.net 提供了一個名爲 DataBinder.Eval 的靜態方法,該方法計算後期綁定的數據綁定表達式,並將結果格式化爲字符串(可選)。利用此方法,能夠避免許多在將值強制爲所需數據類型時必須執行的顯式強制轉換操做。
例如,在下面的代碼片斷中,一個整數顯示爲貨幣字符串。使用標準的 asp.net 數據綁定語法,必須首先強制轉換數據行的類型以便檢索數據字段 IntegerValue。而後,這將做爲參數傳遞到 String.Format 方法:
< %# String.Format("{0:c}", ((DataRowView)Container.DataItem)["IntegerValue"]) %> 將此語法與 DataBinder.Eval 的語法進行比較,後者只有三個參數:數據項的命名容器、數據字段名稱和格式字符串。在模板化列表中(如 DataList 類、DataGrid 類或 Repeater 類),命名容器始終是 Container.DataItem。
< %# DataBinder.Eval(Container.DataItem, "IntegerValue", "{0:c}") %> 格式字符串參數是可選的。若是它被忽略,DataBinder.Eval 將返回類型對象的值,以下面的示例所示:
< %# (bool)DataBinder.Eval(Container.DataItem, "BoolValue") %> 當對模板化列表中的控件進行數據綁定時,DataBinder.Eval 特別有用,由於數據行和數據字段一般都必須強制轉換。
NET中Eval()方法大全
.NET技術 2010-02-20 23:58:35 閱讀463 評論0 字號:大中小 訂閱
<%# 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
<%%> <%= %>
<% %> 放在 HTML中寫後臺程序代碼. <%= %> 放在HTML中輸出一個變量,表達式。。
(此篇文章來自xueer8835的專欄,原地址http://blog.csdn.net/xueer8835/article/details/6000309)