NumberTextBox
public abstract class NumberTextBox<T> : BaseTextBox where T : IComparable<T> { private T minValue = default(T); public virtual T MinValue { get { return minValue; } set { minValue = value; } } private T maxValue = default(T); public virtual T MaxValue { get { return maxValue; } set { maxValue = value; } } protected abstract T DefaultMinValue { get; } protected abstract T DefaultMaxValue { get; } protected ValidationDataType type; public virtual ValidationDataType Type { get { return type; } set { type = value; } } public NumberTextBox() { this.FilterType = FilterTypes.Numbers; this.Options = TextBoxOptions.Required | TextBoxOptions.Filtered; this.MaxValue = this.DefaultMaxValue; this.MinValue = this.DefaultMinValue; } protected override void BuildValidatorControls() { base.BuildValidatorControls(); //添加额外的控件 if ((this.MinValue.CompareTo(this.DefaultMinValue) != 0) && (this.MaxValue.CompareTo(this.DefaultMaxValue) != 0)) { RangeValidator rv = new RangeValidator(); rv.ID = "rv_" + this.ID; rv.ControlToValidate = this.ID; rv.MaximumValue = this.MaxValue.ToString(); rv.MinimumValue = this.MinValue.ToString(); rv.SetFocusOnError = true; rv.Type = this.Type; rv.ErrorMessage = string.Format("{0}的值应该位于{1}和{2}之间", this.DisplayName, this.MinValue, this.MaxValue); this.AddValidator(rv); } else if (this.MinValue.CompareTo(this.DefaultMinValue) != 0) { CompareValidator cv = new CompareValidator(); cv.ID = "rv_" + this.ID; cv.ControlToValidate = this.ID; cv.SetFocusOnError = true; cv.Type = this.Type; cv.Operator = ValidationCompareOperator.GreaterThanEqual; cv.ValueToCompare = this.MinValue.ToString(); cv.ErrorMessage = string.Format("{0}的值应该大于{1}", this.DisplayName, this.MinValue); this.AddValidator(cv); } else if ((this.MaxValue.CompareTo(this.DefaultMaxValue) != 0)) { CompareValidator cv = new CompareValidator(); cv.ID = "rv_" + this.ID; cv.ControlToValidate = this.ID; cv.SetFocusOnError = true; cv.Type = this.Type; cv.Operator = ValidationCompareOperator.LessThanEqual; cv.ValueToCompare = this.MaxValue.ToString(); cv.ErrorMessage = string.Format("{0}的值应该小于{1}", this.DisplayName, this.MaxValue); |