本帖最后由 chen57298067 于 2011-05-16 09:54:28 编辑

解决方案 »

  1.   

    if (cb != null && cb.Checked)
      

  2.   

    GridView1的Rows可能包含表头,表头的话必然是找不到CheckBox所以,在使用cb之前,要做一个判断。foreach (GridViewRow row in GridView1.Rows)
    {
        CheckBox cb = (CheckBox)row.FindControl("Checkbox1");
        if (cb!=null && cb.Checked)
        {
            strid = GridView1.DataKeys[row.RowIndex].Value.ToString();
            string sql = "delete from class where id= ' " + strid + " ' ";
            // 数据库操作
        }  
    }
    你的括号好像有点问题,对不起来,我自己改了一下。这里还有一个更优化的方法,循环调用删除的,每次调用都需要获取一个数据库连接,然后调用一段sql,这样的效率比较低,比如先拼接id串,这样只有一次获取数据库连接和释放连接的损耗。
    string idlist = "";
    foreach (GridViewRow row in GridView1.Rows)
    {
        CheckBox cb = (CheckBox)row.FindControl("Checkbox1");
        if (cb!=null && cb.Checked)
        {
            idlist += GridView1.DataKeys[row.RowIndex].Value.ToString() + ",";
        }  
    }
    if(idlist.endWith(","))
        idlist = idlist.Substring(0, idlist.length - 1);
    string sql = "delete from class where id in ( ' " + idlist + " ' )";
    // 数据库操作
      

  3.   

    没有找到 Checkbox1 这个控件  看你控件id对不
      

  4.   

    string sql = "delete from class where id= ' " + strid + " ' ";
    放到foreach循环中试一下,貌似你的strid一直被覆盖
      

  5.   


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bind();
            }
        }    private void bind()
        {
            DataSet ds = null;
            ds = DAL.SQLHelper.getDt("select top(13)pid, pname from pinglun");
            this.GridView1.DataSource = ds;
            this.GridView1.DataKeyNames = new string[] { "pid" };
            this.GridView1.DataBind();
            ViewState["ck"] = null;
        }    
        protected void Button1_Click(object sender, EventArgs e)
        {
            ViewState["ck"] = null;
            for (int i = 0; i < this.GridView1.Rows.Count;i++ )
            {            if (((CheckBox)(this.GridView1.Rows[i].FindControl("CheckBox1"))).Checked)
                {
                    ViewState["ck"] += ((Label)(this.GridView1.Rows[i].FindControl("Label1"))).Text+",";                    
                }                       
            }
            getID();    }    private void getID()
        {
            if (ViewState["ck"] != null)
            {
                string idlist = ViewState["ck"].ToString();
                idlist = idlist.Remove(idlist.Length - 1, 1);
                string sql = "delete from pinglun where pid in( " + idlist + ")";                         
                if (DAL.SQLHelper.ExecuteNonQuery(sql) == 1)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "alt", "alert('删除成功!');", true);
                    bind();
                }
            }
            else
            {
              ScriptManager.RegisterStartupScript(this,this.GetType(), "alt", "alert('您没有任何选择!');", true);
            }
        }
      

  6.   

    Button1_Click就是你的删除事件。