C#資料繫結控制項中的DataSource屬性淺談

來源:互聯網
上載者:User

有的時候,你在編程進入一定階段,進一步提升很困難的境況之下,不妨回過頭來看看基礎的東西,或許你會有新的受益,或許能夠真正的體會到孔夫子所謂的“溫故而知新”的真正內涵。
常用的C#資料繫結控制項有:Repeater、DataList、GridView、DetailsView等,在這裡我拿Repeater來簡單說明問題。
使用該屬性指定用來填充Repeater控制項的資料來源。DataSource可以是任何System.Collections.IEnumerable對象,
如用於訪問資料庫的System.Data.DataView、System.Collections.ArrayList、System.Collections.Hashtable、數組或IListSource對象。
常用的資料來源:
一個DataTable
一個DataView
一個DataSet
任何實現IListSource介面的組件
任何實現IList介面的組件
注意:
若要綁定到對象的強型別數組,該物件類型必須包含公用屬性。
下面通過幾個簡單的執行個體來介紹DataSource的具體應用。
<1>綁定DataTable,一般都是從資料庫取出資料,然後直接進行綁定,具體的資料庫操作的邏輯不再提供。想必大家都已經非常熟悉。綁定DataView與這個類似。
程式碼

複製代碼 代碼如下:privatevoidBindData()
{
//通過商務邏輯,直接調用資料庫中的資料
DataTablenTable=getTable();

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

HTML代碼
C#資料繫結控制項程式碼

複製代碼 代碼如下:<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、一維數組之類,裡面儲存簡單的資料。
ArrayList
C#資料繫結控制項程式碼

複製代碼 代碼如下: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#資料繫結控制項程式碼複製代碼 代碼如下: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等。這個很是有用,在我們進行資料查詢的時候,經常從資料庫取出資料,為了方便操作,需要封裝成對象,但是有的時候需要將這些對象以列表的形式顯示出來,一種解決方案:對象轉換為DataTable,另一種就是直接調用資料庫。這兩種方案,並不是很理想。而這裡直接將對象集合直接綁定到資料顯示控制項,給我指明一條出路。其實,在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#資料繫結控制項程式碼複製代碼 代碼如下:<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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.