我正在使用listview并尝试删除已检查的多个数据。但是当按下“删除”按钮时,页面将刷新,并且不会检查复选框,以便不会删除数据。这是我的代码: 页面加载:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["username"] == null)
Response.Redirect("admin-login");
lblusername.Text = Session["username"].ToString();
string nation;
bindListview();
}
删除按钮代码:
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();
}
}