本文介绍了在GridView RowDataBound事件中查找并选中/取消选中CheckBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 您好, 受保护 void GridViewTrustee_RowDataBound( object sender,GridViewRowEventArgs e) { if (转换。 ToBoolean(ds.Tables [ 0 ]。行[ 0 ] [ IsDefaultSignatory])== true ) { CheckBox chkisdefault =(CheckBox)GridViewTrustee.FindControl( chkisdefaultsign); chkisdefault.Checked = true ; } else if (Convert.ToBoolean(ds.Tables [ 0 ]。行[ 0 ] [ IsDefaultSignatory])== false ) { CheckBox chkisdefault =(CheckBox) GridViewTrustee.FindControl( chkisdefaultsign); chkisdefault.Checked = false ; } else if (Convert.ToBoolean(ds.Tables [ 0 ]。行[ 0 ] [ IsActive])== true ) { CheckBox chkisact =(CheckBox) GridViewTrustee.FindControl( chisactive); chkisact.Checked = true ; } else if (Convert.ToBoolean(ds.Tables [ 0 ]。行[ 0 ] [ IsActive])== false ) { CheckBox chkisact =(CheckBox) GridViewTrustee.FindControl( chisactive); chkisact.Checked = false ; } } 我在GridView中设置真正的CheckBox,就像上面的代码一样,但是在运行时。 对象引用不是 set 到对象。 描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪 有关错误的更多信息,其中源自代码中的关键字>。 异常详细信息:System.NullReferenceException: Object 引用不是 set 到实例一个对象。 来源错误: 行 335 :{行 336 :CheckBox chkisdefault =(CheckBox)GridViewTrustee.FindControl( chkisdefaultsign); 行 337 :chkisdefault.Checked = true ; 行 338 :} 行 339 : else if (Convert.ToBoolean(ds.Tables [ 0 ]。行[ 0 ] [ IsDefaultSignatory] )== false ) 解决方案 尝试如下... protected void GridViewTrustee_RowDataBound( object sender,GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType .DataRow) { if (Convert.ToBoolean(ds.Tables [ 0 ]。行[ 0 ] [ IsDefaultSignatory])== true ) { CheckBox chkisdefault =(CheckBox)e.Row.FindControl( chkisdefaultsign); chkisdefault.Checked = true ; } else if (Convert.ToBoolean(ds.Tables [ 0 ]。行[ 0 ] [ IsDefaultSignatory])== false ) { CheckBox chkisdefault =(CheckBox) e.Row.FindControl( chkisdefaultsign); chkisdefault.Checked = false ; } else if (Convert.ToBoolean(ds.Tables [ 0 ]。行[ 0 ] [ IsActive])== true ) { CheckBox chkisact =(CheckBox) e.Row.FindControl( chisactive); chkisact.Checked = true ; } else if (Convert.ToBoolean(ds.Tables [ 0 ]。行[ 0 ] [ IsActive])== false ) { CheckBox chkisact =(CheckBox) e.Row.FindControl( chisactive); chkisact.Checked = false ; } } } Hello,protected void GridViewTrustee_RowDataBound(object sender, GridViewRowEventArgs e){ if (Convert.ToBoolean(ds.Tables[0].Rows[0]["IsDefaultSignatory"]) == true) { CheckBox chkisdefault = (CheckBox)GridViewTrustee.FindControl("chkisdefaultsign"); chkisdefault.Checked = true; } else if(Convert.ToBoolean(ds.Tables[0].Rows[0]["IsDefaultSignatory"]) == false) { CheckBox chkisdefault = (CheckBox)GridViewTrustee.FindControl("chkisdefaultsign"); chkisdefault.Checked = false; } else if (Convert.ToBoolean(ds.Tables[0].Rows[0]["IsActive"]) == true) { CheckBox chkisact = (CheckBox)GridViewTrustee.FindControl("chisactive"); chkisact.Checked = true; } else if (Convert.ToBoolean(ds.Tables[0].Rows[0]["IsActive"]) == false) { CheckBox chkisact = (CheckBox)GridViewTrustee.FindControl("chisactive"); chkisact.Checked = false; }}I am setting the true CheckBoxes in GridView like above code but while running having.Object reference not set to an instance of an object.Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.Source Error:Line 335: {Line 336: CheckBox chkisdefault = (CheckBox)GridViewTrustee.FindControl("chkisdefaultsign");Line 337: chkisdefault.Checked = true;Line 338: }Line 339: else if(Convert.ToBoolean(ds.Tables[0].Rows[0]["IsDefaultSignatory"]) == false) 解决方案 Try like below...protected void GridViewTrustee_RowDataBound(object sender, GridViewRowEventArgs e){ if(e.Row.RowType == DataControlRowType.DataRow) { if (Convert.ToBoolean(ds.Tables[0].Rows[0]["IsDefaultSignatory"]) == true) { CheckBox chkisdefault = (CheckBox)e.Row.FindControl("chkisdefaultsign"); chkisdefault.Checked = true; } else if(Convert.ToBoolean(ds.Tables[0].Rows[0]["IsDefaultSignatory"]) == false) { CheckBox chkisdefault = (CheckBox)e.Row.FindControl("chkisdefaultsign"); chkisdefault.Checked = false; } else if (Convert.ToBoolean(ds.Tables[0].Rows[0]["IsActive"]) == true) { CheckBox chkisact = (CheckBox)e.Row.FindControl("chisactive"); chkisact.Checked = true; } else if (Convert.ToBoolean(ds.Tables[0].Rows[0]["IsActive"]) == false) { CheckBox chkisact = (CheckBox)e.Row.FindControl("chisactive"); chkisact.Checked = false; } }} 这篇关于在GridView RowDataBound事件中查找并选中/取消选中CheckBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-23 16:50