2014年7月13日 星期日

[RESOLVED] Validation of two Calendar controls


Hello there,


I would, kindly, ask for help.


I have 2 Calendar control dateFrom and dateTo and I want to be able to notify user when he choses dateFrom which is later than dateTo.


Here's what I have, atm:


Logs.aspx:


                        





ControlToValidate="TextBoxdateFrom"
CotrolToCompare="TextBoxdateTo"
Operator="GreaterThan"
ErrorMessage="Error!"

Logs.apsx.cs:


    protected void dateFrom_SelectionChanged(object sender, EventArgs e)
{
TextBoxdateFrom.Text = dateFrom.SelectedDate.ToShortDateString();
}

protected void dateTo_SelectionChanged(object sender, EventArgs e)
{
TextBoxdateTo.Text = dateTo.SelectedDate.ToShortDateString();
}

Unfortunately, CompareValidator doesn't return error message in case selected dateFrom is later than selected dateTo.


Any sugestion, please.


Thanks a lot



Try setting the Type property of the validator to Date. 



Change your check condition in compare field validator to lessthan instead of GreaterThan,


Please try with the below code


         CotrolToCompare="TextBoxdateTo" Operator="LessThan" ErrorMessage="Error!">                         

Rendered Output




Set Date CompareValidator type = "Date", ControlToCompare = "datefrom", ControlToValidate = "DateTo"


     ControlToValidate="TextBoxdateTo"
ControlToCompare="TextBoxdateFrom"
Operator="GreaterThan"
ErrorMessage="Error!"
Type="Date">

Compare Validator will not work until you fill both values


You need two additional RequiredFieldValidators


                    ControlToValidate="TextBoxdateFrom" ErrorMessage="Error!">
ControlToValidate="TextBoxdateTo" ErrorMessage="Error!">





MetalAsp.Net



Try setting the Type property of the validator to Date. 





When I add Type="Date" to CompareValidator I get next error message:


The value '' of the ValueToCompare property of 'CompareValidator1' cannot be converted to type 'Date'.



try set


CultureInvariantValues="True"



Strangely, with LessThan it always returns error message, while with
GreaterThan it never returns error message.



Again the same error: The value '' of the ValueToCompare property of 'CompareValidator1' cannot be converted to type 'Date'.


                                                    ControlToValidate="TextBoxdateTo"
CotrolToCompare="TextBoxdateFrom"
CultureInvariantValues="True"
Type="Date"
Operator="GreaterThan"
ErrorMessage="Error!"

BTW, I've added two RequiredFieldValidator controls to TextBoxes, like:


                        
ControlToValidate="TextBoxdateTo"
ErrorMessage="Error!"




I have been fiddling with Regional setting and got it to work somehow. Here's final code:


                                                    ControlToValidate="TextBoxdateTo" 
ControlToCompare="TextBoxdateFrom"
CultureInvariantValues="true"
Type="Date"
Operator="GreaterThanEqual"
ErrorMessage="Error!

Thanks again!


沒有留言:

張貼留言