欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#數(shù)據(jù)綁定控件中的DataSource屬性淺談

 更新時(shí)間:2013年02月18日 16:54:02   作者:  
使用該屬性指定用來(lái)填充Repeater控件的數(shù)據(jù)源。DataSource可以是任何System.Collections.IEnumerable對(duì)象, 如用于訪問(wèn)數(shù)據(jù)庫(kù)的System.Data.DataView、System.Collections.ArrayList、System.Collections.Hashtable、數(shù)組或IListSource對(duì)象

有的時(shí)候,你在編程進(jìn)入一定階段,進(jìn)一步提升很困難的境況之下,不妨回過(guò)頭來(lái)看看基礎(chǔ)的東西,或許你會(huì)有新的受益,或許能夠真正的體會(huì)到孔夫子所謂的“溫故而知新”的真正內(nèi)涵。
常用的C#數(shù)據(jù)綁定控件有:Repeater、DataList、GridView、DetailsView等,在這里我拿Repeater來(lái)簡(jiǎn)單說(shuō)明問(wèn)題。
使用該屬性指定用來(lái)填充Repeater控件的數(shù)據(jù)源。DataSource可以是任何System.Collections.IEnumerable對(duì)象,
如用于訪問(wèn)數(shù)據(jù)庫(kù)的System.Data.DataView、System.Collections.ArrayList、System.Collections.Hashtable、數(shù)組或IListSource對(duì)象。
常用的數(shù)據(jù)源:
一個(gè)DataTable
一個(gè)DataView
一個(gè)DataSet
任何實(shí)現(xiàn)IListSource接口的組件
任何實(shí)現(xiàn)IList接口的組件
注意:
若要綁定到對(duì)象的強(qiáng)類型數(shù)組,該對(duì)象類型必須包含公共屬性。
下面通過(guò)幾個(gè)簡(jiǎn)單的實(shí)例來(lái)介紹DataSource的具體應(yīng)用。
<1>綁定DataTable,一般都是從數(shù)據(jù)庫(kù)取出數(shù)據(jù),然后直接進(jìn)行綁定,具體的數(shù)據(jù)庫(kù)操作的邏輯不再提供。想必大家都已經(jīng)非常熟悉。綁定DataView與這個(gè)類似。
程序代碼

復(fù)制代碼 代碼如下:

privatevoidBindData() 

//通過(guò)業(yè)務(wù)邏輯,直接調(diào)用數(shù)據(jù)庫(kù)中的數(shù)據(jù) 
DataTablenTable=getTable(); 

Repeater1.DataSource=nTable; 
Repeater1.DataBind(); 
}

HTML代碼
C#數(shù)據(jù)綁定控件程序代碼

復(fù)制代碼 代碼如下:

<asp:RepeaterIDasp:RepeaterID="Repeater1"runat="server">
<HeaderTemplate>
<table>
<tr>
<thscopethscope="col">
姓名th>
<th>
年齡th>
<tr>
<HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%#Eval("Key")%>
<td>
<td>
<%#Eval("value")%>
<td>
<tr>
<ItemTemplate>
<FooterTemplate>
<table><FooterTemplate>
<asp:Repeater>

<2>綁定Array、ArrayList、List、一維數(shù)組之類,里面存儲(chǔ)簡(jiǎn)單的數(shù)據(jù)。
ArrayList
C#數(shù)據(jù)綁定控件程序代碼

復(fù)制代碼 代碼如下:

privatevoidBindData() 

ArrayListlist=newArrayList(); 
list.Add("Jim"); 
list.Add("Tom"); 
list.Add("Bluce"); 
list.Add("Mary"); 

Repeater1.DataSource=list; 
Repeater1.DataBind(); 
}

HTML適當(dāng)改變
程序代碼
復(fù)制代碼 代碼如下:

