.net数据源(DataSource)

本文详细介绍了ASP.NET中各种控件的数据绑定方法,包括DropDownList、ListBox、CheckBoxList、RadioButtonList等如何使用ListItemCollection集合进行数据绑定,并强调了正确设置DataValueField和DataTextField的重要性。此外还介绍了Repeater、DataList等数据控件如何直接绑定泛型数据。
.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/archive/2007/12/08/987753.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值