asp.net dropdownlist和listbox

if (!IsPostBack) 
        {
            //页面初次加载时执行这里的内容
            DataSet ds = new DataSet(); //数据集
            ds.Tables.Add("stu");
            ds.Tables["stu"].Columns.Add("id",typeof(int));
            ds.Tables["stu"].Columns.Add("name", typeof(string));
            ds.Tables["stu"].Columns.Add("score", typeof(int));
            ds.Tables["stu"].Rows.Add(new object[] { 1, "张三", 100 });
            ds.Tables["stu"].Rows.Add(new object[] { 2, "李四", 100 });
            ds.Tables["stu"].Rows.Add(new object[] { 3, "周五", 100 });
            ds.Tables["stu"].Rows.Add(new object[] { 4, "郑六", 100 });
            ds.Tables["stu"].Rows.Add(new object[] { 5, "甲七", 100 });
            this.ListBox1.DataSource = ds.Tables["stu"];
            this.ListBox1.DataTextField = "name"; //列表显示的字段
            this.ListBox1.DataValueField = "id";
            this.ListBox1.DataBind();

            //this.ListBox1.SelectedIndex  选中项的索引
            //this.ListBox1.SelectedItem   选中项
            //this.ListBox1.SelectedItem.Text   选中项的内容
            //this.ListBox1.SelectedItem.Value;  选中项的值
            //this.ListBox1.SelectedValue     选中项的值
        
            
        }

  

原文地址:https://www.cnblogs.com/mengluo/p/5976094.html