必须禁用选中复选框的行。我已使用以下代码在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;
}
答案 0 :(得分: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;
}
ADDED 根据评论提供的宝贵建议,如果对象为CheckBox
,您甚至可以切换null
州:
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox cbAttachClrReq = e.Row.FindControl("chkAdd") as CheckBox;
e.Row.Enabled = cbAttachClrReq == null || !cbAttachClrReq.Checked;
}
答案 1 :(得分: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;
}
}
}
答案 2 :(得分:0)
尝试以下方法...我假设复选框位于gridview行的Cell ....
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;
}
}
}