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
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