string sql = "";
        string docID = "";
        bool b = false;
        // 为什么的不到选中的checkbox???????????????????????
 
        //循环检查GridView中被选中的checkbox
        for (int i = 0; i < this.GridView1.Rows.Count; i++)
        {
            b = ((CheckBox)GridView1.Rows[i].FindControl("CheckBox1")).Checked;
            // 如果CheckBox被选中则删除文档
            if (b)
            {
                // 获取文档ID
                docID = GridView1.Rows[i].Cells[0].Text.Trim();                sql = "DELETE FROM relationSystemDocument WHERE systemInfoID='" 
                    + txtSysID.Text.Trim() + "' AND systemEditionID='" 
                    + txtSysEditionID.Text.Trim() + "' AND documentID=" + Convert.ToDecimal(docID);                WebFunction.ExecuteSql(sql);                sql = "";
                docID = "";
            }
        }
为什么明明已经选中的CheckBox,所得到的bool值都是false呢,同样的代码,在以前使用过,正常呀
模版列CheckBox的ID绝对没有错,是CheckBox1

解决方案 »

  1.   

    foreach (GridViewRow gvr in GridView2.Rows)
        {
          CheckBox ch = (CheckBox)gvr.FindControl("ItemCheckBox");
          if (ch.Checked)
          {
     /////
          }
        }
      

  2.   

    http://dotnet.aspx.cc/article/a8efc285-f0b1-4f8f-8e73-2b7d8724a47c/read.aspx
      

  3.   

    public void CheckAll(object sender, System.EventArgs e)
    {
    CheckBox cbAll = (CheckBox)sender;
    if(cbAll.Text=="全选")
    {
    foreach(DataGridItem dgi in dgShow.Items)
    {
    CheckBox cb = (CheckBox)dgi.FindControl("cbSelect");
    cb.Checked = cbAll.Checked;
    }
    }
    }
    private void btnDelete_Click(object sender, System.EventArgs e)
    {
    foreach(DataGridItem dgi in dgShow.Items)
    {
    CheckBox cb = (CheckBox)dgi.FindControl("cbSelect");
    if(cb.Checked)
    {
    //以下执行删除操作
    int nID = int.Parse(dgi.Cells[0].Text);
    string strSql = "delete from tbStudentinfo where studentid="+nID;
    ExecuteSql(strSql);
    }
    }
    dgShow.CurrentPageIndex = 0;
    BindData();
    }
      

  4.   

    楼主删除文件的那个事件是写在什么地方的啊,如果单行提交的话,应该是可以啊。不过如果是写在GridView外面的话,这样肯定是不行的吧,楼主参考一下网上关于gv多行结果同时提交的文章了。
      

  5.   

    lz看看你的treeview的数据绑定是在什么地方,是不是提交的时候重新绑定了
      

  6.   

    孟大哥和tangweisun的方法我试过了,返回的所有CheckBox对象的Checked依然全部为false
      

  7.   

    你应该没有把CheckBox的IspostBack() 设为真吧?
      

  8.   

    flyin2006(我是lèsè_败家子) ( ) 信誉:94    Blog   加为好友  2007-05-22 16:32:02  得分: 0  
     
     
       因为没有提交
    ============================================================================
    不明白……
      

  9.   

    因为你在Page_Load时候重新绑定了GridView导致所有CheckBox重置了。你检查一下应该是我说的问题。
      

  10.   

    fdlm_dark() ( ) 信誉:100    Blog   加为好友  2007-05-22 16:48:24  得分: 0  
     
     
       因为你在Page_Load时候重新绑定了GridView导致所有CheckBox重置了。你检查一下应该是我说的问题。
    ===============================================================================
    正解,因为该页面有一个弹出窗口,向数据库添加与文档的关联,返回的时候自然要在Page.IsPostBack属性中重新绑定以下数据。有什么解决方法么?
      

  11.   

    Do you rebind the GridView at the PostBack phase(when u fire the delete event)?
    as following:if(!IsPostBack) {
    GridView1.DataSource = GetMyDataSOurce();
    GridView1.DataBind();
    }Good Luck!
      

  12.   

    就用我的那方法,可以的 你在模版列中将你的CHECKBOX的ISPOSTBACK设置为TRUE
      

  13.   

    xc_lw(汪汪) ( ) 信誉:100    Blog   加为好友  2007-05-22 17:07:58  得分: 0  
     
     
       重新绑定后,控件状态就变了,你要在重新绑定前判断.
      
    ============================================
    会尝试你的方法的
      

  14.   

    int ick_count = 0;
            CheckBox chkEport = null;
            String sExam_Code = "";
            foreach (GridViewRow gridviewrow in gv.Rows)
            {
                chkEport = (CheckBox)gridviewrow.FindControl("chkEport");
                if (chkEport.Checked)
               {
                   sExam_Code = gridviewrow.Cells[1].Text.Trim();
                   ick_count = ick_count + 1;
                }
            }
      

  15.   

    Jinglecat(晓风残月) ( ) 信誉:94    Blog   加为好友  2007-05-22 17:09:11  得分: 0  
     
     
       Do you rebind the GridView at the PostBack phase(when u fire the delete event)?
    as following:if(!IsPostBack) {
    GridView1.DataSource = GetMyDataSOurce();
    GridView1.DataBind();
    }Good Luck!
    ==============================================================
    这样页面IsPostBack的时候,就不绑定数据了?那怎么及时反映GridView中的变化呀。
      

  16.   

    ((CheckBox)GridView1.Rows[i].FindControl("CheckBox1")).Checked=true//才是设置属性
    应该是((CheckBox)GridView1.Rows[i].FindControl("CheckBox1")).State吧!好像是State判断是否选中,查msdn吧,肯定不是....Checked
      

  17.   

    zlCyclone() ( ) 信誉:100    Blog   加为好友  2007-5-23 9:31:19  得分: 0  
     
     
       
    ((CheckBox)GridView1.Rows[i].FindControl("CheckBox1")).Checked=true//才是设置属性
    应该是((CheckBox)GridView1.Rows[i].FindControl("CheckBox1")).State吧!好像是State判断是否选中,查msdn吧,肯定不是....Checked
    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    哪有State属性……
      

  18.   

                this.gv_UserList.DataSource = dataset();
                    if (!this.IsPostBack)//确保可以正确获得CHECKBOX的状态
                    {
                        this.gv_UserList.DataBind();
                   }  protected void Lbtn_UserDetai_Click(object sender, EventArgs e)
            {
                foreach (GridViewRow row in this.gv_UserList.Rows)
                {
                    //Control ctrl =  row.FindControl("cbSel");
                    CheckBox  ctrl = (CheckBox)row.FindControl("cbSel");                if ((ctrl).Checked)
                    {
                        TableCellCollection cell = row.Cells;
                        string studentCode = cell[1].Text;
                    }
                }        }
    这样可以解决
      

  19.   

                this.gv_UserList.DataSource = dataset();
                    if (!this.IsPostBack)//确保可以正确获得CHECKBOX的状态
                    {
                        this.gv_UserList.DataBind();
                   }  protected void Lbtn_UserDetai_Click(object sender, EventArgs e)
            {
                foreach (GridViewRow row in this.gv_UserList.Rows)
                {
                    //Control ctrl =  row.FindControl("cbSel");
                    CheckBox  ctrl = (CheckBox)row.FindControl("cbSel");                if ((ctrl).Checked)
                    {
                        TableCellCollection cell = row.Cells;
                        string studentCode = cell[1].Text;
                    }
                }        }
    这样可以解决
      

  20.   

                this.gv_UserList.DataSource = dataset();
                    if (!this.IsPostBack)//确保可以正确获得CHECKBOX的状态
                    {
                        this.gv_UserList.DataBind();
                   }  protected void Lbtn_UserDetai_Click(object sender, EventArgs e)
            {
                foreach (GridViewRow row in this.gv_UserList.Rows)
                {
                    //Control ctrl =  row.FindControl("cbSel");
                    CheckBox  ctrl = (CheckBox)row.FindControl("cbSel");                if ((ctrl).Checked)
                    {
                        TableCellCollection cell = row.Cells;
                        string studentCode = cell[1].Text;
                    }
                }        }
    这样可以解决
      

  21.   

      当你单击按钮提交时 页面刷新 没有重新绑定gridview  
      

  22.   

    07年的问题,正好今天我也不小心遇到了。楼主这种情况是因为你在绑定gridview的时候直接在Page_Load里面绑定了,而不是在 if (!IsPostBack){}里面绑定的,原因大家应该都明白了。希望后来人不要这么粗心。