C# 回发后如何保持DropDownList 1的选定值?
我有两个下拉列表。选择DropDownList1值后,DropDownList2将基于DropDownList1填充 但在页面刷新后,DropDownList 1 selected值将更改为最高值(DropDownList由数据库填充) .aspx:C# 回发后如何保持DropDownList 1的选定值?,c#,sql,asp.net,webforms,C#,Sql,Asp.net,Webforms,我有两个下拉列表。选择DropDownList1值后,DropDownList2将基于DropDownList1填充 但在页面刷新后,DropDownList 1 selected值将更改为最高值(DropDownList由数据库填充) .aspx: <asp:DropDownList ID="DropDownList1" runat="server" Width="300px" CssClass="makeselect" OnSelectedIndexChanged="DropDownL
<asp:DropDownList ID="DropDownList1" runat="server" Width="300px" CssClass="makeselect" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True" AppendDataBoundItems="true">
</asp:DropDownList>
这可能是因为您的下拉列表在回发时再次绑定。试试下面的代码
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//write your dropdownlist1 binding code here
}
}
您可以为此使用会话值,使用此值您需要从下拉列表中查找项
protected void Page_Load(object sender, EventArgs e)
{
jpyRATE = 1.4;
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 = "VehicleMake";
DropDownList1.DataBind();
if(!String.IsNullOrEmpty(Session["vehiclemake"] as string))
{
DropDownList1.SelectedIndex =
DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(Session["vehiclemake"].ToString()));
}
con.Close();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string makename = DropDownList1.SelectedItem.Value;
keepname = makename;
Session["vehiclemake"] = keepname;
Response.Redirect("default.aspx?QSVehicleMake="+makename);
}
不要Response.Redirect()
到SelectedIndexChanged()
中的同一页最好将
DropDownList2
放在UpdatePanel
中,以避免整页回发,这是清除DropDownList1
的原因。启用了ViewState吗?那么应该没有问题了,因为所选索引是持久的。@TimSchmelter我认为他正在“刷新”屏幕,但很难说,ViewState在这方面帮不了他。你正在刷新页面吗?或者通过“自动回邮”或“提交”按钮实际回邮?@mxmissile:但即使在浏览器中按F5,所选的值也应相同。但是他不能使用if(IsPostback)
,因为在这种情况下,这将是false
。@TimSchmelter-true,OP应该澄清。顺便说一句:if(!String.IsNullOrEmpty(Session[“vehiclemake”])
可能无法编译。如果(!String.IsNullOrEmpty(Session[“vehiclemake”]as String))或类似的东西,最好使用。我这样做了,但还是碰巧写了一个干净的代码。。制作一个函数并在其中编写绑定代码。。以及为什么DataValueField被指定为空白。无论如何,您应该只绑定一次ddl,这样您现在编写的代码就可以工作了。。请立即编写完整的代码,包括CS和aspx文件检查?感谢您的努力:)是否再次重定向到同一页面?Response.Redirect(“default.aspx?QSVehicleMake=“+makename”);
protected void Page_Load(object sender, EventArgs e)
{
jpyRATE = 1.4;
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 = "VehicleMake";
DropDownList1.DataBind();
if(!String.IsNullOrEmpty(Session["vehiclemake"] as string))
{
DropDownList1.SelectedIndex =
DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(Session["vehiclemake"].ToString()));
}
con.Close();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string makename = DropDownList1.SelectedItem.Value;
keepname = makename;
Session["vehiclemake"] = keepname;
Response.Redirect("default.aspx?QSVehicleMake="+makename);
}