天天看點

資料綁定控件----DataSource屬性

資料綁定控件,都有DataSource屬性,用于指定資料源。通常情況下,也是比較常用的就是資料源是資料表(DataTable),其實 DataSource的範圍很廣,我們可以在深入研究的同時,改善我們的程式。

  有的時候,你在程式設計進入一定階段,進一步提升很困難的境況之下,不妨回過頭來看看基礎的東西,或許你會有新的受益,或許能夠真正的體會到孔夫子 所謂的“溫故而知新”的真正内涵。

      常用的資料綁定控件有: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與這個類似。

資料綁定控件----DataSource屬性
資料綁定控件----DataSource屬性

Code

    private void BindData()

    {

        //通過業務邏輯,直接調用資料庫中的 資料

        DataTable nTable = getTable();

        Repeater1.DataSource = nTable;

        Repeater1.DataBind();

    }

 Html代碼

資料綁定控件----DataSource屬性
資料綁定控件----DataSource屬性

Repeater<asp:Repeater ID="Repeater1" runat="server">

                <HeaderTemplate>

                    <table>

                        <tr>

                            <th scope="col">

                                姓 名</th>

                            <th>

                                年齡</th>

                        </tr>

                </HeaderTemplate>

                <ItemTemplate>

                    <tr>

                        <td>

                            <%#Eval("Key") %>

                        </td>

                            <%#Eval("value") %>

                    </tr>

                </ItemTemplate>

                <FooterTemplate>

                    </table></FooterTemplate>

            </asp:Repeater>

<2>綁定Array、ArrayList、List、一維數組之類,裡面存儲簡單的資料。 

資料綁定控件----DataSource屬性
資料綁定控件----DataSource屬性

ArrayList

        ArrayList list = new ArrayList();

        list.Add("Jim");

        list.Add("Tom");

        list.Add("Bluce");

        list.Add("Mary");

        Repeater1.DataSource = list;

Html适當改變

資料綁定控件----DataSource屬性
資料綁定控件----DataSource屬性

        <asp:Repeater ID="Repeater1" runat="server">

            <HeaderTemplate><table><tr><th scope="col">姓名</th></tr></HeaderTemplate>

            <ItemTemplate><tr><td><%#Container.DataItem %></td></tr></ItemTemplate>

            <FooterTemplate></table></FooterTemplate>

        </asp:Repeater>

<3>綁定Dictionary、HashTable 

資料綁定控件----DataSource屬性
資料綁定控件----DataSource屬性

Dictionary

        Dictionary<string, int> dic = new Dictionary<string, int>();

        dic.Add("Jim", 21);

        dic.Add("Tom", 26);

        dic.Add("Bluce", 33);

        dic.Add("Mary", 18);

        Repeater1.DataSource = dic;

Html代碼

資料綁定控件----DataSource屬性
資料綁定控件----DataSource屬性

Code<asp:Repeater ID="Repeater1" runat="server">

            <HeaderTemplate><table><tr><th scope="col">姓名</th><th>年齡</th></tr></HeaderTemplate>

            <ItemTemplate><tr><td><%#Eval("Key") %></td><td><%#Eval("value") %></td></tr></ItemTemplate>

<4>綁定對象集合,IList等。這個很是有用,在我們進行資料查詢的時候,經常從資料庫取出數 據,為了友善操作,需要封裝成對象,但是有的時候需要将這些對象以清單的形式顯示出來,一種解決方案:對象轉換為DataTable,另一種就是直接調用 資料庫。這兩種方案,并不是很理想。而這裡直接将對象集合直接綁定到資料顯示控件,給我指明一條出路。其實,在PetShop4.0就是利用這一點,綁定 ICollection或者IList。簡單明了。

一個簡單的使用者類,包含兩個公共屬性。

資料綁定控件----DataSource屬性
資料綁定控件----DataSource屬性

using System;

using System.Data;

/// <summary>

/// Summary description for User

/// </summary>

public class User

{

    private string _Name;

    public string Name

        get { return _Name; }

        set { _Name = value; }

    private int _Age;

    public int Age

        get { return _Age; }

        set { _Age = value; }

    public User()

        //

        // TODO: Add constructor logic here

    public User(string name,int age)

        _Name = name;

        _Age = age;

}

資料綁定控件----DataSource屬性
資料綁定控件----DataSource屬性

        User user1 = new User("Jim", 21);

        User user2 = new User("Tom",23);

        User user3 = new User("Bluce",33);

        User user4 = new User("Mary",18);

        IList<User> list = new List<User>();

        list.Add(user1);

        list.Add(user2);

        list.Add(user3);

        list.Add(user4);

資料綁定控件----DataSource屬性
資料綁定控件----DataSource屬性

<asp:Repeater ID="Repeater1" runat="server">

                            <%#Eval("Name") %>

                            <%#Eval("Age") %>