C# oncheckedchanged event handler of aspcheckbox does not fire when checkbox is unc
Hey could you look at this code: I get this error:
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:
{
CheckBox chkBox1 = (CheckBox)e.Row.Cells开发者_开发百科[8].FindControl("chkStatus");
chkBox1.CheckedChanged += new EventHandler(chkStatus_OnCheckedChanged);
chkBox1.Checked = true;
chkBox1.AutoPostBack = true;
}
Are you sure that in your first line, the FindControl method call is actually returning a control. Try putting a breakpoint on line 166 and see what chkBox1 is equal to at that point. My guess is that its value is null.
You should, check your cell count and check if there is a CheckBox at the control referenced by FindControl.
if (e.Row.Cells.Count > 8)
{
CheckBox chkBox1 = e.Row.Cells[8].FindControl("chkStatus") as CheckBox;
if (chkBox1 != null)
{
chkBox1.CheckedChanged += new EventHandler(chkStatus_OnCheckedChanged);
chkBox1.Checked = true;
chkBox1.AutoPostBack = true;
}
}
精彩评论