程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> 關於ASP.NET >> 用動態控件模擬復合控件(二)

用動態控件模擬復合控件(二)

編輯:關於ASP.NET

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; }
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved