I have a web application and I need to find the check box inside a listview to check whether it is checked or not in order to delete the selected data. And I am having delete button to do so. But when ever I am pressing the button it is not getting inside the listview code and cannot find the checkbox. Here is my sample code:
protected void btnDelete_Click(object sender, EventArgs e)
{
try
{
foreach (ListViewDataItem Ldi in ListView1.Items)
{
if (Ldi.ItemType == ListViewItemType.DataItem)
{
CheckBox cb = (CheckBox)(Ldi.FindControl("chkBxSelect"));
HiddenField hdnFldCandidateID = (HiddenField)(Ldi.FindControl("hdnFldCandidateID"));
string candidateID = hdnFldCandidateID.Value;
if (cb.Checked)
{
con.Open();
query = "delete from candidates where candidate_id=" + candidateID + "";
cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect("candidate-search");
}
else
{
ShowAlert("Please select one cadidate to Delete");
}
}
}
}
catch (Exception ex)
{
Response.Write("Error:" + ex);
}
finally
{
con.Close();
}
}