扩展RequiredFieldValidator
您好,我正在尝试扩展 requirefieldvalidator 以获取新属性并验证正则表达式。我知道我可以使用正则表达式控件,但我需要 2 个控件,所以我想消除它,所以我只需要使用两个控件。我还想制作其他功能,其中包括扩展它。
我的问题是我不知道要覆盖什么 - 我尝试了 Validate() 但我得到“无法覆盖继承的成员 'System.Web.UI.WebControls.BaseValidator.Validate()' 因为它没有标记为虚拟、抽象、或覆盖”,我了解 EvaluateIsValid() 用于验证控件而不是控件中的内容。
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
namespace ClassLibrary
{
public class RequiredFieldValidatorExtended : RequiredFieldValidator
{
public RequiredFieldValidatorExtended()
{
}
private string _regEx;
public string RegEx
{
get
{
return _regEx;
}
set
{
_regEx = this.RegEx;
}
}
protected override bool EvaluateIsValid()
{
TextBox textBox = (TextBox)Page.Form.FindControl(ControlToValidate);
if (textBox.Text != null && textBox.Text.Length > 0)
{
if (this._regEx != null && _regEx.Length > 0)
{
if (Regex.IsMatch(textBox.Text, _regEx))
IsValid = true;
else
IsValid = false;
}
IsValid = true;
}
else
IsValid = false;
base.Validate();
return IsValid;
}
}
}
Hello I'm trying to extend the requirefieldvalidator to take a new property and validate regularexpressions also. I know I can use the RegularExpression control but then i need 2 controls so i want to eliminate it so i only have to use two controls. There are also other features i want to make which include me extending it.
My problem is i don't know what to override - i tried the Validate() but i get "cannot override inherited member 'System.Web.UI.WebControls.BaseValidator.Validate()' because it is not marked virtual, abstract, or override" and i understand the EvaluateIsValid() is for validating the control not what is in the control.
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
namespace ClassLibrary
{
public class RequiredFieldValidatorExtended : RequiredFieldValidator
{
public RequiredFieldValidatorExtended()
{
}
private string _regEx;
public string RegEx
{
get
{
return _regEx;
}
set
{
_regEx = this.RegEx;
}
}
protected override bool EvaluateIsValid()
{
TextBox textBox = (TextBox)Page.Form.FindControl(ControlToValidate);
if (textBox.Text != null && textBox.Text.Length > 0)
{
if (this._regEx != null && _regEx.Length > 0)
{
if (Regex.IsMatch(textBox.Text, _regEx))
IsValid = true;
else
IsValid = false;
}
IsValid = true;
}
else
IsValid = false;
base.Validate();
return IsValid;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您应该使用 CustomValidator 代替或从 BaseValidator 抽象类派生
http://msdn.microsoft.com/en-us/library/aa720677(v=vs.71).aspx
I think you should use CustomValidator instead or derive from BaseValidator abstract class
http://msdn.microsoft.com/en-us/library/aa720677(v=vs.71).aspx
您应该重写
EvaluateIsValid()
方法。BaseValidator.Validate()< /code>
方法在内部使用
virtual EvaluateIsValid
。例如:You should override
EvaluateIsValid()
method.BaseValidator.Validate()
method usesvirtual EvaluateIsValid
internally. E.g.: