DropDownList 绑定
绑定
OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=" + Server.MapPath(".\\App_Data\\mydatabase.mdb")); string Sql = "SELECT * FROM Class ORDER BY Taxis,Id DESC"; OleDbDataAdapter cmd = new OleDbDataAdapter(Sql, conn); DataSet ds = new DataSet(); cmd.Fill(ds,"Class"); this.DropDownList1.DataSource = ds.Tables["Class"].DefaultView; DropDownList1.DataTextField = "Name";//绑定显示名称的字段 DropDownList1.DataValueField = "Id";//绑定值的字段 DropDownList1.DataBind();
添加
for (int i = 1; i <=3; i++) { LIstItem li = new ListItem("文字" + i, "值" + i); //ListItem 代表列表的一项 DropDownList.Items.Add(li); //Items属性维护了列表的项集合 }
获取选择项的Text,使用DropDownList.SelectedItem.Text 获取选择项的Value,使用DropDownList.SelectedItem.Value 或者 DropDownList.SelectedValue