如图所示,我的设置:
我的代码如下:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex != 0)
{
string D_Num = DropDownList1.SelectedItem.Value.ToString();
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["myoaConnectionString"]);
con.Open();//连接数据库
string sql = "select E_Name,E_Num,E_D from EmployerInfo where E_D=" + D_Num + "";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
da.Fill(ds);
//SqlCommand com = new SqlCommand(sql, con);
//SqlDataReader dr = com.ExecuteReader();
DropDownList2.DataSource = ds;
DropDownList2.DataTextField = "E_Name";
DropDownList2.DataValueField = "E_Num";
DropDownList2.DataBind();
string Re = DropDownList2.SelectedItem.Text;
this.TextReceive.Value = Re;
con.Close();
}
else
{ DropDownList2.Items.Clear();
DropDownList2.Items.Insert(0, "-- 请选择用户 --");
}
}
问题是,我在选择了部门后,第二个控件没有仍然显示"请选择用户",而是直接出现了选择组内的成员.拜托高手帮忙解答一下.我要怎么才能在选择了部门后,第二个控件仍显示"请选择用户",然后继续进行选择.拜托拜托啊!
我的代码如下:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex != 0)
{
string D_Num = DropDownList1.SelectedItem.Value.ToString();
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["myoaConnectionString"]);
con.Open();//连接数据库
string sql = "select E_Name,E_Num,E_D from EmployerInfo where E_D=" + D_Num + "";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
da.Fill(ds);
//SqlCommand com = new SqlCommand(sql, con);
//SqlDataReader dr = com.ExecuteReader();
DropDownList2.DataSource = ds;
DropDownList2.DataTextField = "E_Name";
DropDownList2.DataValueField = "E_Num";
DropDownList2.DataBind();
string Re = DropDownList2.SelectedItem.Text;
this.TextReceive.Value = Re;
con.Close();
}
else
{ DropDownList2.Items.Clear();
DropDownList2.Items.Insert(0, "-- 请选择用户 --");
}
}
问题是,我在选择了部门后,第二个控件没有仍然显示"请选择用户",而是直接出现了选择组内的成员.拜托高手帮忙解答一下.我要怎么才能在选择了部门后,第二个控件仍显示"请选择用户",然后继续进行选择.拜托拜托啊!
DropDownList2.Items.Insert(0, "-- 请选择用户 --");
DropDownList2.SelectedIndex=0;
试试
还不行就Insert(-1, "-- 请选择用户 --");