下拉菜单取值问题(附代码)(93分)

  • 主题发起人 石头花开
  • 开始时间

石头花开

Unregistered / Unconfirmed
GUEST, unregistred user!
各位大侠:
请教一个较弱问题,请大家帮我分析下,如下:
我绑定DropDownList到数据库后,在实际使用时,若将
Label1.text = Drp1.SelectItem.Text,但是每次只能取到第一个数据,假设下拉菜单为<1,2,3>,我想选择2,并赋予文本Label1,但是却将1赋予文本
我将绑定封装成DLL文件使用的,调用如下:
bindControl.BindToDropDownList("select EmployeeId,Employee from Employee", Employeed, 0, "Employee", "EmployeeId");
DLL中DropDownList的绑定如下:
namespace ProjectDll
{
namespace bindClass
{
public class bindControl
{
private static string strConn = "Data Source=HF;Initial Catalog=CheckRecord;Integrated Security=True;";
//1.绑定DROPDOWNLIST控件:
public static void BindToDropDownList(string sqlText, DropDownList controlID, int k, string txtfield, string idText)
{
SqlConnection conNorthwind;
SqlCommand cmdSelect;
//SqlDataReader rs = null;
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
conNorthwind = new SqlConnection(strConn);
conNorthwind.Open();
cmdSelect = new SqlCommand(sqlText, conNorthwind);
ds.Tables.Add("tb1");
ds.Tables["tb1"].Columns.Add(txtfield);
ds.Tables["tb1"].Columns.Add("number");
DataRow addname = ds.Tables["tb1"].NewRow();
addname[txtfield] = "请选择";
addname["number"] = 0;
ds.Tables["tb1"].Rows.Add(addname);
da.SelectCommand = cmdSelect;
da.Fill(ds, "tb1");
if (k == 1)
{
DataRow addname1 = ds.Tables["tb1"].NewRow();
addname1[txtfield] = "新增";
addname1["number"] = 1;
ds.Tables["tb1"].Rows.Add(addname1);
}
controlID.DataSource = ds.Tables["tb1"];
controlID.DataTextField = ds.Tables["tb1"].Columns[txtfield].ToString();
controlID.DataValueField = ds.Tables["tb1"].Columns["number"].ToString();
//rs = cmdSelect.ExecuteReader();
//rs.Read();
//string i = (rs.GetValue(0)).ToString();
controlID.DataBind();
controlID.SelectedValue = "1";
conNorthwind.Close();
}
代码:
}
}
}
 
顶部