[英]Extending RequiredFieldValidator
Hello I'm trying to extend the requirefieldvalidator to take a new property and validate regularexpressions also. 您好我正在尝试扩展requirefieldvalidator以获取新属性并验证regularrexpressions。 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.
我知道我可以使用RegularExpression控件,但后来我需要2个控件,所以我想要消除它,所以我只需要使用两个控件。 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. 我的问题是我不知道要覆盖什么 - 我尝试了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;
}
}
}
I think you should use CustomValidator instead or derive from BaseValidator abstract class 我认为你应该使用CustomValidator或者从BaseValidator抽象类派生
http://msdn.microsoft.com/en-us/library/aa720677(v=vs.71).aspx http://msdn.microsoft.com/en-us/library/aa720677(v=vs.71).aspx
You should override EvaluateIsValid()
method. 您应该覆盖
EvaluateIsValid()
方法。 BaseValidator.Validate()
method uses virtual EvaluateIsValid
internally. BaseValidator.Validate()
方法在内部使用virtual EvaluateIsValid
。 Eg: 例如:
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.