Hi support,
I have been working through some of the tutorials (mainly Two Way Binding and Card View) in order to setup an Edit Form template that uses two way binding in my ASPxGridView.
It is working well except for problems I am having with the ASPxCheckbox controls that are included on the Edit Form.
I have configured the checkbox as shown below. It is displaying the correct database value when I go into ‘Edit’ mode, however, if I try to create a ‘New’ record, then I get a NullReferenceException. If I remove the code to Bind the checkboxes, then the exception disappears, but obviously the value is no longer displayed when I go into ‘Edit’ mode. Can you advise how to get around this? Can I conditionally bind the checkboxes depending on whether I’m editing or inserting records?
<dxe:ASPxCheckBox ID="execCheckBox" runat="server" Text="Executive Manager Approval Required?" Value='<%# Bind("ExecLevelApproval") %>' Checked='<%# Bind("ExecLevelApproval") %>'></dxe:ASPxCheckBox>
Please let me know if you require further information.
Many thanks,
Steve
ASPxGridView - How to use two-way data-binding expressions with the ASPxCheckBox control
Answers approved by DevExpress Support
[Modified by Vest (DevExpress Support)]
Hello Steve,
This error occurs because the data binding expression can't be evaluated into a Boolean value. It returns null when a new row is going to be created. To make your project workable, you should handle the InitNewRow event in the following manner:
C#protected void grid_InitNewRow(object sender, DevExpress.Web.Data.ASPxDataInitNewRowEventArgs e) {
e.NewValues["ExecLevelApproval"] = false;
}
Thanks,
Vest
[Modified by Kate (DevExpress Support)]
Hi Steven:
It seems that the cause of this issue is the Checked property in the markup. Please do not use it. It is enough to use the Value:
<dxe:aspxcheckbox ID="ASPxCheckBox1" runat="server" Value='<%# Bind("ExecLevelApproval") %>' >
</dxe:aspxcheckbox>
In this case there's no requirement to handle the InitNewRow event because Value can be null and it doesn't cause an exception.
Thanks
Kate.
Other Answers
Hi Vest, I have just noticed I still have a problem with this - sorry for not identifying sooner.
The application no longer causes the exception when I press 'New' on the grid, but the NullReferenceException is thrown when I press 'Update' if I leave the checkboxes unchecked.
Is there another event I need to capture? The application is crashing prior to the RowInserting event being fired.
Thanks,
Steve
Hi Steven:
It seems that the cause of this issue is the Checked property in the markup. Please do not use it. It is enough to use the Value:
<dxe:aspxcheckbox ID="ASPxCheckBox1" runat="server" Value='<%# Bind("ExecLevelApproval") %>' >
</dxe:aspxcheckbox>
In this case there's no requirement to handle the InitNewRow event because Value can be null and it doesn't cause an exception.
Thanks
Kate.
Hello Steve,
This error occurs because the data binding expression can't be evaluated into a Boolean value. It returns null when a new row is going to be created. To make your project workable, you should handle the InitNewRow event in the following manner:
protected void grid_InitNewRow(object sender, DevExpress.Web.Data.ASPxDataInitNewRowEventArgs e) { e.NewValues["ExecLevelApproval"] = false; }
Thanks,
Vest