Select all + persist selection + multipage

Thread is closed for posting
2 posts, 0 answers
  1. Marnix Bouwman
    Marnix Bouwman avatar
    22 posts
    Member since:
    Nov 2009

    Posted 01 Dec 2009 Link to this post

    Hi I have this radgrid showing 10 items per page where I want to have a column with checkboxes.
    When I check some checkboxes on page 1, I want to go to page 4 and select some more checkboxes.
    And then go back to page 1 and the selected checkboxes should still be selected.
    I have this working.

    Now I want a select all checkbox which selects, well.... All records... I got it so far so it will select all records on the current page, but not on the other pages.

    I've set it up like this:

    <telerik:radgrid id="RadUsers" allowmultirowselection="true" showfooter="True" runat="server" showstatusbar="True" onneeddatasource="RadUsers_NeedDataSource" gridlines="None" allowpaging="True" pagesize="10" skin="Vista" allowfilteringbycolumn="True" autogeneratecolumns="False" >
        <mastertableview showfooter="True" datakeynames="UserID"  >
            <columns>
                <telerik:gridtemplatecolumn allowfiltering="false">
                    <headertemplate>
                        <asp:checkbox id="headerChkbox" oncheckedchanged="ToggleSelectedState" autopostback="True" runat="server"></asp:checkbox>
                    </headertemplate>
                    <itemtemplate>
                        <asp:checkbox id="cbChecked" runat="server" autopostback="true" oncheckedchanged="OnCheckedChangedEvent" />
                    </itemtemplate>
                </telerik:gridtemplatecolumn>
    ...

    And:

            Private ReadOnly Property UsersChecked() As Hashtable
                Get
                    Dim res As Object = ViewState("_cc")
                    If (res Is Nothing) Then
                        res = New Hashtable
                        ViewState("_cc") = res
                    End If
                    Return CType(res, Hashtable)
                End Get
            End Property

    ....

            Protected Sub RadUsers_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadUsers.ItemDataBound
                If TypeOf e.Item Is GridDataItem Then
                    Dim item As GridDataItem = CType(e.Item, GridDataItem)
                    Dim box As CheckBox = CType(item.FindControl("cbChecked"), CheckBox)
                    Dim isChecked As Object = Nothing

                    isChecked = UsersChecked(item("UserID").Text)

                    If Not (isChecked Is Nothing) Then
                        box.Checked = CType(isChecked, Boolean) = True
                    End If
                End If
            End Sub


            Protected Sub ToggleSelectedState(ByVal sender As Object, ByVal e As EventArgs)
                Dim target As Hashtable = Nothing
                target = UsersChecked

                If (CType(sender, CheckBox)).Checked Then
                    For Each dataItem As GridDataItem In RadUsers.MasterTableView.Items
                        CType(dataItem.FindControl("cbChecked"), CheckBox).Checked = True
                        'dataItem.Selected = True
                        target(dataItem("UserID").Text) = True
                    Next
                Else
                    For Each dataItem As GridDataItem In RadUsers.MasterTableView.Items
                        CType(dataItem.FindControl("cbChecked"), CheckBox).Checked = False
                        'dataItem.Selected = False
                        target(dataItem("UserID").Text) = Nothing
                    Next
                End If
            End Sub


            Sub OnCheckedChangedEvent(ByVal sender As Object, ByVal e As System.EventArgs)
                Dim box As CheckBox = CType(sender, CheckBox)
                Dim item As GridDataItem = CType(box.NamingContainer, GridDataItem)
                Dim target As Hashtable = Nothing

                target = UsersChecked

                If box.Checked Then
                    target(item("UserID").Text) = True
                Else
                    target(item("UserID").Text) = Nothing
                End If
            End Sub




  2. Sebastian
    Admin
    Sebastian avatar
    9934 posts

    Posted 03 Dec 2009 Link to this post

    Hello Marnix,

    You may consider the client-side approach with 'SelectAll' option in built-in GridClientSelectColumn demonstrated in this topic from the online documentation. Thus you should attain the desired result.

    Best regards,
    Sebastian
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top