0

嗨,我使用下拉列表从表中获取 id,然后使用删除按钮删除行,当我运行页面时,下拉列表获取表中的所有 id,但是当我删除它时,即使我选择它,也总是删除最后一个 id其他

List<classe_cv_langues> li6 = new List<classe_cv_langues>();
    SqlConnection con4 = new SqlConnection(@"Data Source=p5-pc\sqlexpress;Initial Catalog=recrutement_online_3;Integrated Security=True");
    SqlCommand cmd4 = new SqlCommand();
    cmd4.Connection = con4;
    con4.Open();
    cmd4.CommandText = "select id from cv_langues as cl inner join cv as c on cl.id_cv = c.id_cv where id_candidat= " + Session["Id_candidat"];
    SqlDataReader dr4 = cmd4.ExecuteReader();
    while (dr4.Read())
    {
        classe_cv_langues p6 = new classe_cv_langues();
        p6.Id = int.Parse(dr4[0].ToString());
        li6.Add(p6);


    }
    dr4.Close();
    con4.Close();


    DropDownList7.DataSource = li6;
    DropDownList7.DataTextField = "id";
    DropDownList7.DataValueField ="id";

    DropDownList7.DataBind()

删除按钮:

SqlConnection con = new SqlConnection(@"Data Source=p5-pc\sqlexpress;Initial Catalog=recrutement_online_3;Integrated Security=True");
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    con.Open();
    cmd.CommandText = "delete from cv_langues where id='"+DropDownList7.SelectedValue+"'";
    cmd.ExecuteNonQuery();
    con.Close();

    Server.Transfer("gestion_cv.aspx");
4

1 回答 1

0

假设您的绑定逻辑在 page_load 方法上。尝试测试Page.IsPostBack

if (!Page.IsPostBack)
{
    List<classe_cv_langues> li6 = new List<classe_cv_langues>();
    SqlConnection con4 = new SqlConnection(@"Data Source=p5-pc\sqlexpress;Initial Catalog=recrutement_online_3;Integrated Security=True");
    SqlCommand cmd4 = new SqlCommand();
    cmd4.Connection = con4;
    con4.Open();
    cmd4.CommandText = "select id from cv_langues as cl inner join cv as c on cl.id_cv = c.id_cv where id_candidat= " + Session["Id_candidat"];
    SqlDataReader dr4 = cmd4.ExecuteReader();
    while (dr4.Read())
    {
        classe_cv_langues p6 = new classe_cv_langues();
        p6.Id = int.Parse(dr4[0].ToString());
        li6.Add(p6);


    }
    dr4.Close();
    con4.Close();


    DropDownList7.DataSource = li6;
    DropDownList7.DataTextField = "id";
    DropDownList7.DataValueField ="id";

    DropDownList7.DataBind()
}
于 2013-07-13T16:02:37.790 回答