新聞中心
有的時候,你在編程進入一定階段,進一步提升很困難的境況之下,不妨回過頭來看看基礎(chǔ)的東西,或許你會有新的受益,或許能夠真正的體會到孔夫子所謂的“溫故而知新”的真正內(nèi)涵。

公司主營業(yè)務(wù):成都網(wǎng)站建設(shè)、做網(wǎng)站、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)推出淮安免費做網(wǎng)站回饋大家。
常用的C#數(shù)據(jù)綁定控件有:Repeater、DataList、GridView、DetailsView等,在這里我拿Repeater來簡單說明問題。
使用該屬性指定用來填充Repeater控件的數(shù)據(jù)源。DataSource可以是任何System.Collections.IEnumerable對象,
如用于訪問數(shù)據(jù)庫的System.Data.DataView、System.Collections.ArrayList、System.Collections.Hashtable、數(shù)組或IListSource對象。
常用的數(shù)據(jù)源:
一個DataTable
一個DataView
一個DataSet
任何實現(xiàn)IListSource接口的組件
任何實現(xiàn)IList接口的組件
注意:
若要綁定到對象的強類型數(shù)組,該對象類型必須包含公共屬性。
下面通過幾個簡單的實例來介紹DataSource的具體應(yīng)用。
<1>綁定DataTable,一般都是從數(shù)據(jù)庫取出數(shù)據(jù),然后直接進行綁定,具體的數(shù)據(jù)庫操作的邏輯不再提供。想必大家都已經(jīng)非常熟悉。綁定DataView與這個類似。
程序代碼
- privatevoidBindData()
- {
- //通過業(yè)務(wù)邏輯,直接調(diào)用數(shù)據(jù)庫中的數(shù)據(jù)
- DataTablenTable=getTable();
- Repeater1.DataSource=nTable;
- Repeater1.DataBind();
- }
HTML代碼
C#數(shù)據(jù)綁定控件程序代碼
- <asp:RepeaterIDasp:RepeaterID="Repeater1"runat="server">
- <thscopethscope="col">
- 姓名 th>
- 年齡 th>
- tr>
- HeaderTemplate>
- <%#Eval("Key")%>
- td>
- <%#Eval("value")%>
- td>
- tr>
- ItemTemplate>
- table> FooterTemplate>
- asp:Repeater>
<2>綁定Array、ArrayList、List、一維數(shù)組之類,里面存儲簡單的數(shù)據(jù)。
ArrayList
C#數(shù)據(jù)綁定控件程序代碼
- privatevoidBindData()
- {
- ArrayListlist=newArrayList();
- list.Add("Jim");
- list.Add("Tom");
- list.Add("Bluce");
- list.Add("Mary");
- Repeater1.DataSource=list;
- Repeater1.DataBind();
- }
HTML適當(dāng)改變
程序代碼
- <asp:RepeaterIDasp:RepeaterID="Repeater1"runat="server">
<thscopethscope="col">姓名 th> tr> HeaderTemplate> <%#Container.DataItem%> td> tr> ItemTemplate> table> FooterTemplate> - asp:Repeater>
<3>綁定Dictionary、HashTable
Dictionary
C#數(shù)據(jù)綁定控件程序代碼
- privatevoidBindData()
- {
- Dictionary
,int>dic=newDictionary ,int>(); - dic.Add("Jim",21);
- dic.Add("Tom",26);
- dic.Add("Bluce",33);
- dic.Add("Mary",18);
- Repeater1.DataSource=dic;
- Repeater1.DataBind();
- }
HTML代碼
程序代碼
- <asp:RepeaterIDasp:RepeaterID="Repeater1"runat="server">
<thscopethscope="col">姓名 th> 年齡 th> tr> HeaderTemplate> <%#Eval("Key")%> td> <%#Eval("value")%> td> tr> ItemTemplate> table> FooterTemplate> - asp:Repeater>
<4>綁定對象集合,IList等。這個很是有用,在我們進行數(shù)據(jù)查詢的時候,經(jīng)常從數(shù)據(jù)庫取出數(shù)據(jù),為了方便操作,需要封裝成對象,但是有的時候需要將這些對象以列表的形式顯示出來,一種解決方案:對象轉(zhuǎn)換為DataTable,另一種就是直接調(diào)用數(shù)據(jù)庫。這兩種方案,并不是很理想。而這里直接將對象集合直接綁定到數(shù)據(jù)顯示控件,給我指明一條出路。其實,在PetShop4.0就是利用這一點,綁定ICollection或者IList。簡單明了。
一個簡單的用戶類,包含兩個公共屬性。
程序代碼
- 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;
- }
- }
綁定對象集合:
IList
程序代碼
- privatevoidBindData()
- {
- Useruser1=newUser("Jim",21);
- Useruser2=newUser("Tom",23);
- Useruser3=newUser("Bluce",33);
- Useruser4=newUser("Mary",18);
- IList
list=newList (); - list.Add(user1);
- list.Add(user2);
- list.Add(user3);
- list.Add(user4);
- Repeater1.DataSource=list;
- Repeater1.DataBind();
- }
對應(yīng)的Repeater綁定對象的公共屬性:
C#數(shù)據(jù)綁定控件程序代碼
- <asp:RepeaterIDasp:RepeaterID="Repeater1"runat="server">
- <thscopethscope="col">
- 姓名 th>
- 年齡 th>
- tr>
- HeaderTemplate>
- <%#Eval("Name")%>
- td>
- <%#Eval("Age")%>
- td>
- tr>
- ItemTemplate>
- table> FooterTemplate>
- asp:Repeater>
當(dāng)前標(biāo)題:C#數(shù)據(jù)綁定控件中的DataSource屬性
標(biāo)題URL:http://www.5511xx.com/article/cddoijh.html


咨詢
建站咨詢
