This is a migrated thread and some comments may be shown as answers.

GridTemplateColumn Checkbox reposted as checked Incorrectly

4 Answers 120 Views
Grid
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 01 Sep 2015, 09:19 AM

This is one strange bug. I have a simple RadGrid with a couple of columns, I use a GridTemplateColumn with a checkbox control for deletion, heres the markup.

 

<telerik:GridTemplateColumn ItemStyle-HorizontalAlign="Center" UniqueName="chkItem" ItemStyle-Width="1%">
                         <HeaderTemplate>
                             <asp:CheckBox runat="server" onclick="$('.chk-locations2 input').prop('checked', this.checked).trigger('change');" ID="chkSelectAll" />
                         </HeaderTemplate>
                         <ItemTemplate>
                             <asp:CheckBox CssClass="chk chk-locations2" data-value='<%#Eval("LocationID")%>' text='<%#Eval("LocationID")%>'
                                 runat="server" ID="chkItem" Checked="false" />
                         </ItemTemplate>
                     </telerik:GridTemplateColumn>

Anyway, I have a button that does a full postback that checks for .Checked checkboxes and executes the delete operation, like this.

 

For Each Item As Telerik.Web.UI.GridItem In Input.Items
            If Item.ItemType = GridItemType.Item Or Item.ItemType = GridItemType.AlternatingItem Then
                Dim Chk As CheckBox = CType(Item.FindControl("chkItem"), CheckBox)
                If Chk.Checked Then
                    Dim ID As String = Chk.Text
                    If IsNumeric(ID) Then
                        CallBack.Invoke(ID, Argument)
                    End If
                End If
 
            End If
        Next

All works as expected, only the checked rows are deleted and since I reload the grid again, no items are checked once the page has reloaded, it all looks ok until you refresh the page again. If your grid had say 10 items and you ticked the first item and deleted it, as expected the grid now would have 9 items. If you refresh the page, a full postback is done as expected however, the first item is marked as "Checked" when it was not, so the item is then deleted and how you have 8 items despite the fact that you had not explicitly checked any item.

 This seems to be a bug in the radgrid, the checkbox is shown as Checked when nobody checked it. The only solution I can see for now is to prevent a postback with Javascript by check for the no. of checked checkboxes.

 

4 Answers, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 02 Sep 2015, 01:26 PM
Hi John,

Since you have custom logic for deleting your items with the GridTemplateColumn and an external button, it is not likely that the issue is related to the RadGrid. With the provided information only, the only thing that I could suggest is that you ensure to rebind your RadGrid (by setting an empty object as a DataSource and call the Rebind method) once you delete the items from your database.

Hope this helps.


Regards,
Konstantin Dikov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
John
Top achievements
Rank 1
answered on 02 Sep 2015, 03:19 PM

Thanks for your reply Konstantin. I have tried it all, setting the dataSource to null + rebind, force unchecking each and every checkbox using javascript, enabling/disabling viewstate nothing works. It happens on a full postback, for some reason refreshing the page causes the button click event to fire again and while that is expected, what is not is that a row in the Grid will be detected as checked.

 Firefox luckily shows a warning that the form will be submitted again, the chrome version I am on (47.0.2498.0 dev-m (64-bit)), does not show the warning so it looks like a standard page refresh. Either way, there is no checkbox ticked on the client side and therefore when looping through the grid items on postback, they should all be un-checked.

0
Accepted
Konstantin Dikov
Telerik team
answered on 04 Sep 2015, 12:27 PM
Hello John,

With the additional information that you have provided I was able to replicate the problem, but I have to say that this is not related to RadGrid in any way and you can replicate the same issue with the following simple example:
<asp:Label ID="lbltest" runat="server"></asp:Label>
 
<asp:Button runat="server" Text="Get checked state" ID="Button1" OnClick="Button1_Click"></asp:Button>
<asp:CheckBox CssClass="chk chk-locations2"
    runat="server" ID="chkItem" Checked="false" />

And the code-behind:
protected void Button1_Click(object sender, EventArgs e)
{
    lbltest.Text = "Status: " + chkItem.Checked + ", " + DateTime.Now.ToString();
    chkItem.Checked = false;
}

An easy workaround for this behavior is to enable AJAX on your page by wrapping the controls within a RadAjaxPanel for example.

Other solutions could be found in the following forum thread:

Regards,
Konstantin Dikov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
John
Top achievements
Rank 1
answered on 09 Sep 2015, 07:07 AM

So the issue is with asp.net itself, interesting. I'll try out wrapping it all in an ajax control vs, the response.redirect route.

Thanks

Tags
Grid
Asked by
John
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
John
Top achievements
Rank 1
Share this question
or