DevExpress DateEdit repository editor date validation
If users are going to be typing dates as well as choosing from the dropdown calendar widget, where is the best event to trap the entered value, whether it was typed or picked, then warn users if the date fails some validation, and finally rollback the edit value to where it was if the user decides not to override the war开发者_StackOverflowning?
We need to allow dates in the past, but want to prevent accidental dates in the past, which typically occurs in the first few months of the new year after users have been accustomed to typing, say, 2011 for the entire year and then when the year changes to 2012, they type 2011 out of habit. So this validation would only be in effect for the first few months of the year, not year-round.
I don't see how to rollback the value in EditValueChanged. The args don't have a cancel option there. Is there another better event to do this that works with typed values and picked values?
Use the EditValueChanging
event. It does have a Cancel
event, along with NewValue
and OldValue
.
I prefer to use Validating event of Control, where i get CancelEventArgs with which you can set the value as well as the focus on the control. e.Cancel will set the focus on control. eg:
if (txtName.Text == "")
{
txtName.ErrorText = "Client Name should not be blank.";
e.Cancel = true;
}
精彩评论