如何在asp.net c#中检查listview中的复选框状态?

时间:2016-02-26 16:02:11

标签: c# asp.net listview

我有一个Web应用程序,我需要找到列表视图中的复选框,以检查是否已选中它以删除所选数据。我有删除按钮这样做。但是,当我按下按钮时,它没有进入listview代码,也找不到复选框。这是我的示例代码:

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();
    }
}

0 个答案:

没有答案