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

Want to customize RadFilterDateFieldEditor and/or RadFilter in general

1 Answer 132 Views
Filter
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 06 May 2015, 01:05 AM

I'm currently using a RadFilter on a page that contains the following field editors:

 

<FieldEditors>
    <telerik:RadFilterDateFieldEditor DataType="System.DateTime" DisplayName="Date Created"
        FieldName="DateCreated" />
    <telerik:RadFilterTextFieldEditor DataType="System.DateTime" DisplayName="Date Last Modified"
        FieldName="DateModified" />
    <telerik:RadFilterNumericFieldEditor DisplayName="File Size (bytes)" FieldName="FileSizeBytes"
        DataType="System.Int32" PreviewDataFormat="'{0:#,##0}'" />
</FieldEditors>

 

I'm using the RadFilter in an unusual way, however.  I'm not using it against live data.  Instead, I'm using RadFilter to build a Linq "Where" clause that I save, and then I add that "Where" clause to a Linq query later.

For example, here is a Linq query created by the RadFilter, in combination with RadFilterDynamicLinqQueryProvider:

 

(it.DateCreated = DateTime.Parse("3/16/2015 12:00:00 AM") AND it.DateModified > DateTime.Parse("3/16/2015 12:00:00 AM") AND it.FileSizeBytes > 0)

 

It currently works perfectly.  What I would like to do, however, is to use a "dynamic date syntax" in place of an actual hard-coded date when entering the text into the RadFilterDateFieldEditor.  At a later time, my code will substitute this "dynamic date syntax" with an actual date, before running the actual Linq query.  So what I would like the RadFilter-generated Linq query to look like is this:

 

(it.DateCreated = DateTime.Parse("[MM/dd/yyyy hh:mm:ss PP]") AND it.DateModified > DateTime.Parse("[MM/dd/yyyy hh:mm:ss PP]") AND it.FileSizeBytes > 0)

 

I would then substitute everything within the brackets with the current date and time just before executing the query, so that it would look exactly like the first query example before executing.  (The exception being that the actual dates would be different, of course, and would vary depending on when the query was going to execute.)

When I try to enter my dynamic date syntax into the text box, however, it fails validation immediately because it contains non-date characters.  The warning symbol pops up inside the text box to show invalid input.

I tried to use a RadFilterTextFieldEditor instead of a RadFilterDateFieldEditor, and the validation was no longer an issue (no warning symbol popped up inside the text box).  When I attempted to save the settings and generate the Linq query, however, I got the following error:

 

System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at System.Convert.ToDateTime(String value, IFormatProvider provider)
   at System.String.System.IConvertible.ToDateTime(IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType)
   at Telerik.Web.UI.RadFilterNonGroupExpression.ParseValue[T](Object value)
   at Telerik.Web.UI.RadFilterSingleValueExpression`1.Telerik.Web.UI.IRadFilterValueExpression.SetValues(ArrayList values)
   at Telerik.Web.UI.RadFilter.FetchExpressionsValues(RadFilterGroupExpressionItem group)
   at Telerik.Web.UI.RadFilter.SaveSettings()

 

So it appears that the RadFilter code is parsing the value of the input when saving the settings, and of course the parse fails.  I would like it to succeed anyway, of course, so that I can substitute the actual date just before I run the Linq query.  I can always verify the user input using other methods, and throw an error if the input will not result in a valid generated date.

Changing the DataType from System.DateTime to System.String is not an option, because then the control uses string comparisons instead of date comparisons, and the Linq query changes completely.

I already have a dynamic date component that's fully functional.  It does a lot more than simply substitute the current date, however.  There are numerous possible output formats, as well as numerous possible calculations that can be applied on the date, and you can even run external scripts that return dates if the built-in functions don't meet your needs.  The choices are so many that I have an ASP.NET web server control that I'd like to use instead of a regular text box inside the RadFilterDateFieldEditor, to make it easy to choose the correct dynamic date syntax.  I'll be happy to worry about that later, however, if I can somehow get the filter to work the way that I want even with manually-typed dynamic date syntax.

I've been reading a bit about creating your own custom field editors.  I think that the SaveSettings() issue is outside of that, however.  I think that I would get the same DateTime.Parse() error even if I wrote my own custom field editor.  So I would like to fix the SaveSettings() issue first, before worrying about creating my own custom field editor.

I know it's an unusual application of the RadFilter, but any help getting things to work the way I'd like them to would be greatly appreciated.

Thank you.

 

1 Answer, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 08 May 2015, 02:13 PM
Hi Chris,

With DataType set to DateTime you will not be able to achieve the desired result.

However, you could try to build your filter expressions manually, as documented in the following help article:
I Hope that this approach will help you with the requirement.


Best Regards,
Konstantin Dikov
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
Tags
Filter
Asked by
Chris
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Share this question
or