自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

淺析C# 綁定相關(guān)數(shù)據(jù)方法

開發(fā) 后端
這里將介紹C# 綁定相關(guān)數(shù)據(jù)方法,這些包括靈活的運用數(shù)據(jù)綁定操作,希望對大家有所幫助。

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

靈活的運用C# 綁定數(shù)據(jù)操作

綁定到簡單屬性:<%#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 %>

若數(shù)組里里放的是對象則可能要進行必要的轉(zhuǎn)換后再綁定如:

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

綁定到DataView,DataTable,DataSet:

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

要格式化則:

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

綁定到DataReader:

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

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

在綁定數(shù)據(jù)時經(jīng)常會用到這個句程序:<%# DataBinder.Eval(Container.DataItem,"xxxx")%>或者<%# DataBinder.Eval(Container,"DataItem.xxxx")%>
今天又學到一種,而且微軟也說這種方法的效率要比以上兩種高。

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

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

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

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

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

綁定到DataSet、DataTable時:

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

C# 綁定到DataReader時:

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

關(guān)鍵是Container這個東西,它比較神秘。它的名稱空間是System.ComponentModel。對于它我還需要進一步理解。

初學.NET,現(xiàn)在在看DataGrid控件,在ItemTemplate顯示數(shù)據(jù)時,

DataBinder.Eval(Container.DataItem,"Name")和Container.DataItem("Name")有什么區(qū)別?

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

【編輯推薦】

  1. C# Attribute的概念與使用淺析
  2. C# AttributeUsage的使用淺析
  3. 淺析Attribute在C# WinForm控件開發(fā)中的使用
  4. 淺談C#控件屬性串行化的實現(xiàn)
  5. C#實例詳解TypeConverterAttribute應(yīng)用
責任編輯:彭凡 來源: 和訊空間
相關(guān)推薦

2009-09-15 15:14:27

C# 綁定

2009-09-11 09:15:06

C# get方法

2009-08-20 16:15:19

C# 匿名方法

2009-08-10 17:36:17

C#擴展方法

2009-09-10 14:52:55

C# get

2009-12-30 09:55:51

Silverlight

2009-08-17 17:56:32

C# 枚舉

2009-09-10 09:10:17

C# TextBox換

2009-08-10 16:47:45

Visual C#數(shù)據(jù)

2009-08-20 10:53:23

C#操作內(nèi)存

2009-09-07 09:36:29

C# DisposeDispose方法

2009-08-28 15:52:23

C#利用sharpzi

2009-09-18 10:58:31

C#數(shù)組操作

2009-08-12 17:27:11

C#讀取文件

2009-08-28 12:19:19

C#靜態(tài)方法

2009-08-31 16:01:28

C#水晶報表數(shù)據(jù)獲取方

2009-09-02 19:11:42

C#鼠標滾輪

2009-08-14 16:02:50

C#啟動windows

2009-09-07 09:53:01

C# DisposeDispose方法

2009-09-07 13:41:48

C# HttpWebR提交數(shù)據(jù)方式
點贊
收藏

51CTO技術(shù)棧公眾號