我有两个DropDownLists . 一旦选择了DropDownList1值,DropDownList2将根据Dropdownlist1进行填充 .
但是,在Page refresh的dropdownlist1选择值更改为最高值之后 .
(DropDownList由数据库填充)
CssClass="makeselect"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
AutoPostBack="True" AppendDataBoundItems="true">
</asp:DropDownList>
protected void Page_Load(object sender,EventArgs e){
DataTable subjects = new DataTable();
if (Page.IsPostBack == false)
{
using (SqlConnection con = new SqlConnection(constring))
{
SqlDataAdapter adapter = new SqlDataAdapter("SELECT VehicleMake FROM VehicleDB", con);
adapter.Fill(subjects);
con.Open();
DropDownList1.DataSource = subjects;
DropDownList1.DataTextField = "VehicleMake";
DropDownList1.DataValueField = "";
DropDownList1.DataBind();
con.Close();
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender,EventArgs e){
string makename = DropDownList1.SelectedItem.Value;
keepname = makename;
Response.Redirect("default.aspx?QSVehicleMake="+makename);
}
3 回答
您可以使用SESSION值来实现此目的,使用此值您需要从下拉列表中查找项目
How to implement cascading DropDownList ASP.Net control?
不要
Response.Redirect()
到SelectedIndexChanged()
中的同一页面 .最好将
DropDownList2
放在UpdatePanel
中以避免整页回发,这是DropDownList1
被清除的原因 .这可能是因为您的下拉列表在回发时再次绑定 . 尝试以下代码 .