asp.net dropdownlist和listbox

≯℡__Kan透↙ 提交于 2020-03-02 19:15:23
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     选中项的值
        
            
        }

  

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!