CompareValidator is not working correctly
I would prefer to use the asp.net Validation control as I currently have other validation controls within the same view. I need the error messages to appear in the validation summary.
I have two textboxes and I need to make sure textboxA is a LessThan textbox.
I used CompareValidator and set the following properties:
- ControlToCompare: textboxB
- ControlToValidate: textboxA
- Operator: GreaterThan / Also tried LessThan
- Type: Date
Here's the problem:
- When I provide the time in the textbox and go to the textbox. an error is displayed. I thought if the expression would fix this, but it doesnt.
In the Click event for the Update button, I added the following code as I only need it to check if both textboxA / textboxB! = Null.
if(String.IsNullOrEmpty(textboxB.Text))
{
Debug.Write("Valid");
timeCompareValidator.IsValid = true;
}
Thanks in advance for your help.
Clare
source to share
If you understand correctly, you need to compare two dates.
DateTime date1 = new DateTime(2009, 8, 1, 0, 0, 0);
DateTime date2 = new DateTime(2009, 8, 1, 12, 0, 0);
int result = DateTime.Compare(date1, date2);
string relationship;
if (result < 0)
relationship = "is earlier than";
else if (result == 0)
relationship = "is the same time as";
else
relationship = "is later than";
Console.WriteLine("{0} {1} {2}", date1, relationship, date2);
// The example displays the following output:
// 8/1/2009 12:00:00 AM is earlier than 8/1/2009 12:00:00 PM
source to share
I would use CustomValidator ( http://msdn.microsoft.com/en-us/library/9eee01cx(VS.71).aspx ). BTW: Why are you calling the validator directly?
timeCompareValidator.Validate();
Typically validators are evaluated during Page.Validate () which is triggered by each button (unless CausesValidation is set to false)
source to share