动态绑定下拉列表在获取ddl.SelectedValue时始终取第一个值

时间:2014-03-14 18:16:29

标签: c# asp.net

我有一个动态绑定下拉列表,我希望在表中插入所选值。但是当我提交表单时,它将获取dropdownlist的第一个值而不是选定的值,并插入下拉列表的第一个值。

这是我的代码

protected void Page_Load(object sender, EventArgs e)
{
    using (SqlConnection conn = new SqlConnection(connection.getConnection()))
    {
        string sqlGetClass = "select pk_classID,brachName+'-'+classYear as classInfo from tbl_studentClass";
        SqlCommand cmdGetClass = new SqlCommand(sqlGetClass, conn);
        conn.Open();
        SqlDataAdapter da = new SqlDataAdapter(cmdGetClass);
        DataSet ds = new DataSet();
        da.Fill(ds);
        ddlClass.DataSource = ds;
        ddlClass.DataTextField = "classInfo";
        ddlClass.DataValueField = "pk_classID";
        ddlClass.DataBind();
        ddlClass.Items.Insert(0, new ListItem("--SELECT--", ""));
        conn.Close();
    }
}

protected void btnStdRegisterSubmit_Click(object sender, EventArgs e)
{
    string dateOfBirth = txtStdDOBYear.Text+"-"+ddlStdDOBMonth.SelectedValue + "-"+txtStdDOBDate.Text;
    using (SqlConnection conn = new SqlConnection(connection.getConnection()))
    {
        string sqlInsertStd = "Insert into tbl_studentRegistration (firstName,surname,studentUsername,studentPassword,studentDOB,studentGender,studentMobile,class) values(@firstName,@surname,@studentUsername,@studentPassword,@studentDOB,@studentGender,@studentMobile,@class)";
        conn.Open();
        SqlCommand cmdInsertStd = new SqlCommand(sqlInsertStd, conn);
        cmdInsertStd.Parameters.AddWithValue("@firstName", txtStdFirstName.Text);
        cmdInsertStd.Parameters.AddWithValue("@surname", txtStdSurname.Text);
        cmdInsertStd.Parameters.AddWithValue("@studentUsername", txtStdUsername.Text);
        cmdInsertStd.Parameters.AddWithValue("@studentPassword", txtStdPassword.Text);
        cmdInsertStd.Parameters.AddWithValue("@studentDOB", DateTime.Parse(dateOfBirth).ToString("yyyy-MM-dd"));
        cmdInsertStd.Parameters.AddWithValue("@studentGender", ddlStdGender.SelectedValue.ToString());
        cmdInsertStd.Parameters.AddWithValue("@studentMobile", txtStdMobile.Text);
        cmdInsertStd.Parameters.AddWithValue("@class", ddlClass.SelectedValue);

        cmdInsertStd.ExecuteNonQuery();
        conn.Close();

        txtStdFirstName.Text = "";
        txtStdSurname.Text = "";
        txtStdUsername.Text = "";
        ddlClass.SelectedValue = "";
        txtStdPassword.Text = "";
        txtStdConfirmPassword.Text = "";
        ddlStdDOBMonth.SelectedValue = "";
        txtStdDOBDate.Text = "";
        txtStdDOBYear.Text = "";
        ddlStdGender.SelectedValue = "";
        txtStdMobile.Text = "";
        Response.Redirect("~/index.aspx");
    }
}

请帮助我是asp.net的新手

2 个答案:

答案 0 :(得分:0)

问题:您在DropDownList EventHandler中添加的代码中,每个页面请求都会将项目添加到Page_Load。因此即使您选择了不同的项目,DropDownList也始终将第一个项目包含为selectedItem。

解决方案:只有在页面加载时才需要将项目附加到DropDownList,而不是每个PostBack请求都附加。

您可以使用Page.IsPostBack来确定是否有PostBack请求。

试试这个:

if(!Page.IsPostBack)
{
    using (SqlConnection conn = new SqlConnection(connection.getConnection()))
    {
        string sqlGetClass = "select pk_classID,brachName+'-'+classYear as 
                                         classInfo from tbl_studentClass";
        SqlCommand cmdGetClass = new SqlCommand(sqlGetClass, conn);
        conn.Open();
        SqlDataAdapter da = new SqlDataAdapter(cmdGetClass);
        DataSet ds = new DataSet();
        da.Fill(ds);
        ddlClass.DataSource = ds;
        ddlClass.DataTextField = "classInfo";
        ddlClass.DataValueField = "pk_classID";
        ddlClass.DataBind();
        ddlClass.Items.Insert(0, new ListItem("--SELECT--", ""));
        conn.Close();
    }
}

答案 1 :(得分:0)

每次页面回发时都会执行Page_Load,例如当您单击提交按钮时。您应该将使用stateme3nt放在

条件:

    protected void Page_Load(object sender, EventArgs e)
    {
       if(!Page.IsPostBack)
       {
        using (SqlConnection conn = new SqlConnection(connection.getConnection()))
        {
            string sqlGetClass = "select pk_classID,brachName+'-'+classYear as classInfo from tbl_studentClass";
            SqlCommand cmdGetClass = new SqlCommand(sqlGetClass, conn);
            conn.Open();
            SqlDataAdapter da = new SqlDataAdapter(cmdGetClass);
            DataSet ds = new DataSet();
            da.Fill(ds);
            ddlClass.DataSource = ds;
            ddlClass.DataTextField = "classInfo";
            ddlClass.DataValueField = "pk_classID";
            ddlClass.DataBind();
            ddlClass.Items.Insert(0, new ListItem("--SELECT--", ""));
            conn.Close();
        }
      }
    }

因此,每次单击子按钮时,都不会重新填充列表。 (您应该考虑将列表填充代码放在一个单独的方法中)