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

[Solved] Grid filter text with less than or greater than sign

3 Answers 794 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Arsi Halttunen
Top achievements
Rank 2
Arsi Halttunen asked on 11 Aug 2009, 09:54 AM
Hi,

I have grid with filtering enabled. Filtering seems to be working well except when I use '>' or '<' chars in filter box.

When I type for example "> 50%" and select "contains" from the menu it returns all the rows in datasource. It seems that  RadGrid.MasterTableView.FilterExpression is allways empty when using less than or greater than signs.

Also when I try to access the filter textbox on server-side(see below) it's empty when using those chars.
            GridFilteringItem filterItem = (GridFilteringItem)grid1.MasterTableView.GetItems(GridItemType.FilteringItem)[0]; 
            TextBox txtFilter = (TextBox)filterItem["answer"].Controls[0]; 
            string searchFor = txtFilter.Text; 



However the filter value is available in __EVENTARGUMENT hidden field.
  • FireCommand:grid1$ctl00;Filter;answer|> 50%|Contains


So my question is:
Why FilterExpression is empty and how can easily fix filtering with less than and greater than signs?




By the way.. Using single apostrophe causes filtering not to work and javascript error because string is not escaped in postback function.
__doPostBack('grid1','FireCommand:grid1$ctl00;Filter;answer|abc's|Contains') 


I'm using version 2008.2.826.35.


Regards,
Arsi

3 Answers, 1 is accepted

Sort by
0
Mira
Telerik team
answered on 12 Aug 2009, 12:05 PM
Hello Arsi,

Have you seen Filter grid by any column that contains specific text code library?

Please let me know whether it helps.

All the best,
Mira
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Arsi Halttunen
Top achievements
Rank 2
answered on 13 Aug 2009, 07:43 AM
Hello Mira,

Thank you for your help. It didn't fix the problem but was helpful.

I got my filtering working anyway. My solution is not the most elegant but it works with characters that I need. See below.

 
        protected void RadGrid1_PreRender(object sender, EventArgs e) 
        { 
            string theArgs = string.Empty; 
            string strFilter = string.Empty; 
            string filterFunc = string.Empty; 
 
            if (Request["__EVENTARGUMENT"] != null
                theArgs = Request["__EVENTARGUMENT"].ToString(); 
 
            // Get filter string from args because MasterTableView.FilterExpression is empty when using '<' or '>'. 
            strFilter = ResolveFilterString(theArgs); 
            // Get Selected filterFunction from event arguments because columns CurrentFilterFunction is empty when using '<' or '>'. 
            filterFunc = ResolveFilterFunction(theArgs); 
 
            // Check if string contains less than or greater than sign. 
            if (strFilter.Contains("<") || strFilter.Contains(">")){ 
 
                string strExpression = string.Empty; 
                switch (filterFunc){ 
                    case "Contains"
                        strExpression = "(it[\"answer\"].ToString().Contains(\"" + strFilter + "\"))"
                        break
                    case "DoesNotContain"
                        strExpression = "(!it[\"answer\"].ToString().Contains(\"" + strFilter + "\"))"
                        break
                    case "EqualTo"
                        strExpression = "(Convert.ToString(it[\"answer\"]) = \"" + strFilter + "\")"
                        break
                    case "StartsWith"
                        strExpression = "(it[\"answer\"].ToString().StartsWith(\"" + strFilter + "\"))"
                        break
                        // etc.. 
                } 
                grid1.MasterTableView.FilterExpression = strExpression; 
                grid1.MasterTableView.Rebind(); 
            } 
        } 
 
        /// <summary> 
        /// Separates filter string from event args 
        /// </summary> 
        /// <param name="theArgs">Value of EVENTARGUMENT hidden field</param> 
        /// <returns>Filter value as string</returns> 
        private string ResolveFilterString(string theArgs) 
        { 
            int firstPipe = 0; 
            int lastPipe = 0; 
            string resStr = string.Empty; 
 
            firstPipe = theArgs.IndexOf("|"); 
 
            // Return if pipe not found 
            if (firstPipe == 0 || theArgs.Length < 1) 
                return ""
 
            resStr = theArgs.Substring(firstPipe + 1); 
 
            lastPipe = resStr.IndexOf("|"); 
 
            if (lastPipe < 1) 
                return ""
 
            return resStr.Substring(0, lastPipe); 
        } 
 
        /// <summary> 
        /// Separates filter functions name from event arguments 
        /// </summary> 
        /// <param name="theArgs">Value of EVENTARGUMENT hidden field</param> 
        /// <returns>Filter function name as string</returns> 
        private string ResolveFilterFunction(string theArgs) 
        { 
            int lastPipe = 0; 
            string resStr = string.Empty; 
 
            lastPipe = theArgs.LastIndexOf("|"); 
 
            if (lastPipe == 0) 
                return "NoFilter"
 
            return theArgs.Substring(lastPipe + 1, theArgs.Length - lastPipe - 1); 
        } 


Regards,
Arsi
0
Mira
Telerik team
answered on 18 Aug 2009, 02:09 PM
Hello Arsi,

I am glad to hear you managed to solve the faced problem. And your code seems fine to me.
Indeed the desired functionality is not that easy to be achieved and the other way is to use Custom filter template which will again require checking whether string contains less than or greater than sign.

All the best,
Mira
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Grid
Asked by
Arsi Halttunen
Top achievements
Rank 2
Answers by
Mira
Telerik team
Arsi Halttunen
Top achievements
Rank 2
Share this question
or