在GridView RowDataBound事件中查找并选中/取消选中CheckBox [英] Find and Check/Uncheck CheckBox inside GridView RowDataBound Event

查看:123
本文介绍了在GridView RowDataBound事件中查找并选中/取消选中CheckBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

 受保护  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 ] [ < span class =code-string> IsDefaultSignatory])== false
{
CheckBox chkisdefault =(CheckBox) GridViewTrustee.FindControl( chkisdefaultsign);
chkisdefault.Checked = false ;
}
else if (Convert.ToBoolean(ds.Tables [ 0 ]。行[ 0 ] [ < span class =code-string> IsActive])== true
{
CheckBox chkisact =(CheckBox) GridViewTrustee.FindControl( chisactive);
chkisact.Checked = true ;
}
else if (Convert.ToBoolean(ds.Tables [ 0 ]。行[ 0 ] [ < span class =code-string> IsActive])== false
{
CheckBox chkisact =(CheckBox) GridViewTrustee.FindControl( chisactive);
chkisact.Checked = false ;
}
}



我在GridView中设置真正的CheckBox,就像上面的代码一样,但是在运行时。

< pre lang =c#> 对象引用不是 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 ] [ < span class =code-string> IsDefaultSignatory
])== false
{
CheckBox chkisdefault =(CheckBox) e.Row.FindControl( chkisdefaultsign);
chkisdefault.Checked = false ;
}
else if (Convert.ToBoolean(ds.Tables [ 0 ]。行[ 0 ] [ < span class =code-string> IsActive])== true
{
CheckBox chkisact =(CheckBox) e.Row.FindControl( chisactive);
chkisact.Checked = true ;
}
else if (Convert.ToBoolean(ds.Tables [ 0 ]。行[ 0 ] [ < span class =code-string> 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