0

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(); } } 
8
  • this issue is so comun from asp.net enviroment , the problem are on postback call , that you have to do is validate no postback call and bind all data to checks , other solution you can use ajax call from controll or using wcf ajaxenableds or if you dont wann make all isssues , the solution is disable postback from button Commented Feb 26, 2016 at 21:36
  • 1
    Post your page_load event handler code here. Commented Feb 26, 2016 at 21:38
  • I am new in javascript. Will you please explain with sample?thanks Commented Feb 26, 2016 at 21:38
  • In your page_load, where you bind your data, be sure to check if not ispostback. e.g. if (!Page.IsPostBack) {databinding} stackoverflow.com/questions/9672522/… Commented Feb 26, 2016 at 21:43
  • You could always attach a value changed event to the checkbox which calls a javascript function to a c# function which saves the boolean value to session. You can then retrieve the value on page load. Not very efficient but its a possibility depending on your scenario Commented Feb 26, 2016 at 21:51

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.