DropDownList1.SelectedIndex=-1; DropDownList1.Items.FindByValue("选定项目的值").Selected=true; ______________________________________________________________________________________________ 答6: 我有一办法,从数据库检取,这个是radioButtonList,需要使用哈希表,你可以参考一下 using System.Web.SessionState;
public class modrole : System.Web.UI.Page { public Hashtable StateIndex; private void Page_Load(object sender, System.EventArgs e) { StateIndex = new Hashtable(); myConnection = new OleDbConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]); if (!IsPostBack) BindGrid(); }
//数据绑定 public void BindGrid() { OleDbDataReader myReader; String sql = "select * from tb_role order by roleid"; OleDbDataAdapter myCommand = new OleDbDataAdapter(sql, myConnection); DataSet ds = new DataSet(); myCommand.Fill(ds, "tb_role"); DataView dv = ds.Tables["tb_role"].DefaultView; if (ds.Tables["tb_role"].Rows.Count !=0) //如果表不空,绑定数据 { rbtl_role.DataSource=ds.Tables["tb_role"].DefaultView; rbtl_role.DataTextField = "rolename"; rbtl_role.DataValueField = "roleid"; rbtl_role.DataBind(); } //对RadioButtonList进行哈稀编号,保持同RadioButtonList.SelectedIndex的值一致编号 int i = 0; foreach(DataRowView drv in dv ) { StateIndex[drv.Row["roleid"]]=i; i++; } //进行比较,对选中的进行设置 sql = "select roleid from tb_userrole where user_id=1"; OleDbCommand myCmd = new OleDbCommand(sql, myConnection); myConnection.Open(); myReader = myCmd.ExecuteReader(); while (myReader.Read()) { //此句选中设置 rbtl_role.SelectedIndex = Convert.ToInt32(StateIndex[myReader["roleid"]].ToString()); } // always call Close when done reading. myReader.Close(); // Close the connection when done with it. myConnection.Close(); }