您好,我正在尝试扩展requirefieldvalidator,以获取一个新属性,并验证规则表达式。我知道我可以使用RegularExpression控件,但是我需要两个控件,所以我想消除它,所以我只需要使用两个控件。我还想做一些其他的功能,包括扩展它。
我的问题是我不知道要覆盖什么-我尝试了validating (),但是我得到了“无法覆盖继承的成员'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;
}
}
}发布于 2011-03-16 18:29:16
我认为你应该使用CustomValidator或者从BaseValidator抽象类派生
http://msdn.microsoft.com/en-us/library/aa720677(v=vs.71).aspx
发布于 2011-03-16 18:49:34
您应该重写EvaluateIsValid()方法。BaseValidator.Validate()方法在内部使用virtual EvaluateIsValid。例如:
protected override bool EvaluateIsValid()
{
bool isValid = base.EvaluateIsValid();
if (isValid)
{
string controlToValidate = this.ControlToValidate;
string controlValue = GetControlValidationValue(controlToValidate);
if (!string.IsNullOrWhiteSpace(controlValue))
{
if (this._regEx != null && _regEx.Length > 0)
{
if (Regex.IsMatch(controlValue, _regEx))
isValid = true;
}
}
}
return isValid;
}https://stackoverflow.com/questions/5323925
复制相似问题