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

grid with checkboxes & select all

3 Answers 64 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Marnix Bouwman
Top achievements
Rank 2
Marnix Bouwman asked on 15 Apr 2010, 01:46 PM
Hi, I made this grid with checkboxes and select all function, and it basically works.
Only the grid has an option to allow filtering on columns which ruins my selection.
(when I click select all, the selection (of user id's) is saved into an arraylist which I'm putting in a session variable.
however when I do something like lastname contains "blabla" I don't know how I would filter this.

<telerik:radgrid id="RadUsers"
                allowmultirowselection="true"
                showfooter="True"
                runat="server"
                showstatusbar="True"
                onneeddatasource="RadUsers_NeedDataSource"
                onpageindexchanged ="RadUsers_PageIndexChanged"
                gridlines="None"
                allowpaging="True"
                pagesize="50"
                skin="Vista"
                allowfilteringbycolumn="True"
                autogeneratecolumns="False">
                <groupingsettings casesensitive="false" />
                <mastertableview showfooter="True" datakeynames="UserID" allowsorting="true">
                    <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>
                        <telerik:gridtemplatecolumn allowfiltering="false">
                            <itemtemplate>
                                <asp:imagebutton id="ibEditUser" runat="server" imageurl="~/images/edit.gif" alternatetext="Edit" commandargument='<%# Eval("UserID") %>' commandname="Bewerk" causesvalidation="false" onmouseover="this.style.cursor = 'hand';" />
                            </itemtemplate>
                        </telerik:gridtemplatecolumn>
                        <telerik:gridtemplatecolumn allowfiltering="false">
                            <itemtemplate>
                                <asp:imagebutton id="ibDelete" imageurl="~/images/delete.gif" runat="server" alternatetext="Delete" commandargument='<%# Eval("UserID") %>' commandname="Delete" causesvalidation="false" onmouseover="this.style.cursor = 'hand';" />
                            </itemtemplate>
                        </telerik:gridtemplatecolumn>
                        <telerik:gridboundcolumn datafield="UserID" headertext="UserID" sortexpression="UserID" uniquename="UserID" allowfiltering="false" allowsorting="false">
                        </telerik:gridboundcolumn>
                        <telerik:gridboundcolumn allowsorting="true" datafield="Lastname" headertext="Lastname" sortexpression="Lastname" uniquename="Lastname">
                        </telerik:gridboundcolumn>
                        <telerik:gridboundcolumn allowsorting="true" datafield="Firstname" headertext="Firstname" sortexpression="Firstname" uniquename="Firstname">
                        </telerik:gridboundcolumn>
                        <telerik:gridboundcolumn allowsorting="true" datafield="Email" headertext="Email" sortexpression="Email" uniquename="Email">
                        </telerik:gridboundcolumn>
                        <telerik:gridboundcolumn allowsorting="true" groupable="true" datafield="Country" headertext="Country" sortexpression="Country" uniquename="Country">
                        </telerik:gridboundcolumn>
                        <telerik:gridboundcolumn allowsorting="true" groupable="true" datafield="UserType" headertext="UserType" sortexpression="UserType" uniquename="UserType">
                        </telerik:gridboundcolumn>
                        <telerik:gridcheckboxcolumn datafield="Authorised" headertext="Authorised" sortexpression="Authorised" uniquename="Authorised" datatype="System.Boolean">
                        </telerik:gridcheckboxcolumn>
                    </columns>
                </mastertableview>
            </telerik:radgrid>
<asp:label id="lblTest" runat="server" />

And then:

#Region "Variables e.d."
        Dim arlCheckedItems As New arraylist
        Dim allChecked As String = "False"
#End Region

#Region "Page_Init"
        Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
            Me.ModuleConfiguration.IconFile = "~/desktopmodules/eapc/images/users32.gif"
            BindRoles()
            lblTest.Text = ""
            If Not Page.IsPostBack Then
                Session.Remove("arlCheckedItems")
                Session.Remove("allChecked")
            Else
                If Session("arlCheckedItems") IsNot Nothing Then
                    arlCheckedItems = Session("arlCheckedItems")
                End If
                If Session("allChecked") IsNot Nothing Then
                    allChecked = Session("allChecked")
                End If
            End If
        End Sub
#End Region

#Region "RadUsers_NeedDataSource"
        Sub RadUsers_NeedDataSource(ByVal [source] As Object, ByVal e As GridNeedDataSourceEventArgs)
            Dim objUserController As New MBUserController
            Dim lstUserInfo As List(Of MBUserInfo) = objUserController.GetUsers(PortalId, drpRoles.SelectedValue, drpBoard.SelectedValue, -1)

            RadUsers.DataSource = lstUserInfo
        End Sub
#End Region

#Region "RadUsers_ItemCommand"
        Protected Sub RadUsers_ItemCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadUsers.ItemCommand
            Select Case e.CommandName
                Case "Bewerk"
                    Dim formatString As String = EditUrl("uid", e.CommandArgument, "EditUser", "m=u")
                    Response.Redirect(formatString)
                Case RadGrid.FilterCommandName
                    'remove all selections
                    allChecked = "False"
                    Session("allChecked") = "False"
                    arlCheckedItems.Clear()
                    Session.Remove("arlCheckedItems")

                    RadUsers.Rebind()
            End Select
        End Sub
#End Region

#Region "RadUsers_PageIndexChanged"
        Sub RadUsers_PageIndexChanged(ByVal source As Object, ByVal e As Telerik.Web.UI.GridPageChangedEventArgs) Handles RadUsers.PageIndexChanged
            ShowIds()
        End Sub
#End Region

#Region "RadUsers_ItemDataBound"
        Protected Sub RadUsers_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadUsers.ItemDataBound
            If allChecked = "True" Then
                If TypeOf e.Item Is GridHeaderItem Then
                    Dim headerChkbox As CheckBox = DirectCast(e.Item.FindControl("headerChkbox"), checkbox)
                    headerChkbox.checked = True
                End If
            End If
            If TypeOf e.Item Is GridDataItem Then

                Dim deletebutton As ImageButton = CType(e.Item.FindControl("ibDelete"), ImageButton)
                If Not deletebutton Is Nothing Then
                    deletebutton.Attributes.Add("onClick", "javascript:return confirm('Are you sure?');")
                End If

                Dim objEAPCUserInfo As EapcUserInfo = CType(e.Item.DataItem, EapcUserInfo)
                If Not objEAPCUserInfo Is Nothing Then
                    If objEAPCUserInfo.UserID = UserId Then
                        deletebutton.Visible = False
                    End If
                End If

                Dim ditem As GridDataItem = e.Item
                If arlCheckedItems.contains(ditem("UserID").Text.ToString) Then
                    Dim chk As CheckBox = DirectCast(ditem.FindControl("cbChecked"), checkbox)
                    chk.checked = True
                End If
            End If
        End Sub
#End Region

#Region "ToggleSelectedState"
        Protected Sub ToggleSelectedState(ByVal sender As Object, ByVal e As EventArgs)
            If (CType(sender, CheckBox)).Checked Then
                'select all
                allChecked = "True"
                Session("allChecked") = "True"
                Dim objUserController As New MBUserController
                Dim lstUserInfo As List(Of MBUserInfo) = objUserController.GetUsers(PortalId, drpRoles.SelectedValue, drpBoard.SelectedValue, -1)
                Dim objUserInfo As MBUserInfo
                For Each objUserInfo In lstUserInfo
                    arlCheckedItems.add(objUserInfo.UserID.ToString)
                Next

                Session("arlCheckedItems") = arlCheckedItems
            Else
                'deselect all
                allChecked = "False"
                Session("allChecked") = "False"
                arlCheckedItems.Clear()
                Session.Remove("arlCheckedItems")
            End If

            RadUsers.Rebind()

            ShowIds()
        End Sub
#End Region
#Region "OnCheckedChangedEvent"
        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)

            If box.Checked Then
                arlCheckedItems.add(item("UserID").Text.ToString)
                Session("arlCheckedItems") = arlCheckedItems
            Else
                arlCheckedItems.remove(item("UserID").Text.ToString)
                Session("arlCheckedItems") = arlCheckedItems

                If allChecked = "True" Then
                    allChecked = "False"
                    Session("allChecked") = "False"
                    RadUsers.Rebind()
                End If
            End If


            ShowIds()
        End Sub
#End Region
#Region "RadUsers_DeleteCommand"
        Protected Sub RadUsers_DeleteCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadUsers.DeleteCommand
            Try
                Dim userId As Integer = Int32.Parse(e.CommandArgument.ToString)

                Dim user As UserInfo = UserController.GetUserById(PortalId, userId)

                If Not user Is Nothing Then
                    UserController.DeleteUser(user, True, False)
                    RadUsers.Rebind()
                End If

            Catch exc As Exception    'Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub
#End Region

#Region "ShowIds"
        Sub ShowIds()
            lblTest.Text = ""
            arlCheckedItems.sort()
            Dim Item As String
            For Each Item In arlCheckedItems
                lblTest.Text += Item.ToString & "<br />"
            Next
        End Sub
#End Region




3 Answers, 1 is accepted

Sort by
0
Pavlina
Telerik team
answered on 20 Apr 2010, 01:59 PM
Hello Marnix,

In order to persist the selection across filtering, paging etc., I suggest that you refer to the following help article, which elaborates on this subject:
Persisting the selected rows server-side on sorting/paging/filtering/grouping

Give it a try and let me know how it goes.

Kind regards,
Pavlina
the Telerik team

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 Public Issue Tracking system and vote to affect the priority of the items.
0
Marnix Bouwman
Top achievements
Rank 2
answered on 23 Apr 2010, 04:41 PM
I've seen it bfore I think, but it didn't do what I wanted.
When I click select all, I would like to select all, so also all on page 2, page 3, etc.
And I think that didn't work with this example.

So when you click select all, it should really select all the available records.
And then when you use the column filter, it should only keep the filtered items.
0
Pavlina
Telerik team
answered on 28 Apr 2010, 03:20 PM
Hi Marnix,

When paging is enabled in the grid, a solution would be to temporarily disable paging, loop through all the items in the grid, select the items and then enable paging back.

Give this suggestion  a try and let me know about the result.

Greetings,
Pavlina
the Telerik team

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 Public Issue Tracking system and vote to affect the priority of the items.
Tags
Grid
Asked by
Marnix Bouwman
Top achievements
Rank 2
Answers by
Pavlina
Telerik team
Marnix Bouwman
Top achievements
Rank 2
Share this question
or