c# - 回发后如何保持 DropDownList1 的选定值?

标签 c# sql asp.net webforms

我有两个下拉列表。选择 DropDownList1 值后,DropDownList2 将基于 Dropdownlist1 进行填充。

但在页面刷新后,dropdownlist1 selected value 更改为最上面的值(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);
}

最佳答案

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

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

关于c# - 回发后如何保持 DropDownList1 的选定值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38375120/

相关文章:

c# - 由于 SQL 字符串而重复值?

MySQL - 删除一行并使用正确的排序 ID 对其他行重新排序

c# - 无需连接字符串即可连接到数据库服务器

asp.net - ASP.NET MVC是否在某些方面向后退?

c# - 系统.线程.任务.并行

c# - Entity Framework 6 和 mysql 的问题

c# - 通过rest api c#在jira中设置自定义单选按钮字段

c# - 是否有必要使用构造函数注入(inject)检查空值?

mySQL:将 View 和存储函数与动态参数结合起来

c# - SQL Server报告对齐到网页中心