0

必须禁用选中 chechbox 的行。我已使用以下代码在 RowDataBound 事件中尝试过它,但它显示错误Object reference not set to an instance of an object

  CheckBox cbAttachClrReq = (CheckBox)gvEntity.FindControl("chkAdd");

  if (cbAttachClrReq.Checked)
  {
      this.gvEntity.Rows[e.Row.RowIndex].Enabled = false;
  }
4

3 回答 3

1

您的CheckBox对象可能是null. 所以我还在null代码中添加了一个检查。

if (e.Row.RowType == DataControlRowType.DataRow)
{
    CheckBox cbAttachClrReq = e.Row.FindControl("chkAdd") as CheckBox;

    if (cbAttachClrReq != null && cbAttachClrReq.Checked)
        e.Row.Enabled = false;
}

根据评论的宝贵建议添加,如果对象是,您甚至可以切换CheckBox状态null

if (e.Row.RowType == DataControlRowType.DataRow)
{
    CheckBox cbAttachClrReq = e.Row.FindControl("chkAdd") as CheckBox;
    e.Row.Enabled = cbAttachClrReq == null || !cbAttachClrReq.Checked;
}
于 2012-10-31T06:17:40.563 回答
1

尝试以下操作...我假设复选框位于 gridview 行中...。

protected void gvEntity_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
       CheckBox cbAttachClrReq = (CheckBox) e.Row.FindControl("chkAdd");

      if (cbAttachClrReq.Checked)
      {
          e.Row.Enabled = false;
      }       
    }

}
于 2012-10-31T06:19:29.643 回答
0

尝试以下操作...我假设复选框位于 gridview 行的单元格中...。

protected void gvEntity_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
       CheckBox cbAttachClrReq = (CheckBox) e.Row.Cells[yourCellIndexOFChBox].FindControl("chkAdd");

      if (cbAttachClrReq.Checked)
      {
          e.Row.Enabled = false;
      }       
    }

}
于 2012-10-31T06:35:59.080 回答