对asp.net不熟悉,刚开始使用!大家帮我看看,到底该怎么改才对?前台:用Gridview
*************************************************************
<asp:CheckBox ID="cbAll" runat="server" AutoPostBack="True" OnCheckedChanged="cbAll_CheckedChanged" /></td> 
***********************************************************
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
BackColor="White" BorderColor="#CC9966" BorderWidth="1px" CellPadding="4" 
DataKeyNames="n_id" HorizontalAlign="Center" 
Width="100%" BorderStyle="None" ShowHeader="False" AllowPaging="True" OnDataBound="GridView1_DataBound" > ***********************************************************<asp:TemplateField > 
<ItemStyle HorizontalAlign="Center"  Width="1%"/> 
<ItemTemplate> 
<asp:CheckBox ID="cb" runat="server" /> 
</ItemTemplate> 
</asp:TemplateField> 
***********************************************************
    protected void Button2_Click(object sender, EventArgs e)
    {
        int Count_BoxCheckValue = this.GridView1.Rows.Count;
        for (int rowindex = 0; rowindex < this.GridView1.Rows.Count; rowindex++)
        {
            if (((CheckBox)this.GridView1.Rows[rowindex].Cells[0].FindControl("cb")).Checked == true)
            {
                int n_id = Convert.ToInt32(this.GridView1.DataKeys[rowindex].Value);
                if (!string.IsNullOrEmpty(n_id.ToString()))
                {
                    AdoSql SqlDel = new AdoSql();
                    SqlDel.ExecuteSqlWrite("delete from tb_news where n_id=" + n_id);
                    this.DataBinds();
                }
            }
            else
            {
                Count_BoxCheckValue--;
            }
        }
        if (Count_BoxCheckValue <= 0)
        {
            Response.Write("<script language='javascript'>alert('没有选中需要处理的行记录!!')</script>");
        }
        DataBinds();
    }

解决方案 »

  1.   

    试试将for替换为:
    foreach GridViewRow gr in GridView1.rows
    if (((CheckBox)gr.Cells[0].FindControl("cb")).Checked == true)
    …………
      

  2.   

    /// <summary>
            /// 执行SQL语句-写 
            /// </summary>
            /// <param name="SqlString">SQL语句</param>
            /// <returns></returns>
            public Boolean ExecuteSqlWrite(string SqlString)        
            {
                OleDbCommand SqlCmd = new OleDbCommand();
                SqlCmd.Connection = new OleDbConnection(ConfigurationManager.AppSettings["ConnString1"] + System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["ConnString2"].Trim()));
                SqlCmd.CommandText = SqlString;
                SqlCmd.CommandType = CommandType.Text;
                SqlCmd.Connection.Open();
                SqlCmd.ExecuteNonQuery();
                SqlCmd.Connection.Close();
                return true;        }
      

  3.   

    this.DataBinds();
    把这句注释掉,应该可以了
      

  4.   

    if (!string.IsNullOrEmpty(n_id.ToString()))
                    {
                        AdoSql SqlDel = new AdoSql();
                        SqlDel.ExecuteSqlWrite("delete from tb_news where n_id=" + n_id);
                        this.DataBinds();//楼主在这里对GridView重新绑定了吧?那么,更新了第一条记录后重新绑定,所有CheckBox也都恢复到未选中的状态了~  这行代码应该去掉~
                    }
      

  5.   

    http://www.cnblogs.com/eddie005/archive/2006/07/04/GridView_CheckBox.html