Monday, 29 December 2014

Compare Validator creating at codebehind in c#

Compare Validator is one of the validator control in ASP.NET validators. With this,we can use to compare the controls with the help of operator and type attributes.

Listing all Types of CompareValidator

  • Currency
  • Date
  • Double
  • Integer
  • String

Listing all Operators of CompareValidator

  • DateTypeCheck
  • Equal
  • GreaterThan
  • GreaterThanEqual
  • NotEqual
  • LessThan
  • LessThanEqual

Now we can see the below example code created in code behind and used for validating the fields,

            CompareValidator cmpval = new CompareValidator();
            cmpval.ControlToCompare = FromDate.ClientID;
            cmpval.ControlToValidate = ToDate.ClientID;
            cmpval.Type = ValidationDataType.Date;
            cmpval.Operator = ValidationCompareOperator.GreaterThanEqual;
            cmpval.ForeColor = Color.Red;
            cmpval.Display = ValidatorDisplay.Dynamic;
            cmpval.ErrorMessage = "From Date should be smaller than the To Date";

Adding the above example control to Placeholder

That's it,

Happy Coding:)