日期:2014-05-18 浏览次数:20455 次
/// <summary> /// 执行绑定dropdownlist方法 /// </summary> /// By Lifuyun /// <param name="strsql">要执行的SQL语句</param> /// <param name="tablename">要执行的SQL语句中的表名</param> /// <param name="strtext">DropDownList显示出来的文本</param> /// <param name="strvalue">DropDownList隐藏的对象</param> /// <param name="ddl">DropDownList对象</param> public void ConnectDDL(string strsql, string tablename, string strtext, string strvalue, DropDownList ddl) { OpenConnection(); SqlCommand cmd = new SqlCommand(strsql, _conn); SqlDataReader dr = cmd.ExecuteReader(); //将数据添加到 DropDownList while (dr.Read()) { ddl.Items.Add(new ListItem(dr[strtext].ToString(), dr[strvalue].ToString())); } dr.Close(); cmd.Dispose(); ColseConnection(); }
------解决方案--------------------
你可以试试如下:
DropDownList drp1 = ((DropDownList)gvPerson.Rows[e.NewEditIndex].FindControl("drp1"));
drp1.SelectedIndex = drp1.Items.IndexOf(drp1.Items.FindByText(dt.Rows[0]["f1"].ToString()));