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

关于gridview里面chenckbox的问题
想问一下大神们,我在GirdView里面加了checkbox,但是问什么下面的代码里面的cb.check老是false
protected void btnAddStaff_Click(object sender, EventArgs e)
  {
  bool atLeastOneRowDeleted = false;

  // Iterate through the Products.Rows property
  foreach (GridViewRow row in gvShowStaff.Rows)
  {
  // Access the CheckBox
  if ((CheckBox)row.FindControl("cbSelectStaff") == null)
  {
  atLeastOneRowDeleted = true;
  atLeastOneRowDeleted = true;
  atLeastOneRowDeleted = true;
  atLeastOneRowDeleted = true;
  }
  CheckBox cb = (CheckBox)row.FindControl("cbSelectStaff");
  if (cb != null && cb.Checked==true)
  {
  // Delete row! (Well, not really...)
  atLeastOneRowDeleted = true;

  // First, get the ProductID for the selected row
  int productID =
  Convert.ToInt32(gvShowStaff.DataKeys[row.RowIndex].Value);

  // "Delete" the row
  DeleteResults.Text += string.Format(
  "This would have deleted ProductID {0}<br />", productID);
  }
  }

  // Show the Label if at least one row was deleted...
  DeleteResults.Visible = atLeastOneRowDeleted;
  }
}
取到了cb不为空,但是cb.checked一直是false,如果我把开始的时候checkbox的checked属性设为true,那他就一直是true,改都改不了。求大神们解释一下,小弟初学,感激不尽。。。。

------解决方案--------------------
我已经解决了,楼下不用回复了,谢谢各位大神。。。