首页 文章

如何在回发后保持DropDownList1选中的值?

提问于
浏览
2

我有两个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 回答

  • 0

    您可以使用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);
         }
    
  • 2

    How to implement cascading DropDownList ASP.Net control?

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

  • -1

    这可能是因为您的下拉列表在回发时再次绑定 . 尝试以下代码 .

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

相关问题