RadGrid: Case-Insensitive LINQ Filter using Web Service

4 posts, 0 answers
  1. Kevin
    Kevin avatar
    27 posts
    Member since:
    Aug 2012

    Posted 12 Jan 2012 Link to this post

    I have a grid, bound declaratively to a PageMethod web service, using LINQ filtering with System.Linq.Dynamic.
    Some of my settings are as follows:

    <GroupingSettings CaseSensitive="false" />
    <ClientSettings>
    <DataBinding SelectMethod="GetAllOffices" Location="UserDetails.aspx" EnableCaching="true" FilterParameterType="Linq" />
    <Selecting AllowRowSelect="true" UseClientSelectColumnOnly="true" />
    </ClientSettings>
    <MasterTableView>

    I understand that <GroupingSettings> should be used for case-insensitive filtering, but that is not holding true when using client-side LINQ binding.  Fiddler shows the dynamic LINQ statement being returned to server, as JSON, to be:  "filterExpression":"OfficeName.Contains(\"name\") AND OfficeCode.Contains(\"c\")".

    How do you get case-insensitivity when using a LINQ filter, bound declaratively, to a web service?

    Thank you,
    Kevin Kalitowski 
  2. The Oracle
    The Oracle avatar
    46 posts
    Member since:
    Aug 2012

    Posted 28 Aug 2012 Link to this post

    Can someone answer this question?  I have the same issue.
    Thank you much!
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Kevin
    Kevin avatar
    27 posts
    Member since:
    Aug 2012

    Posted 29 Aug 2012 Link to this post

    Hey Oracle, this is the solution I have for the problem.

    These are my relevant ClientSettings in the RadGrid:
    <ClientSettings>
        <DataBinding SelectMethod="GetAllOffices" Location="UserDetails.aspx" FilterParameterType="Linq" />
        <ClientEvents OnDataBinding="OfficeGridDataBinding" />
    </ClientSettings>

    This is the OnDataBinding function.  I also included two lines for sending an extra parameter to the web method as I thought you might find that as useful as I did.
    function OfficeGridDataBinding(sender, e) {
        //Adds an additional parameter to the JSON web service call
        var methodArguments = e.get_methodArguments();
        methodArguments.previouslySelectedOfficeIds = $('#previouslySelectedOfficesHiddenField').val();
        MakeSearchCaseInsensitive(sender, e);
    }

    And here is the magic.  In summary, it uses JavaScript regular expressions to add a ".ToLower()" string in the appropriate places which is interpreted by dynamic LINQ:
    function MakeSearchCaseInsensitive(sender, args) {
        var master = sender.get_masterTableView();
        if (master.get_filterExpressions().get_count()) {
            //get the filter expression string that will be sent to your web service
            var filterExpression = args.get_methodArguments().filterExpression;
     
            for (var i = 0; i < master.get_filterExpressions().get_count(); i++) {
                //for each filter expression item in the collection, modify the above
                //filter expression if the filter data type is System.String
                var expr = master.get_filterExpressions().getItem(i);
                if (expr.get_dataTypeName() === "System.String") {
                    filterExpression = filterExpression.replace(new RegExp(expr.get_fieldName(), "gim"), expr.get_fieldName() + ".ToLower()");
                    filterExpression = filterExpression.replace(new RegExp('"' + expr.get_fieldValue() + '"', "gim"), '"' + expr.get_fieldValue() + '".ToLower()');
     
                    //set the modified filter expression back in the method arguments
                    args.get_methodArguments().filterExpression = filterExpression;
                }
            }
        }
    }

    Good luck!
  5. The Oracle
    The Oracle avatar
    46 posts
    Member since:
    Aug 2012

    Posted 30 Aug 2012 Link to this post

    Dude, thanks for this.  It seems like 95% of dev time is spent trying to get controls to work the way they are purported to or to work in "unanticipated" scenarios. LOL.  telerik still has a great toolset.
    Graeme
Back to Top