asp.net

位置:IT落伍者 >> asp.net >> 浏览文章

ASP.NET之数据绑定2


发布日期:2022年04月26日
 
ASP.NET之数据绑定2
NET 框架中 在命名空间SystemCollectionsGeneric 中存在与哈希表和Arraylist 不同的集合只能存储单一类型的对象被称为泛型集合泛型类型集合可创建强类型集合强类型集合需选择存储项的类型他在编译是不可添加不同类型的对象 因为不需要类型的转化在一定程度上提高了数据访问的速度

强类型集合进行数据绑定示例

代码如下

aspx 文件中要做的是从工具箱中拖入相应的控件

aspxcs 文件代码如下

using System;

using SystemCollectionsGeneric;

using SystemLinq;

using SystemWeb;

using SystemWebUI;

using SystemWebUIWebControls;

using SystemCollections;

public partial class _Default : SystemWebUIPage

{

protected void Page_Load(object sender EventArgs e)

{

//创建集合

List<string> fruit = new List<string>()

fruitAdd(苹果

fruitAdd(香蕉

fruitAdd(葡萄

//定义数据源

ListBoxDataSource = fruit;

DropDownListDataSource = fruit;

CheckBoxListDataSource = fruit;

RadioButtonListDataSource = fruit;

//绑定

thisDataBind()

}

}

字典集合

NET 框架中 在命名空间SystemCollectionsGeneric 中有个Dictionary类他表示键和值的集合

要想 避免了类型的不断转化减少了系统装箱和拆箱 且数据类型相对确定的情况下可以采用Dictionary类 例如电子商务网站中存储用户信息的购物车

字典集合进行数据绑定示例

命名空间和aspx 文件同上

代码段如下

public partial class _Default : SystemWebUIPage

{

protected void Page_Load(object sender EventArgs e)

{

//创建一个字典集合每项的索引式整型每项是字符串类型

Dictionary<int string> fruit = new Dictionary<int string>()

fruitAdd( 苹果

fruitAdd( 香蕉

fruitAdd( 葡萄

//绑定列表控件

ListBoxDataSource = fruit;

//选择要显示的字段

ListBoxDataTextField = Value;

//绑定

thisDataBind()

}

}

上一篇:ASP.NET教程:Ref和Out关键字异同

下一篇:在ASP.NET 2.0中实现异常管理