当前位置:编程学习 > asp >>

ASP.NET之数据绑定(2-2)

。。 强类型集合 :
    在 .NET 框架中 ,在命名空间System.Collections.Generic 中存在与哈希表和Arraylist 不同的集合,只能存储单一类型的对象被称为泛型集合。泛型类型集合可创建强类型集合。强类型集合需选择存储项的类型,他在编译是不可添加不同类型的对象 ,因为不需要类型的转化在一定程度上提高了数据访问的速度。
    强类型集合进行数据绑定示例:
              代码如下:
              aspx 文件中要做的是从工具箱中拖入相应的控件
              .aspx.cs 文件代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       //创建集合 www.zzzyk.com
        List<string> fruit = new List<string>();
        fruit.Add("苹果");
        fruit.Add("香蕉");
        fruit.Add("葡萄");
        //定义数据源
        ListBox1.DataSource = fruit;
        DropDownList1.DataSource = fruit;
        CheckBoxList1.DataSource = fruit;
        RadioButtonList1.DataSource = fruit;
        //绑定
        this.DataBind();
    }

 。。字典集合
 在 .NET 框架中 ,在命名空间System.Collections.Generic 中有个Dictionary类他表示键和值的集合。
 要想 避免了类型的不断转化减少了系统装箱和拆箱 且数据类型相对确定的情况下可以采用Dictionary类 例如:电子商务网站中存储用户信息的购物车。
 字典集合进行数据绑定示例:
 命名空间和aspx 文件同上
 代码段如下:
public partial class _Default : System.Web.UI.Page
{
    
        protected void Page_Load(object sender, EventArgs e)
        {
            //创建一个字典集合,每项的索引式整型,每项是字符串类型
            Dictionary<int, string> fruit = new Dictionary<int, string>();
            fruit.Add(1, "苹果");
            fruit.Add(2, "香蕉");
            fruit.Add(3, "葡萄");
            //绑定列表控件
            ListBox1.DataSource = fruit;
            //选择要显示的字段   
            ListBox1.DataTextField = "Value";
            //绑定
            this.DataBind();
        }
 
    }

 

摘自 jory
补充:Web开发 , ASP.Net ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,