.net中:
DropDownList,ListBox,CheckBoxList,RodioButtonList等控制項的.DataSource屬性可使用
ListItemCollection集合,注意:綁定時須指定DataValueField,DataTextField屬性的值!
不指定欄位後value和Text的值都為Text.
ListItemCollection listItems = new ListItemCollection();
listItems.Add(new ListItem("測試資料一", "1"));
listItems.Add(new ListItem("測試資料二", "2"));
RadioButtonList1.DataSource = listItems;
/*指定欄位值,不指定預設為:
RadioButtonList1.DataValueField = "Text";
RadioButtonList1.DataTextField = "Text";
*/
RadioButtonList1.DataValueField = "Value"; //指定控制項Value欄位值
RadioButtonList1.DataTextField = "Text"; //指定控制項Text欄位值
RadioButtonList1.DataBind();
資料控制項:Repeater,DataList,DetailView,GridView等都可以直接綁定資料來源為泛型資料,如:
List<>,實現IList<>介面資料。public class person
{
private string name;
private int age;
public string Name
{
get { return this.name; }
set { this.name = value; }
}
public int Age
{
get { return this.age; }
set { this.age = value; }
}
public person(string name,int age)
{
this.name = name;
this.age = age;
}
}
//使用資料控制項:
IList<person> testList = new List<person>();
testList.Add(new person("張三",31));
testList.Add(new person("李四",17));
Repeater1.DataSource = testList;
Repeater1.DataBind();