2013-07-13 65 views
0

嗨即時通訊使用下拉列表從表中獲取id,然後刪除行刪除buton當我運行頁面時dropdownlist獲取表中所有的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"); 
+0

是您的數據綁定的邏輯指令只使用Page.IsPostback =假? –

+0

對不起,我是一個新的程序員,我怎麼看到這個? –

+0

如果您正在填充Page_Load方法的組合,請確保僅在Page.IsPostback = false時才執行此邏輯。添加這個作爲答案,以幫助解決問題 –

回答

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() 
} 
+0

很好,現在可以工作了,是的,我正在填充頁面中的組合,你可以解釋一下現在發生了什麼?對不起im noob:p –

+0

@ youssef.abd:使用測試'IsPostBack'屬性(除其他外)僅在第一次請求時執行初始化邏輯,而不是在回發時執行初始化邏輯。更多信息[這裏](http://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback.aspx) –