Example #1
0
        protected override void OnLoad(EventArgs e)
        {
            bool isComparison = !string.IsNullOrEmpty(_comparisonControlID);

            if (string.IsNullOrEmpty(_targetControlID))
            {
                throw new ApplicationException("Required parameter TargetControlID not specified.");
            }

            if (string.IsNullOrEmpty(_fieldName))
            {
                throw new ApplicationException("Required parameter FieldName not specified.");
            }

            if (_required || isComparison)
            {
                RequiredFieldValidator rfv = new RequiredFieldValidator();

                rfv.ID = this.ID + "RFV";
                rfv.ControlToValidate = _targetControlID;
                rfv.Display           = ValidatorDisplay.None;
                rfv.ErrorMessage      = "<b>Required Field</b><br/>" + _fieldName + " is a required field.";
                rfv.ValidationGroup   = _validationGroup;

                this.Controls.Add(rfv);
                this.Controls.Add(GetExtender(rfv));
            }

            if (isComparison)
            {
                CompareValidator cv = new CompareValidator();

                cv.ID = this.ID + "CV";
                cv.ControlToValidate  = _targetControlID;
                cv.ControlToCompare   = _comparisonControlID;
                cv.Display            = ValidatorDisplay.None;
                cv.ErrorMessage       = "<b>Field Mismatch</b><br/>The " + _fieldName + " fields do not match.";
                cv.EnableClientScript = true;
                cv.ValidationGroup    = _validationGroup;

                this.Controls.Add(cv);
                this.Controls.Add(GetExtender(cv));
            }
            else
            {
                if (!string.IsNullOrEmpty(_mask) && string.IsNullOrEmpty(_validationRegex) && !_knownValidationRegex.HasValue)
                {
                    throw new ApplicationException("When the Mask parameter is set, the ValidationRegex or KnownValidationRegex must be set as well.");
                }

                if (!string.IsNullOrEmpty(_mask))
                {
                    MaskedEditExtender mex = new MaskedEditExtender();

                    mex.ID = this.ID + "MEX";
                    mex.TargetControlID      = _targetControlID;
                    mex.Mask                 = _mask;
                    mex.ClearMaskOnLostFocus = false;
                    mex.AutoComplete         = false;
                    mex.PromptCharacter      = "_";

                    this.Controls.Add(mex);
                }

                if (_knownValidationRegex.HasValue || !string.IsNullOrEmpty(_validationRegex))
                {
                    RegularExpressionValidator rev = new RegularExpressionValidator();

                    rev.ID = this.ID + "REV";
                    rev.ControlToValidate  = _targetControlID;
                    rev.Display            = ValidatorDisplay.None;
                    rev.ErrorMessage       = GetInvalidFieldDescription();
                    rev.EnableClientScript = true;
                    rev.ValidationGroup    = _validationGroup;

                    if (_knownValidationRegex.HasValue)
                    {
                        rev.ValidationExpression = RegexUtil.GetRegexStringForJavaScript(_knownValidationRegex.Value);
                    }
                    else
                    {
                        rev.ValidationExpression = _validationRegex;
                    }

                    this.Controls.Add(rev);
                    this.Controls.Add(GetExtender(rev));
                }
            }

            if (!string.IsNullOrEmpty(_clientValidationFunction))
            {
                CustomValidator cuv = new CustomValidator();

                cuv.ID = this.ID + "CUV";
                cuv.ControlToValidate        = _targetControlID;
                cuv.ClientValidationFunction = _clientValidationFunction;
                cuv.Display            = ValidatorDisplay.None;
                cuv.ErrorMessage       = GetInvalidFieldDescription();
                cuv.EnableClientScript = true;
                cuv.ValidationGroup    = _validationGroup;

                this.Controls.Add(cuv);
                this.Controls.Add(GetExtender(cuv));
            }

            base.OnLoad(e);
        }