如何根据条件绑定dropdownlist
例如有两个dropdownlist,一个dropdownlist1,一个dropdownlist2
然后dropdownlist1显示泉洲,dropdownlist2就显示泉洲有的城市
dropdownlist1显示厦门,dropdownlist2就显示厦门有的城市
最好要有代码的
例如有两个dropdownlist,一个dropdownlist1,一个dropdownlist2
然后dropdownlist1显示泉洲,dropdownlist2就显示泉洲有的城市
dropdownlist1显示厦门,dropdownlist2就显示厦门有的城市
最好要有代码的
{ if (!IsPostBack)
{
//绑定第一个下拉框
Dd1DataBind();
//显示第一个下拉框对应的第二个下拉框的内容
Dd2DataBind();
//显示第二个下拉框对应的第三个下拉框的内容
Dd3DataBind();
}
}
//当下拉框改变时,显示相应的内容
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Dd2DataBind();
Dd3DataBind();
}
//当下拉框改变时,显示相应的内容
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
Dd3DataBind();
}
public void Dd1DataBind()
{
DBhelp dd = new DBhelp();
string sqlStr = "Select TypeID,TypeName From a ";
dd.FillDropList(sqlStr, DropDownList1);
}
public void Dd2DataBind()
{
DBhelp ddd = new DBhelp();
int PreID = Convert.ToInt32(DropDownList1.SelectedValue);
string sqlStr1 = "Select ID,name From b Where TypeID=' " + PreID.ToString() + "'";
ddd.FillDropList(sqlStr1,DropDownList2);
}
public void Dd3DataBind()
{
DBhelp dddd = new DBhelp();
int PreID = Convert.ToInt32(DropDownList2.SelectedValue);
string sqlStr2 = "Select IDcun,name From c Where ID=' " + PreID.ToString() + "'";
dddd.FillDropList(sqlStr2,DropDownList3);
}
绑定方法
public void FillDropList(string SQLString, DropDownList drp)
{
SqlConnection connection = new SqlConnection("database=wxd;server=(local);uid=sa;pwd=sa"); SqlCommand cmd = new SqlCommand(SQLString, connection); SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds, "DropList"); drp.DataSource = ds.Tables["DropList"].DefaultView;
drp.DataTextField = ds.Tables["DropList"].Columns[1].ColumnName;
drp.DataValueField = ds.Tables["DropList"].Columns[0].ColumnName;
drp.DataBind();
}
dr1_changed()
{
string str = dr1.SelectedValue;
dr2.DataSource = Load(str);//just the datatable
dr2.DataBind()
}