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

Problem with grid ItemCommands

2 Answers 97 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Johan
Top achievements
Rank 1
Johan asked on 12 Jan 2018, 12:25 PM

Hello,

for our website i've built some functionality into our basepage that creates a commandrow with a 'ClearAllFilters' button for every grid.

At first glance this seems to work fine. However, when selecting an item in a grid after a postback (for example filtering, or paging) it is selecting not from the filtered source, but the unfiltered source. So visually the grid is filtered, but when selecting an item it is using the complete list.

 

Somehow i have the feeling that the adding of a handler for the itemcommand is overwriting certain functionality that is otherwise called during the itemcommand event. However i have not been able to fix the problem, and ive tried tons of things.

Please have a look at the code and let me know if i am doing something wrong here!

 

Public Class BasePage
        Inherits Page
 
        Private ReadOnly Property AllowedFilterItems As String() = New String() {"Contains", "StartsWith", "EqualTo", "NotEqualTo"}
 
        Protected Overrides Sub OnInit(e As EventArgs)
            For Each grid As RadGrid In Controls.All().OfType(Of RadGrid)()
                DefineGridStructure(grid)
            Next
            MyBase.OnInit(e)
        End Sub
 
        Private Sub DefineGridStructure(grid As RadGrid)
            AddHandler grid.Load, New EventHandler(AddressOf Grid_OnLoad)
            AddHandler grid.ItemCommand, New GridCommandEventHandler(AddressOf Grid_OnCommand)
            AddHandler grid.PreRender, New EventHandler(AddressOf Grid_OnPreRender)
            AddCommandRow(grid)
        End Sub
 
        Protected Sub BasePage_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
            SetupRadgridPaging()
        End Sub
 
#Region "Radgrid event handlers and helper functions"
        Public Sub SetupRadgridPaging()
            For Each grid As RadGrid In Controls.All().OfType(Of RadGrid)()
                If grid.MasterTableView.DetailTables.Count = 0 AndAlso (grid.AllowPaging OrElse grid.MasterTableView.AllowPaging) Then
                    AddHandler grid.PageSizeChanged, AddressOf Grid_OnPageSizeChanged
                    Dim pageSize = GetPagerSizeForUser(GetGridPagerSettingsKey(grid.ID))
 
                    UpdateNewPageSize = False
                    grid.PageSize = pageSize
 
                    grid.PagerStyle.AlwaysVisible = True
                    grid.AllowPaging = False
 
                    grid.MasterTableView.PageSize = pageSize
                    grid.MasterTableView.PagerStyle.AlwaysVisible = True
                    grid.MasterTableView.AllowPaging = True
 
                    UpdateNewPageSize = True
                End If
            Next
        End Sub
 
        Private Sub Grid_OnCommand(sender As Object, e As GridCommandEventArgs)
            Dim grid As RadGrid = CType(sender, RadGrid)
            If e.CommandName.Equals("ClearAllFilters", StringComparison.InvariantCultureIgnoreCase) Then
                For Each column As GridColumn In grid.Columns
                    column.CurrentFilterFunction = GridKnownFunction.NoFilter
                    column.CurrentFilterValue = String.Empty
                Next
 
                grid.MasterTableView.FilterExpression = String.Empty
                grid.Rebind()
            End If
        End Sub
 
        Private Sub Grid_OnLoad(sender As Object, e As EventArgs)
            Dim grid As RadGrid = CType(sender, RadGrid)
            ConfigureFilterMenu(grid)
        End Sub
 
        Private Sub Grid_OnPreRender(sender As Object, e As EventArgs)
            Dim grid As RadGrid = CType(sender, RadGrid)
            SetCommandItemVisibility(grid)
        End Sub
 
        Private Sub SetCommandItemVisibility(grid As RadGrid)
            Dim hasFilter As Boolean = False
            For Each column As GridColumn In grid.Columns
                If Not column.CurrentFilterValue = String.Empty Then
                    hasFilter = True
                    Exit For
                End If
            Next
 
            grid.MasterTableView.CommandItemDisplay = If(hasFilter, GridCommandItemDisplay.Top, GridCommandItemDisplay.None)
            grid.Rebind()
        End Sub
 
        Private Sub AddCommandRow(grid As RadGrid)
            grid.MasterTableView.CommandItemTemplate = New GridCommandItemTemplate(CType(LocalizationManager, LocalizationManager))
        End Sub
 
        Private Sub ConfigureFilterMenu(grid As RadGrid)
            grid.FilterMenu.Items().ToList().ForEach(Sub(fi) grid.FilterMenu.Items.Remove(fi))
            AllowedFilterItems.ForEach(Sub(fi) grid.FilterMenu.Items.Add(New RadMenuItem(fi) With {.Value = fi}))
            grid.FilterMenu.Localize(App.Session.CurrentCultureIdentifier)
        End Sub
 
        Public Sub Grid_OnPageSizeChanged(sender As Object, e As GridPageSizeChangedEventArgs)
            Dim grid As RadGrid = CType(sender, RadGrid)
            UpdateUserSetting(GetGridPagerSettingsKey(grid.ID), e.NewPageSize.ToString())
        End Sub
 
        Public Function GetGridPagerSettingsKey(grid As String) As String
            Const prefix As String = "CPGridPager"
            Dim sPath As String = Request.Url.AbsolutePath
            Dim oInfo As New FileInfo(sPath)
            Dim pageName As String = Path.GetFileNameWithoutExtension(oInfo.Name)
            Return String.Concat(prefix, separator, pageName, separator, grid)
        End Function
 
