DropDownlist联动,为什么第二个的值老是不对。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.drop1();
this.drop2();
}
Label1.Text = this.DropDownList1.SelectedValue;
Label2.Text = this.DropDownList2.SelectedValue;
}
public void drop1()
{
con.Open();
string sql = "select * from col ";
DataTable dt = new DataTable();
SqlDataAdapter da=new SqlDataAdapter(sql,con.Conn);
da.Fill(dt);
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "cname ";
DropDownList1.DataValueField = "id ";
DropDownList1.SelectedValue = this.DropDownList1.SelectedValue;
DropDownList1.DataBind();
con.Close();
}
public void drop2()
{
con.Open();
string sql = "select * from cl where ccode= ' "+DropDownList1.SelectedValue+ " ' ";
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(sql, con.Conn);
da.Fill(dt);
DropDownList2.DataSource = dt;
DropDownList2.DataTextField = "name ";
DropDownList2.DataValueField = "id ";
DropDownList2.DataBind();
con.Close();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
this.drop1();
this.drop2();
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
this.drop1();
this.drop2();
}
我用LABEL来测试值,DropDownlist1取值是正确的,但是当切换DropDownlist1的项时,DropDownlist2的总是取到上次DropDownlist2的值,而不是根据DropDownlist1来联动之后,产生的值。必须要选择一次DropDownlist2,才能取到正确的值,到底哪里不对...
[解决办法]
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//this.drop1();
this.drop2();
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
//this.drop1();
//this.drop2();
}
[解决办法]
DropDownList1的AutoPostBack 设置为true
DropDownList2的AutoPostBack 设置为false(默认)
然后
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
this.drop2();
}
[解决办法]
因为你在选ddl时首先调用的是page_load
他只显示上一次的值
你可以在
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Write(DropDownList1.SelectValue+ "--- "+DropDownList2.SelectValue);
}
这样试试就正确了
还有在
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//this.drop1();
this.drop2();
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
//this.drop1();
//this.drop2();
}
[解决办法]
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.drop1();
//this.drop2();
}
Label1.Text = this.DropDownList1.SelectedValue;
Label2.Text = this.DropDownList2.SelectedValue;
}
public void drop1()
{
con.Open();
string sql = "select * from col ";
DataTable dt = new DataTable();
SqlDataAdapter da=new SqlDataAdapter(sql,con.Conn);
da.Fill(dt);
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "cname ";
DropDownList1.DataValueField = "id ";
DropDownList1.SelectedValue = 0;//this.DropDownList1.SelectedValue 第一项可以设成提示项如: "请选择 "之类的
DropDownList1.DataBind();
con.Close();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//this.drop1();
this.drop2();
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
//this.drop1();
//this.drop2();
}