用动态控件模拟复合控件(二)

用动态控件模拟复合控件(二)

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);
this.AddValidator(cv);
}
}
}
public class NumberTextBox : NumberTextBox<int>
{
public NumberTextBox()
{
this.Type = ValidationDataType.Integer;
}
protected override int DefaultMinValue
{
get { return int.MinValue; }
}
protected override int DefaultMaxValue
{
get { return int.MaxValue; }
}
}
public class MoneyTextBox : NumberTextBox<decimal>
{
public MoneyTextBox()
{
this.Type = ValidationDataType.Currency;
}
protected override decimal DefaultMinValue
{
get { return decimal.MinValue; }
}
protected override decimal DefaultMaxValue
{
get { return decimal.MaxValue; }
}
}

RegexTextBox ,PhoneTextBox ,EMailTextBox,StringTextBox

    public class RegexTextBox : BaseTextBox
{
private string regexMatch = string.Empty;
public virtual string ValidationExpression
{
get { return regexMatch; }
set { regexMatch = value; }
}
protected override void BuildValidatorControls()
{
base.BuildValidatorControls();
//添加额外的控件
if (!string.IsNullOrEmpty(this.ValidationExpression))
{
RegularExpressionValidator rev = new RegularExpressionValidator();
rev.ID = "rv_" + this.ID;
rev.ControlToValidate = this.ID;
rev.ValidationExpression = this.ValidationExpression;
rev.SetFocusOnError = true;
rev.ErrorMessage = string.Format("{0}的值格式不正确!{1}",this.DisplayName, GetExampleString());
this.AddValidator(rev);
}
}
}
public class StringTextBox : RegexTextBox
{
}
public class EMailTextBox : RegexTextBox
{
public EMailTextBox()
{
this.ValidationExpression = @"^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$";
}
}
[Flags]
public enum PhoneTypes
{
/// <summary>
/// 手机
/// </summary>
Mobile = 1,
/// <summary>
/// 小灵通
/// </summary>
Unicom = 2,
/// <summary>
/// 电话
/// </summary>
Phone = 4
}
public class PhoneTextBox : RegexTextBox
{
private PhoneTypes phoneTypes = PhoneTypes.Phone;
/// <summary>
/// 电话类型
/// </summary>
public PhoneTypes PhoneTypes
{
get { return phoneTypes; }
set { phoneTypes = value; }
}
public override string ValidationExpression
{
get
{
if (!string.IsNullOrEmpty(base.ValidationExpression))
{
return base.ValidationExpression;
}
else
{
StringBuilder sb = new StringBuilder();
if ((this.PhoneTypes & PhoneTypes.Phone) == PhoneTypes.Phone)
{
if (sb.Length > 0)
sb.Append("|");
sb.Append(@"(^d{3,4}-d{7,8}$)");
}
if ((this.PhoneTypes & PhoneTypes.Mobile) == PhoneTypes.Mobile)
{
if (sb.Length > 0)
sb.Append("|");
sb.Append(@"(^1[35]d{9}$)");
}
if ((this.PhoneTypes & PhoneTypes.Unicom) == PhoneTypes.Unicom)
{
if (sb.Length > 0)
sb.Append("|");
sb.Append(@"(^d{3,4}-d{7,8}$)");
}
return sb.ToString();
}
}
set
{
base.ValidationExpression = value;
}
}
}

差不多就这样了,如果有问题请留言。