日期:2014-05-17  浏览次数:20463 次

[上一帖太长了,重发一个]批量删除文章,没反应。
for前加上Response.Write("<script>alert('"+GridView1.Rows.Count+"')</script>");
输出10,说明GridView1.Rows.Count没问题了。

有一种说法,没有绑定DataKeysName ,这个不用绑定吧?
怎样绑定?

C# code

protected void delectAll_Click(object sender, EventArgs e)
        {
            delectAll.Attributes.Add("onClick", "javascript:return confirm('确定删除吗?');");

            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["new_conn"].ConnectionString);
            SqlCommand cmd;
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
                if (cb.Checked == true)
                {
                    cmd = new SqlCommand("delete from News where News_id='" + GridView1.DataKeys[i].Value + "'", conn);
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
            } 

           Data_Bind();
        }



------解决方案--------------------
每次cb.Checked都为false,所以if代码块进不去。