GridCheckBoxColumn becomes "empty" when expanding NestedViewTemplate

9 posts, 0 answers
  1. Massimiliano
    Massimiliano avatar
    185 posts
    Member since:
    Oct 2012

    Posted 22 Sep 2013 Link to this post

    I'm facing a very singular behaviour in my RadGrid.
    I have some GridCheckBoxColumn(s) that I mangled in code behind (ASP.NET 4.5 WebForms) to show nome nicer labels in "view" mode and revert back to checkbox in edit mode.
    The code is:
    Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs) Handles RadGrid1.ItemDataBound
        ' Display booleans with text labels when grid is not in editing mode
        If TypeOf e.Item Is GridDataItem AndAlso Not e.Item.IsInEditMode Then
            Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
     
            Dim verifiedCheck As Boolean = DirectCast(item("IsVerified").Controls(0), CheckBox).Checked
            Dim activeCheck As Boolean = DirectCast(item("IsActive").Controls(0), CheckBox).Checked
            Dim approvedCheck As Boolean = DirectCast(item("IsApproved").Controls(0), CheckBox).Checked
            Dim lockedCheck As Boolean = DirectCast(item("IsLockedOut").Controls(0), CheckBox).Checked
     
            If verifiedCheck Then
                item("IsVerified").Text = "<span class=""label label-success autowidth"">Ok</span>"
            Else
                item("IsVerified").Text = "<span class=""label label-warning autowidth"">No</span>"
            End If
     
            If activeCheck Then
                item("IsActive").Text = "<span class=""label label-success autowidth"">Ok</span>"
            Else
                item("IsActive").Text = "<span class=""label label-important autowidth"">No</span>"
            End If
     
            If approvedCheck Then
                item("IsApproved").Text = "<span class=""label label-success autowidth"">Ok</span>"
            Else
                item("IsApproved").Text = "<span class=""label label-warning autowidth"">No</span>"
            End If
     
            If lockedCheck Then
                item("IsLockedOut").Text = "<span class=""label label-warning autowidth"">Ok</span>"
            Else
                item("IsLockedOut").Text = "<span class=""label label-success autowidth"">No</span>"
            End If
        End If
    End Sub

    It works beautifully as you can see in the following screenshot:
    Screenshot 1

    Now the problem arise when I click on the little arrow to expand a row and show the content of my NestedViewTemplate. For some reason I come up with empty checkboxes all over the page, as documented in this second screenshot:
    Screenshot 2

    Thanks in advance as always.
  2. Angel Petrov
    Admin
    Angel Petrov avatar
    1007 posts

    Posted 25 Sep 2013 Link to this post

    Hi Massimiliano,

    The problem in this case is that the GridDataItems are bound initially when the control is created. Later when an item is being expanded they are restored from the control state and ItemDataBound will not fire. That said I would recommend setting the text in the PreRender event of the grid. By looping through the items and executing the same logic as demonstrated in the provided code you should be able to make things work correctly.

    Regards,
    Angel Petrov
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Massimiliano
    Massimiliano avatar
    185 posts
    Member since:
    Oct 2012

    Posted 25 Sep 2013 Link to this post

    Thank you Angel you are precious as always. Tomorrow I will try and report back here!
  5. Massimiliano
    Massimiliano avatar
    185 posts
    Member since:
    Oct 2012

    Posted 06 Nov 2013 Link to this post

    Today is tomorrow (after 1 month ofc) and your suggestion worked great on checkboxes! Thanks

    I'm still having problems in "translating" the grouping re-formatting as well in the pre render event.
    I tryed with:

                ' Display booleans when grouped with text labels
                Dim groupHeaders As GridItem() = RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader)
                For Each item As GridItem In groupHeaders
                    Dim hitem As GridGroupHeaderItem = DirectCast(item, GridGroupHeaderItem)
                    Dim groupDataRow As System.Data.DataRowView = DirectCast(hitem.DataItem, System.Data.DataRowView)
     
                    If hitem.DataCell.Text.StartsWith("Verificato: ") Then
                        If groupDataRow("IsVerified").ToString() = "False" Then
                            hitem.DataCell.Text = "Verificato: <span class=""label label-warning"">No</span>"
                        Else
                            hitem.DataCell.Text = "Verificato: <span class=""label label-success"">Ok</span>"
                        End If
                    End If
    ..........

    But I receive a System.NullReferenceException here (line 241):

    Line 239:
    Line 240:                If hitem.DataCell.Text.StartsWith("Verificato: ") Then
    >>>> Line 241:                    If groupDataRow("IsVerified").ToString() = "False" Then
    Line 242:                        hitem.DataCell.Text = "Verificato: <span class=""label label-warning"">No</span>"
    Line 243:                    Else

    Sorry I'm really a newbie I know... if you have any hint on how to fix this it would be greatly appreciated.
  6. Angel Petrov
    Admin
    Angel Petrov avatar
    1007 posts

    Posted 08 Nov 2013 Link to this post

    Hi Massimiliano,

    Could you please elaborate more on in which event is the below shown logic executed? If it is called in the PreRender event for example hitem.DataItem will be null and that is probably causing the problem. Please modify the logic so that the DataItem is accessed in the OnItemDataBound event.

    Regards,
    Angel Petrov
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  7. Massimiliano
    Massimiliano avatar
    185 posts
    Member since:
    Oct 2012

    Posted 08 Nov 2013 Link to this post

    Sorry Angel I knew it was lame...
    Yes I moved the GridGroupHeaderItem back in the ItemDataBound event where it was originally and just moved to the prerender the checbox part.
    Everything works as expected

    Thanks again
  8. Massimiliano
    Massimiliano avatar
    185 posts
    Member since:
    Oct 2012

    Posted 11 Nov 2013 Link to this post

    Sadly I have to post a followup to this issue that I hoped was closed.
    While everything works as expected so far, I'm facing a strange behaviour. It seems that when I expand a row to show a detail table or when I go in edit mode (custom edit) or do anything else to the grid wich doesn't involve rebinding (but maybe getting data from control state) and I apply the above transformation on pre render, all checkbox results are "false".
    Even more strange is that if I don't apply the trasformation, the checkboxes show correctly with true/false on the right records and fields, while if I apply the above code to read them in pre render with the following code, the always give FALSE (tested with debugger also).

    Protected Sub RadGrid1_PreRender(sender As Object, e As EventArgs) Handles RadGrid1.PreRender
        ' Display booleans with text labels when grid is not in editing mode
        For Each item As GridDataItem In RadGrid1.Items
            GridHelpers.BooleanDisplay(item("IsVerified"), "success", "Ok", "warning", "No")
            GridHelpers.BooleanDisplay(item("IsActive"), "success", "Ok", "important", "No")
            GridHelpers.BooleanDisplay(item("IsApproved"), "success", "Ok", "warning", "No")
            GridHelpers.BooleanDisplay(item("IsLockedOut"), "success", "No", "warning", "Ok")
        Next
    End Sub


    Public Shared Sub BooleanDisplay(item As TableCell, trueLabel As String, trueString As String, falseLabel As String, falseString As String)
        Dim check As CheckBox = DirectCast(item.Controls(0), CheckBox)
        Dim value As Boolean = check.Checked
     
        If value Then
            item.Text = "<span class=""label label-" & trueLabel & " autowidth"">" & trueString & "</span>"
        Else
            item.Text = "<span class=""label label-" & falseLabel & " autowidth"">" & falseString & "</span>"
        End If
    End Sub

    Any hint? Is driving me crazy since if I don't try to replace the checkboxes they are shown correctly, while if I access them on pre render on such events wich don't include rebinding to replace them with labels they always return false.
  9. Antonio Stoilkov
    Admin
    Antonio Stoilkov avatar
    530 posts

    Posted 13 Nov 2013 Link to this post

    Hello Massimilioano,

    Based on your description it is hard to determine the cause of the problem. It could be caused from a custom logic in your application. In such cases we recommend assembling a sample runnable project showing the unwanted behavior so we could debug it and advice you with the best possible solution. You could upload your project on public sharing site and provide us with a public URL.

    Regards,
    Antonio Stoilkov
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  10. Massimiliano
    Massimiliano avatar
    185 posts
    Member since:
    Oct 2012

    Posted 19 Nov 2013 Link to this post

    Hallo Antonio,
    thanks to a friend of mine I've been able to find the culprit wich resides in the script posted above.
    It seems that if I just change the text of the TableCell wich contains the checbox (after reading is value of course) to my label, somehow this is stored in the control state. So when retrieving the grid from the control state (like in situation where a rebind doesn't happen) I have no more checkbox to check and thus I always get "false" from them.
    The trick was to HIDE the checkbox and add a Literal control, instead of replacing the content of the table cell. This way:

    ''' <summary>
    ''' Replace boolean checkbox columns in the grid with labels
    ''' </summary>
    ''' <param name="item">The actual table cell containing the bool checkbox</param>
    ''' <param name="trueLabel">Label bootstrap css class if value is true (success, warning, other)</param>
    ''' <param name="trueString">String to display instead of "true"</param>
    ''' <param name="falseLabel">Label bootstrap css class if value is false (important, warning, other)</param>
    ''' <param name="falseString">String to display instead of "false"</param>
    ''' <remarks></remarks>
    Public Shared Sub BooleanDisplay(ByRef item As TableCell, trueLabel As String, trueString As String, falseLabel As String, falseString As String)
        Dim check As CheckBox = DirectCast(item.Controls(0), CheckBox)
        Dim value As Boolean = check.Checked
        check.Visible = False
     
        Dim labelLiteral As New Literal
        If value Then
            labelLiteral.Text = "<span class=""label label-" & trueLabel & " autowidth"">" & trueString & "</span>"
        Else
            labelLiteral.Text = "<span class=""label label-" & falseLabel & " autowidth"">" & falseString & "</span>"
        End If
     
        item.Controls.Add(labelLiteral)
    End Sub
     

    Just in case someone needs something similar (css classes are from Bootstrap labels)
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017