然后,我们扩充该Validate函数,并把编译它为本地代码
public override void Validate() {
this.IsValid = true;
bool isBlank = (this.Text.Trim() == "");
if (isBlank) {
if (!AllowBlank) {
this.ErrorMessage = String.Format("''{0}'' " +
"cannot be blank.", this.FriendlyName);
this.IsValid = false;
}
} else {
try {
_value = Int32.Parse(this.Text);
if (_value < this.MinValue) {
this.ErrorMessage = String.Format("''{0}'' cannot " +
"be less than {1}",
this.FriendlyName, this.MinValue);
this.IsValid = false;
}
if (_value > this.MaxValue) {
this.ErrorMessage = String.Format("''{0}'' " +
"cannot be more than {1}",
this.FriendlyName, this.MinValue);
this.IsValid = false;
}
} catch {
this.ErrorMessage = String.Format("''{0}'' " +
"is not a valid integer.", this.FriendlyName);
this.IsValid = false;
}
}
}
public int Value {
get { return _value; }
set {
_value = value;
this.Text = _value.ToString();
}
}
结论
要写就那么多了,现在我们还可以在这个类的基础上创建诸如要求只能输入符合一定时间格式和货币格式,下面我们举一个例子以说明如何使用我们创建的控件
在此以前我们要实现同样的功能需要写以下的代码:
<asp:TextBox id="Number" runat="server"/>
<asp:RequiredFieldValidator id