<asp:RepeaterIDasp:RepeaterID="Repeater1"runat="server">
<HeaderTemplate><table><tr><thscopethscope="col">姓名<th><tr><HeaderTemplate>
<ItemTemplate><tr><td><%#Container.DataItem%><td><tr><ItemTemplate>
<FooterTemplate><table><FooterTemplate>
<asp:Repeater>

<3>綁定Dictionary、HashTable
Dictionary
C#數(shù)據(jù)綁定控件程序代碼
復(fù)制代碼 代碼如下:

privatevoidBindData() 

Dictionary<string,int>dic=newDictionary<string,int>(); 
dic.Add("Jim",21); 
dic.Add("Tom",26); 
dic.Add("Bluce",33); 
dic.Add("Mary",18); 

Repeater1.DataSource=dic; 
Repeater1.DataBind(); 
}

HTML代碼
程序代碼
復(fù)制代碼 代碼如下:

<asp:RepeaterIDasp:RepeaterID="Repeater1"runat="server">
<HeaderTemplate><table><tr><thscopethscope="col">姓名<th><th>年齡<th><tr><HeaderTemplate>
<ItemTemplate><tr><td><%#Eval("Key")%>td><td><%#Eval("value")%><td><tr><ItemTemplate>
<FooterTemplate><table><FooterTemplate>
<asp:Repeater>

<4>綁定對(duì)象集合,IList等。這個(gè)很是有用,在我們進(jìn)行數(shù)據(jù)查詢的時(shí)候,經(jīng)常從數(shù)據(jù)庫(kù)取出數(shù)據(jù),為了方便操作,需要封裝成對(duì)象,但是有的時(shí)候需要將這些對(duì)象以列表的形式顯示出來(lái),一種解決方案:對(duì)象轉(zhuǎn)換為DataTable,另一種就是直接調(diào)用數(shù)據(jù)庫(kù)。這兩種方案,并不是很理想。而這里直接將對(duì)象集合直接綁定到數(shù)據(jù)顯示控件,給我指明一條出路。其實(shí),在PetShop4.0就是利用這一點(diǎn),綁定ICollection或者IList。簡(jiǎn)單明了。
一個(gè)簡(jiǎn)單的用戶類,包含兩個(gè)公共屬性。
程序代碼
復(fù)制代碼 代碼如下:

usingSystem; 
usingSystem.Data; 

///

///SummarydescriptionforUser 
///

publicclassUser 

privatestring_Name; 
publicstringName 

get{return_Name;} 
set{_Name=value;} 

privateint_Age; 
publicintAge 

get{return_Age;} 
set{_Age=value;} 

publicUser() 

// 
//TODO:Addconstructorlogichere 
// 

publicUser(stringname,intage) 

_Name=name; 
_Age=age; 

}


綁定對(duì)象集合:
IList
程序代碼
復(fù)制代碼 代碼如下:

privatevoidBindData() 

Useruser1=newUser("Jim",21); 
Useruser2=newUser("Tom",23); 
Useruser3=newUser("Bluce",33); 
Useruser4=newUser("Mary",18); 

IList<User>list=newList<User>(); 
list.Add(user1); 
list.Add(user2); 
list.Add(user3); 
list.Add(user4); 

Repeater1.DataSource=list; 
Repeater1.DataBind(); 
}

對(duì)應(yīng)的Repeater綁定對(duì)象的公共屬性:
C#數(shù)據(jù)綁定控件程序代碼
復(fù)制代碼 代碼如下:

<asp:RepeaterIDasp:RepeaterID="Repeater1"runat="server">
<HeaderTemplate>
<table>
<tr>
<thscopethscope="col">
姓名th>
<th>
年齡<th>
<tr>
<HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%#Eval("Name")%>
<td>
<td>
<%#Eval("Age")%>
<td>
<tr>
<ItemTemplate>
<FooterTemplate>
<table><FooterTemplate>
<asp:Repeater>

相關(guān)文章

最新評(píng)論