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

Implementing a custom filter

3 Answers 196 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 1
Mike asked on 28 May 2010, 04:42 PM
I'm having a bit of difficulty figuring out how to implement a custom filter for the grid. I'm using advanced data binding and my filtering parameters are implemented by separate HTML controls not part of RadGrid. Essentially what I want to do is define the FilterExpression value client-side and make sure that it's being passed and available each time server-side during _NeedDataSource.

If I do something like this client-side:

            var grid = $find("MessageGrid");
            var MasterTable = grid.get_masterTableView();
            MasterTable.filter("LogTime", 10254, Telerik.Web.UI.GridFilterFunction.GreaterThan, true);            

during _NeedDataSource, MessageGrid.MasterTableView.FilterExpression is empty. 

In general, how do I set a parameter value on the grid such that it is persisted in the View State so that it is also available, for example, during Paging, when _NeedDataSource will be called automatically?

Here's my client-side definition of the grid. I'm binding the columns server-side during Page_Init:

            <telerik:RadGrid ID="MessageGrid" runat="server" ShowStatusBar="true" EnableViewState="false" AllowSorting="true" AllowMultiRowSelection="true" AllowCustomPaging="true" AllowPaging="True" AutoGenerateColumns="False" GridLines="None" OnPageSizeChanged="MessageGrid_PageSizeChanged" OnNeedDataSource="MessageGrid_NeedDataSource" > 
                <MasterTableView ClientDataKeyNames="Id, ReferenceId"
                </MasterTableView> 
                <ClientSettings AllowColumnsReorder="true" ReorderColumnsOnClient="true"
                    <Resizing AllowColumnResize="true" /> 
                    <Scrolling AllowScroll="true" UseStaticHeaders="true" /> 
                    <ClientEvents OnRowSelected="MessageGrid_OnRowSelected" OnRowDblClick="MessageGrid_OnDblClick" OnGridCreated="MessaegGrid_Created" /> 
                    <Selecting AllowRowSelect="True" /> 
                </ClientSettings> 
            </telerik:RadGrid> 
            <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server"  OnAjaxRequest="RadAjaxManager1_AjaxRequest"
                <AjaxSettings> 
                    <telerik:AjaxSetting AjaxControlID="MessageGrid"
                        <UpdatedControls> 
                            <telerik:AjaxUpdatedControl ControlID="MessageGrid" /> 
                        </UpdatedControls> 
                    </telerik:AjaxSetting> 
                </AjaxSettings> 
            </telerik:RadAjaxManager> 
 

3 Answers, 1 is accepted

Sort by
0
Radoslav
Telerik team
answered on 03 Jun 2010, 08:02 AM
Hello Mike,

Following your scenario I created a simple example which calls MasterTable.filter() function when the button is pressed, however everything works as expected and into NeedDataSource event the RadGrid.MasterTableView.FilterExpression property contains a valid filter expression. I am sending you this simple application, please check  it out and let me know what differs in your case.

Also you could try adding your separate controls into the FilterTemplate of the RadGrid's columns. On the following online example you could find implementation of this approach:
http://demos.telerik.com/aspnet-ajax/grid/examples/programming/filtertemplate/defaultcs.aspx

Additionally to define the structure of a RadGrid control that is declared in the ASPX page, use the Page_Load event handler. For more information please check out the following online documentation article:
http://www.telerik.com/help/aspnet-ajax/grdprogrammaticcreation.html

I hope this helps.

All the best,
Radoslav
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
Mike
Top achievements
Rank 1
answered on 03 Jun 2010, 06:56 PM
Radoslav,

Thank you for your response. I did wind up trying something similar to what you posted. However, there were two problems:

- We have multiple filter parameters that can be set individually by the user. Try seeing what happens if, for example, you have two filter controls. You can't call MasterTable.filter() twice, since it fires a post-back immediately. I dug into the client-side internal MasterTable._filterExpressions array and was going to go that route to set multiple expressions client-side before the rebind() until I ran into the second problem.
- Our grid is paging. When the user clicked the next page, FilterExpression in NeedDataSource was blank.

Our solution was to just create a class derived from RadGrid and include another HiddenField control that  I use client-side to stuff a filtering parameter that I then parse server-side - in short, implementing filter expressions on my own since I couldn't guarantee that grid's filter expression would be set on paging.

In fact, there's a core problem with the grid's filter expressions and using them for your database query (instead of just using it to filter the results of the query on data bind). Telerik's documentation says that if you do so, you should clear out FilterExpression at the end of NeedDataSource. Well, if you clear it out, it's not available again until it's re-created by a user action (but not re-created automatically when the user pages).

Another aside - I wound up having to create the entire grid in the code-behind, using a asp.net place holder in the *.aspx. The reason for this is that if you want to do custom sorting and paging, one expects programmatic creation during _Init and the other expects it during _Load. The help says that in such cases you should create the entire grid in the code-behind - when there's an ASP.NET conflict like this. Let me suggest that this is pretty inflexible ;)

Thanks again,

- Mike
0
Radoslav
Telerik team
answered on 09 Jun 2010, 08:12 AM
Hi Mike,

You could not use client-side filter() function to filter on multiple column with multiple filter parameters. However  you could try passing the value to the server through an ajax request. Please check out the following code library example which demonstrates a similar approach:
http://www.telerik.com/community/code-library/aspnet-ajax/grid/multi-selection-radcombobox-for-filtering-grid.aspx

Also note that we could not support the scenarios in which the RadGrid is inherited.

Regarding your last issue considering custom paging and sorting:
Declaring the RadGrid in code behind is not the only one option when you use custom paging. Please check out the following online example which demonstrates the RadGrid with custom paging, bound to the ObjectDataSource:
http://demos.telerik.com/aspnet-ajax/grid/examples/programming/custompaging/defaultcs.aspx

Best wishes,
Radoslav
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
Mike
Top achievements
Rank 1
Answers by
Radoslav
Telerik team
Mike
Top achievements
Rank 1
Share this question
or