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

validating properly in code behind

9 Answers 105 Views
DateTimePicker
This is a migrated thread and some comments may be shown as answers.
Brian
Top achievements
Rank 1
Brian asked on 07 Sep 2010, 05:29 PM
I am validating a date in code behind using ParseDateTime event and setting the date back to a previous value if the validation failed but the DateTimeText value of the control is remaining at the invalid date that was selected.

Here is my scenario:

- Active date and Inactive date on page both bound to properties in datacontext.
- Inactive date can be null but if it is set must be greater than active date.
The following code works for the most part.  this._myDataContext.InactiveDate gets the correct value, inactiveDatePicker.SelectedDate gets the correct value but the date control still shows the invalid date. I need inactiveDatePicker.DateTimeText to be set to the proper value (or should display the watermark if _previousInactiveDate is null).
 
void
 _InactiveDatePicker_ParseDateTimeValue(object sender, ParseDateTimeEventArgs args)
{
    if (!ValidateDates())
    {
        this._myDataContext.InactiveDate = _previousInactiveDate;
    }
}
















9 Answers, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 09 Sep 2010, 09:46 AM
Hi Brian,

First of all I would recommend you to use the SelectedValue instead of the SelectedDate. Also the whole  validation code must be set in the ParseDateTimeValue event. In case you want to set your own parsing logic you can put the value to the ParseDateTimeEventArgs property. The WaterMarkContent will be displayed in case of null SelectedValue. Let us know if you need any further questions.

Sincerely yours,
Kaloyan
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
Brian
Top achievements
Rank 1
answered on 09 Sep 2010, 02:49 PM

My logic was in the ParseDateTimeValue event handler. I do not want to do my own parsing logic, I just want to validate the date after it has been entered and if necessary revert it to a previous value.

The following does seem to work. Is this what you meant?

        void _ActiveDatePicker_ParseDateTimeValue(object sender, ParseDateTimeEventArgs args)
        {
            if (!ValidateDates())
            {
                _ActiveDatePicker.SelectedValue = _previousActiveDate;

            }
        }

0
Kaloyan
Telerik team
answered on 14 Sep 2010, 09:07 AM
Hello Brian,

Follow the recommended code bellow:

private void radDateTimePicker_ParseDateTimeValue(object sender, Telerik.Windows.Controls.ParseDateTimeEventArgs args)
       {
           if (!args.IsParsingSuccessful && !ValidateDates())
           {
               args.Result = previosDate;
               args.IsParsingSuccessful = true;
           }
       }


Kind regards,
Kaloyan
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
Brian
Top achievements
Rank 1
answered on 07 Oct 2010, 11:23 PM
I believe it should be

if (!args.IsParsingSuccessful && !ValidateDates())

otherwise my ValidateDates method never gets called if the parsing was successful.
0
Brian
Top achievements
Rank 1
answered on 07 Oct 2010, 11:43 PM
If I do the following in the ParseDateTime handler and this._OriginalObject.InactiveDate is null I get the exception listed below once control leaves the handler.

            if (args.IsParsingSuccessful && !ValidateDates())
            {
                args.Result = this._OriginalObject.InactiveDate;
                args.IsParsingSuccessful = true;
            } 
Exception and call stack.
[System.InvalidOperationException] {System.InvalidOperationException: Nullable object must have a value.
   at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
   at System.Nullable`1.get_Value()
   at Telerik.Windows.Controls.RadDateTimePicker.OnCurrentDateTimeTextChanged()
   at Telerik.Windows.Controls.RadDateTimePicker.OnCurrentDateTimeTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   at Telerik.Windows.PropertyMetadata.<>c__DisplayClass1.<Create>b__0(DependencyObject d, DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object oldValue, Object newValue)
   at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
   at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet)
   at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
   at Telerik.Windows.Controls.RadDateTimePicker.set_CurrentDateTimeText(String value)
   at Telerik.Windows.Controls.RadDateTimePicker.OnDateTimeTextChanged()
   at Telerik.Windows.Controls.RadDateTimePicker.OnDateTimeTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   at Telerik.Windows.PropertyMetadata.<>c__DisplayClass1.<Create>b__0(DependencyObject d, DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object oldValue, Object newValue)
   at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
   at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet)
   at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
   at Telerik.Windows.Controls.RadDateTimePicker.set_DateTimeText(String value)
   at Telerik.Windows.Controls.RadDateTimePicker.<UpdateText>b__5()} System.InvalidOperationException
0
Brian
Top achievements
Rank 1
answered on 08 Oct 2010, 03:30 PM
Okay, I get why the exception. I am setting args.IsParsingSuccessful even if the date is null. If my previous date is null I will set IsParsingSuccessful to false.

However, there is a bug in RadDateTime. If I set it to a value using args.Result = previousValue then the text portion of the date control does not change. If it set it to null the text portion of the control does not blank out.
0
Kaloyan
Telerik team
answered on 13 Oct 2010, 02:04 PM
Hi Brian,

Which version of the control are you using?

Kind regards,
Kaloyan
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
Brian
Top achievements
Rank 1
answered on 13 Oct 2010, 02:27 PM
2010.2.924.1040
0
Kaloyan
Telerik team
answered on 18 Oct 2010, 12:23 PM
Hi Brian,

I have made a small test project. It stores the old valid date entry and in case of an invalid input it restores the old valid. This means that the text will display the valid one. Am i missing something, as I may be not properly into your case.

public partial class MainPage : UserControl
    {
        DateTime oldValue;
        public MainPage()
        {
            InitializeComponent();
        }
 
        private void radDateTimePicker_ParseDateTimeValue(object sender, ParseDateTimeEventArgs args)
        {
            if (!args.IsParsingSuccessful)
            {
                args.IsParsingSuccessful = true;
                args.Result = oldValue;
            }
            else
            {
                oldValue = args.Result.Value;
            }
        }
    }


Regards,
Kaloyan
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
DateTimePicker
Asked by
Brian
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Brian
Top achievements
Rank 1
Share this question
or