DataBinder.Eval的基本格式

 

一、DataBinder.Eval的基本格式

在綁定數據時經常會用到這個句程序:

<%# DataBinder.Eval(Container.DataItem,"xxxx")%>

或者

<%# DataBinder.Eval(Container,"DataItem.xxxx")%>

今天又學到一種,而且微軟也說這種方法的效率要比以上兩種高。

<%# ((DataRowView)Container.DataItem)["xxxx"]%>

很有用的,這樣可以在前臺頁面做好多事情了。

還要記住要這樣用必須要在前臺頁面導入名稱空間System.Data,否則會生成錯誤信息。

<%@ Import namespace="System.Data" %>

這種用法其實和<%# ((DictionaryEntry)Container.DataItem).Key%>是一個道理。

Text='<%# DataBinder.Eval(Container.DataItem, "字段") %>'  這樣的方法是最快的

Text='<%# GetPrice() %>'  也可以綁定方法,但方法要是public

Text='<%# "CarDetails.aspx?CarID=" + DataBinder.Eval(Container.DataItem, "CarID") %>'

還可以連接多個字段

關鍵是Container這個東西,它比較神祕。它的名稱空間是System.ComponentModel。對於它我還需要進一步理解。

二、DataBinder.Eval實現判斷選擇

<asp:TemplateColumn HeaderText="性別">

<ItemTemplate>

<%# DGFormatSex(Convert.ToString(DataBinder.Eval(Container.DataItem,"xb"))) %>

</ItemTemplate>

</asp:TemplateColumn>

cs裏定義DGFormatSex方法

protected string DGFormatSex(string xb)

{

  if(xb == "1")

  return "";

  else

  return "";

}

DataBinder.Eval用法範例

//顯示二位小數

//<%# DataBinder.Eval(Container.DataItem, "UnitPrice", "${0:F2}") %>

//{0:G}代表顯示TrueFalse

//<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} 貨幣樣式

靈活的運用數據綁定操作

綁定到簡單屬性:<%#UserName%>

綁定到集合:<asp:ListBox id="ListBox1" datasource='<%# myArray%>' runat="server">

綁定到表達式:<%#(class1.property1.ToString() + "," + class1.property2.ToString())%>

綁定到方法返回值:<%# GetSafestring(str) %>

綁定到Hashtable:<%# ((DictionaryEntry)Container.DataItem).Key%>

綁定到ArrayList:<%#Container.DataItem %>

 

若數組裏裏放的是對象則可能要進行必要的轉換後再綁定如:

<%#((對象類型)Container.DataItem).屬性%>

 

綁定到DataView,DataTable,DataSet:

<%#((DataRowView)Container.DataItem)["字段名"]%>

<%#((DataRowView)Container.DataItem).Rows[0]["字段名"]%>

要格式化則:

<%#string.Format("格式",((DataRowView)Container.DataItem)["字段名"])%>

<%#DataBinder.Eval(Container.DataItem,"字段名","格式")%>

 

綁定到DataReader:

<%#((IDataReader)Container.DataItem).字段名%>

 

當然爲了方便一般使用最多的就是DataBinder類的Eval方法了.不過這樣對於同時要綁定大量的數據效率要低一些

 

在綁定數據時經常會用到這個句程序:<%# DataBinder.Eval(Container.DataItem,"xxxx")%>或者<%# DataBinder.Eval(Container,"DataItem.xxxx")%>

今天又學到一種,而且微軟也說這種方法的效率要比以上兩種高。

 

<%# ((DataRowView)Container.DataItem)["xxxx"]%>

 

很有用的,這樣可以在前臺頁面做好多事情了。

 

還要記住要這樣用必須要在前臺頁面導入名稱空間System.Data,否則會生成錯誤信息。

 

<%@ Import namespace="System.Data" %>

 

這種用法其實和<%# ((DictionaryEntry)Container.DataItem).Key%>是一個道理。

 

綁定到DataSetDataTable:

 

<%#((System.Data.DataRowView)Container.DataItem)["字段名"]%>

<%#((System.Data.DataRowView)Container.DataItem)[索引]%>

 

 

綁定到DataReader:

<%#((System.Data.Common.DbDataRecord)Container.DataItem)[索引]%>

<%#((System.Data.Common.DbDataRecord)Container.DataItem)["字段名"]%>

 

關鍵是Container這個東西,它比較神祕。它的名稱空間是System.ComponentModel。對於它我還需要進一步理解。

 

初學.NET,現在在看DataGrid控件,在ItemTemplate顯示數據時,

DataBinder.Eval(Container.DataItem,"Name")Container.DataItem("Name")有什麼區別?

 

 

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

 

本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/f9inux/archive/2009/05/09/4163098.aspx

 

<%# (Boolean)Eval("Often")?"<font color=green>推薦</font>":"<font color=gray>無推薦</font>"%> //按條件判斷綁定顯示不同內容

<%# Bind("Subject") %> //綁定字段

不行就試試<%# (Container.ItemIndex+1).ToString()%>
<%# 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()了

發佈了29 篇原創文章 · 獲贊 0 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章