I am having a listview and trying to delete multiple data that is checked. But when pressing the Delete button the page is getting refreshed and the Checkbox is not getting checked so that data is not getting deleted. Here is my code: Page Load:
protected void Page_Load(object sender, EventArgs e) { if (Session["username"] == null) Response.Redirect("admin-login"); lblusername.Text = Session["username"].ToString(); string nation; bindListview(); } Delete button code:
protected void btnDelete_Click(object sender, EventArgs e) { try { foreach (ListViewDataItem Ldi in ListView1.Items) { CheckBox cb = (CheckBox)(Ldi.FindControl("chkBxSelect")); HiddenField hdnFldCandidateID = (HiddenField)(Ldi.FindControl("hdnFldCandidateID")); string candidateID = hdnFldCandidateID.Value; if (cb.Checked) { ScriptManager.RegisterStartupScript(this, this.GetType(), "Confirm", "Confirm();", true); con.Open(); query = "delete from candidates where candidate_id=" + candidateID + ""; cmd = new SqlCommand(query, con); cmd.ExecuteNonQuery(); con.Close(); Response.Redirect("candidate-search"); } } } catch (Exception ex) { Response.Write("Error:" + ex); } finally { con.Close(); } }
page_loadevent handler code here.