多个删除功能仅删除转发器控件中的一行

时间:2014-07-29 08:26:57

标签: c# asp.net repeater

我有2个转发器控件一个在另一个内,实际上我显示来自两个不同表的内容,我已添加每行的复选框以使用单个按钮单击删除多行,但在选择多个复选框后删除查询只删除一行在一个时间。我只是在转发器控制下添加了checkobx,我试图找出异常,但堆栈没有显示它!以下是我的代码隐藏:

protected void btnDeleteNews_Click(object sender, EventArgs e)
{

    foreach (RepeaterItem item in rptMainBlock.Items)
    {

        if ((item.FindControl("chkDel") as CheckBox).Checked)
        {
            string newsId = (item.FindControl("lblNewsId") as Label).Text;
            cmd = new SqlCommand("DELETE FROM newsImageGal where newsId IN (SELECT newsId from news where newsId=@newsId);DELETE from news where newsId=@newsId", con);
            con.Open();
            cmd.Parameters.AddWithValue("@newsId", newsId);
            cmd.ExecuteNonQuery();

            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "ex", "alert('Selected News Deleted Successfully');", true);
            con.Close();
            Response.Redirect("deleteNews.aspx");

        }
        else
        {
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "ex", "alert('No Data Deleted');", true);
        }
    }

}

请让我知道为什么会这样。

1 个答案:

答案 0 :(得分:2)

问题是你在循环中重定向:

foreach (RepeaterItem item in rptMainBlock.Items)
{
    // delete ..
    Response.Redirect("deleteNews.aspx");  // aborts this thread and ends the page's life-cycle
}

我认为你最后只想做一次:

foreach (RepeaterItem item in rptMainBlock.Items)
{
    // delete ..
}
Response.Redirect("deleteNews.aspx");

但是,当您重定向到另一个页面时,alert('Selected News Deleted Successfully');也毫无意义。