Ticket Q218016
Visible to All Users

ASPxGridView - How to use two-way data-binding expressions with the ASPxCheckBox control

created 16 years ago

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

Comments (1)
DevExpress Support Team 16 years ago

    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

    Answers approved by DevExpress Support

    created 11 years ago (modified 11 years ago)

    [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

      created 16 years ago (modified 11 years ago)

      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

        Comments (1)
        DevExpress Support Team 16 years ago

          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.

          Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

          Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.