.net数据源(DataSource)

.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();



原文地址:https://www.cnblogs.com/ywkpl/p/987753.html