DataSource可以是任何System.Collections.IEnumerable對象,
如用於訪問數據庫的System.Data.DataView、System.Collections.ArrayList、System.Collections.Hashtable、數組或IListSource對象。
常用的數據源:
一個DataTable
一個DataView
一個DataSet
任何實現IListSource接口的組件
任何實現IList接口的組件
注意:
若要綁定到對象的強類型數組,該對象類型必須包含公共屬性。
下面通過幾個簡單的實例來介紹DataSource的具體應用。
1、綁定dataset、datatable
ddl.datasource=ds;
ddl.databind();
2、綁定數組
string[] list={"a","b","c"};
ddl.datasource=list;
ddl.databind();
這種綁定會試的text和value一樣
3、綁定Dictionary、HashTable
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.DataTextField = "key";如果是dropdownlist控件
//Repeater1.DataValueField = "value";如果是dropdownlist控件
Repeater1.DataBind();
}
.aspx中調用就用
<%# Eval("key")%><%# Eval("value")%>