新聞中心
C# 綁定相關(guān)數(shù)據(jù)涉及到綁定簡(jiǎn)單屬性,綁定到集合,綁定到表達(dá)式,綁定到方法返回值等等。希望本文能對(duì)大家有所幫助。

創(chuàng)新互聯(lián)專(zhuān)注于馬鞍山網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供馬鞍山營(yíng)銷(xiāo)型網(wǎng)站建設(shè),馬鞍山網(wǎng)站制作、馬鞍山網(wǎng)頁(yè)設(shè)計(jì)、馬鞍山網(wǎng)站官網(wǎng)定制、微信平臺(tái)小程序開(kāi)發(fā)服務(wù),打造馬鞍山網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供馬鞍山網(wǎng)站排名全網(wǎng)營(yíng)銷(xiāo)落地服務(wù)。
靈活的運(yùn)用C# 綁定數(shù)據(jù)操作
綁定到簡(jiǎn)單屬性:<%#UserName%>
綁定到集合:
綁定到表達(dá)式:<%#(class1.property1.ToString() + "," + class1.property2.ToString())%>
綁定到方法返回值:<%# GetSafestring(str) %>
綁定到Hashtable:<%# ((DictionaryEntry)Container.DataItem).Key%>
綁定到ArrayList:<%#Container.DataItem %>
若數(shù)組里里放的是對(duì)象則可能要進(jìn)行必要的轉(zhuǎn)換后再綁定如:
<%#((對(duì)象類(lèi)型)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).字段名%>
當(dāng)然為了方便一般使用最多的就是DataBinder類(lèi)的Eval方法了.不過(guò)這樣對(duì)于同時(shí)要綁定大量的數(shù)據(jù)效率要低一些
在綁定數(shù)據(jù)時(shí)經(jīng)常會(huì)用到這個(gè)句程序:<%# DataBinder.Eval(Container.DataItem,"xxxx")%>或者<%# DataBinder.Eval(Container,"DataItem.xxxx")%>
今天又學(xué)到一種,而且微軟也說(shuō)這種方法的效率要比以上兩種高。
- <%# ((DataRowView)Container.DataItem)["xxxx"]%>
很有用的,這樣可以在前臺(tái)頁(yè)面做好多事情了。
還要記住要這樣用必須要在前臺(tái)頁(yè)面導(dǎo)入名稱(chēng)空間System.Data,否則會(huì)生成錯(cuò)誤信息。
<%@ Import namespace="System.Data" %>
這種用法其實(shí)和<%# ((DictionaryEntry)Container.DataItem).Key%>是一個(gè)道理。
綁定到DataSet、DataTable時(shí):
- <%#((System.Data.DataRowView)Container.DataItem)["字段名"]%>
- <%#((System.Data.DataRowView)Container.DataItem)[索引]%>
C# 綁定到DataReader時(shí):
- <%#((System.Data.Common.DbDataRecord)Container.DataItem)[索引]%>
- <%#((System.Data.Common.DbDataRecord)Container.DataItem)["字段名"]%>
關(guān)鍵是Container這個(gè)東西,它比較神秘。它的名稱(chēng)空間是System.ComponentModel。對(duì)于它我還需要進(jìn)一步理解。
初學(xué).NET,現(xiàn)在在看DataGrid控件,在ItemTemplate顯示數(shù)據(jù)時(shí),
DataBinder.Eval(Container.DataItem,"Name")和Container.DataItem("Name")有什么區(qū)別?
DataBinder是System.Web里面的一個(gè)靜態(tài)類(lèi),它提供了Eval方法用于簡(jiǎn)化數(shù)據(jù)綁定表達(dá)式的編寫(xiě),但是它使用的方式是通過(guò)Reflection等開(kāi)銷(xiāo)比較大的方法來(lái)達(dá)到易用性,因此其性能并不是***的。而Container則根本不是任何一個(gè)靜態(tài)的對(duì)象或方法,它是ASP.NET頁(yè)面編譯器在數(shù)據(jù)綁定事件處理程序內(nèi)部聲明的局部變量,其類(lèi)型是可以進(jìn)行數(shù)據(jù)綁定的控件的數(shù)據(jù)容器類(lèi)型(如在Repeater內(nèi)部的數(shù)據(jù)綁定容器叫RepeaterItem),在這些容器類(lèi)中基本都有DataItem屬性,因此你可以寫(xiě)Container.DataItem,這個(gè)屬性返回的是你正在被綁定的數(shù)據(jù)源中的那個(gè)數(shù)據(jù)項(xiàng)。如果你的數(shù)據(jù)源是DataTable,則這個(gè)數(shù)據(jù)項(xiàng)的類(lèi)型實(shí)際是DataRowView。
網(wǎng)頁(yè)題目:淺析C#綁定相關(guān)數(shù)據(jù)方法
網(wǎng)址分享:http://m.fisionsoft.com.cn/article/ccesooj.html


咨詢(xún)
建站咨詢(xún)
