C#數(shù)據(jù)綁定控件中的DataSource屬性
有的時候,你在編程進入一定階段,進一步提升很困難的境況之下,不妨回過頭來看看基礎(chǔ)的東西,或許你會有新的受益,或許能夠真正的體會到孔夫子所謂的“溫故而知新”的真正內(nèi)涵。
常用的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的具體應用。
<1>綁定DataTable,一般都是從數(shù)據(jù)庫取出數(shù)據(jù),然后直接進行綁定,具體的數(shù)據(jù)庫操作的邏輯不再提供。想必大家都已經(jīng)非常熟悉。綁定DataView與這個類似。
程序代碼
- privatevoidBindData()
- {
- //通過業(yè)務邏輯,直接調(diào)用數(shù)據(jù)庫中的數(shù)據(jù)
- DataTablenTable=getTable();
- Repeater1.DataSource=nTable;
- Repeater1.DataBind();
- }
HTML代碼
C#數(shù)據(jù)綁定控件程序代碼
- <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ù)組之類,里面存儲簡單的數(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適當改變
程序代碼
- <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ù)綁定控件程序代碼
- 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代碼
程序代碼
- <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>綁定對象集合,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<User>list=newList<User>();
- list.Add(user1);
- list.Add(user2);
- list.Add(user3);
- list.Add(user4);
- Repeater1.DataSource=list;
- Repeater1.DataBind();
- }
對應的Repeater綁定對象的公共屬性:
C#數(shù)據(jù)綁定控件程序代碼
- <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>
【編輯推薦】