#Region "GridCommandItemTemplate"
        Private Class GridCommandItemTemplate
            Implements ITemplate
 
            Private clearFilter As ImageButton
            Private _localizationManager As LocalizationManager
 
            Public Sub New(ByRef localizationManager As LocalizationManager)
                MyBase.New
                _localizationManager = localizationManager
            End Sub
 
            Public Sub InstantiateIn(container As Control) Implements ITemplate.InstantiateIn
                clearFilter = New ImageButton With {
                    .ID = "clearFilter",
                    .ToolTip = _localizationManager.Localize("Grid_ClearFilters", App.Session.CurrentCultureIdentifier),
                    .CommandName = "ClearAllFilters",
                    .ImageUrl = "/Images/Grid/ClearFilter.png"
                }
 
                container.Controls.Add(clearFilter)
            End Sub
        End Class
#End Region
#End Region

2 Answers, 1 is accepted

Sort by
0
Johan
Top achievements
Rank 1
answered on 15 Jan 2018, 05:07 PM
So i did find out that the OnNeedDataSource event keeps firing before the ItemCommand when filtering or paging, likely resetting the datasource before the itemcommand takes place. The grids viewstate is enabled, what else could be causing this behavior? Im at a loss here!
0
Johan
Top achievements
Rank 1
answered on 16 Jan 2018, 02:00 PM

Ive been trying a clientside solution with a javascript function, it works in the sense that it removes the text, but then it refreshes the page and the text is there again. Is this solution even feasible with server side databinding

 

function clearallfilters(sender) {
    var gridid = sender.dataset.gridid
    var masterTable = $find(gridid).get_masterTableView()
    masterTable.get_filterExpressions().clear();   
 
    var cols = $find(gridid).get_masterTableView().get_columns();
    for (var i = 0; i < cols.length; i++) {       
        cols[i].set_filterFunction("NoFilter");
    }
 
    var filterboxes = $telerik.$($find(gridid).get_element()).find(".rgFilterBox");
    for (var i = 0; i < filterboxes.length; i++) {
        filterboxes[i].value = "";
    }
 
    masterTable.rebind();
    return false;
}
Tags
Grid
Asked by
Johan
Top achievements
Rank 1
Answers by
Johan
Top achievements
Rank 1
Share this question
or