ASP.NET-關於Container dataitem 與 eval方法介紹

來源:互聯網
上載者:User

標籤:blog   http   使用   os   資料   art   

Container是一個資料容器,代表集合類或者dataview中的一行,而Container。dataitem代表該行的資料;所有的container   被存

放在是一個棧堆stack中,自動的將每一個container壓入棧堆內,最後一行記錄先進棧,最後是第一行;(或者說棧頂是集合類的第

一個container)

GridView的每一行都是一個類型為GridViewRow的Container(容器)對象。GridViewRow有一個屬性叫DataItem,它是GridViewRow對象

綁定到的基礎資料對象,一般來說它就是綁定到GridView的關聯式資料庫資料來源的一行(DataRow)。


在上面的例子中,因為GridViewRow(即GridView的一行)通過原先的資料繫結已經綁定到了一個ItemInformation對象,所以

Container.DataItem可以轉換為temInformation。
總之,Container是GridView的一行,而Container.DataItem是這一行所繫結資料。


每個DataItem代表集合類內的一個對象,或者dataview內的一行記錄(datarowview);

此外Eval(object,string)方法,是通過反射機制在object內尋找string的,所以對於效能有些許損耗。

Eval(container。dataitem,“name”):表示在當前的dataitem對象內尋找 name的值

在使用Repeater的時候,會使用Container.DataItem.其實DataItem就是一個Object,這個Object就是Repeater的DataSource中的一個

元素。

如果 DataSourc是DataTable 那麼這個DataItem就是DataRow

如果DataSource是List<Article> 那麼這個DataItem就是Article。這裡Article是一個自訂的類。可以先做類型轉換 然後調用

Article的方法。比用DataBinder.Eval效率高一些,因為DataBinder.Eval需要使用反射。

使用資料繫結的範例:

綁定到簡單屬性:<%#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).欄位名%>

 

轉自:http://2334495.blog.hexun.com/58444731_d.html

聯繫我們

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