【问题标题】:How to keep DropDownList1 selected value after postback?回发后如何保持 DropDownList1 选定的值?
【发布时间】: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 应该澄清一下。

标签: c# sql asp.net webforms


【解决方案1】:

这可能是因为您的下拉菜单在回发时再次绑定。试试下面的代码。

 protected void Page_Load(object sender, EventArgs e)
        {
          if (!Page.IsPostBack)
                    {
                               //write your dropdownlist1 binding code here
                    }

        }

【讨论】:

  • 我做到了,但它仍然会发生
  • 编写一个干净的代码.. 制作一个函数并在里面编写绑定代码.. 以及为什么将 DataValueField 分配为空白。无论如何,您应该只绑定一次 ddl,以便您现在编写的代码应该可以工作。
  • 在这种情况下.. 请给你写完整的代码,包括 CS 和 aspx 文件
  • 现在检查吗?感谢您的努力 :)
  • 您是否再次重定向到同一页面? Response.Redirect("default.aspx?QSVehicleMake="+makename);
【解决方案2】:

您可以为此目的使用 SESSION 值,使用此值您需要从下拉列表中查找项目

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);
     }

【讨论】:

  • 顺便说一句:if(!String.IsNullOrEmpty(Session["vehiclemake"])) 可能无法编译。最好使用if(!String.IsNullOrEmpty(Session["vehiclemake"] as string)) 或类似的东西。
【解决方案3】:

How to implement cascading DropDownList ASP.Net control?

不要Response.Redirect()SelectedIndexChanged() 中的同一页面。
最好将DropDownList2 放在UpdatePanel 中,以避免整页回发,这就是DropDownList1 被清除的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 2013-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多