【发布时间】:2016-11-17 10:12:00
【问题描述】:
我有两个 DropDownList。选择 DropDownList1 值后,DropDownList2 将根据 Dropdownlist1 进行填充。
但页面刷新后,dropdownlist1 选择的值变为最顶部的值(DropDownList 由数据库填充)。
.aspx:
<asp:DropDownList ID="DropDownList1" runat="server" Width="300px" CssClass="makeselect" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True" AppendDataBoundItems="true">
</asp:DropDownList>
.cs:
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);
}
【问题讨论】:
-
您是否启用了 ViewState?那么应该没有问题,因为选择的索引是持久化的。
-
@TimSchmelter 我认为他正在“刷新”屏幕,但很难说,ViewState 不会帮助他。
-
您在刷新页面吗?还是通过 AutoPostBack 或提交按钮实际发回?
-
@mxmissile: 但即使您在浏览器中按 F5,所选值也应该相同。但他不能使用
if(IsPostback),因为在这种情况下那将是false。 -
@TimSchmelter true,OP 应该澄清一下。