【源码】DropDownList绑定数据
DropDownList1.DataSource = sdr;
DropDownList1.DataTextField = "proname";
DropDownList1.DataValueField = "proid";
--选定的Item
DptID_ddl.SelectedIndex = DptID_ddl.Items.IndexOf(DptID_ddl.Items.FindByValue(dr["DptID"].ToString()));
动态绑定方法一:动态绑定数据库中的字段。
SqlConnection conn = UtilitySqlClass.OperateDataBase.ReturnConn();
string strSQL = "select * from CompanyType";
SqlDataAdapter ada = new SqlDataAdapter(strSQL, conn);
DataSet ds = new DataSet();
ada.Fill(ds, "CompanyType");
DropDownList1.DataSource = ds.Tables["CompanyType"].DefaultView;
DropDownList1.DataValueField = ds.Tables["CompanyType"].Columns[1].ColumnName;
DropDownList1.DataTextField = ds.Tables["CompanyType"].Columns[1].ColumnName;
DropDownList1.DataBind();
ds.Dispose();
手动添加
ListItem itemAll = new ListItem();
itemAll.Text = "未分配角色";
itemAll.Value = "";
RoleID_ddl.Items.Insert(0, itemAll);