喂!
我有一个简单的服务器控件:
public sealed class FooControl : CompositeControl
{
public string ValidationGroup
{
get
{
this.EnsureChildControls();
return this._fooValidator.ValidationGroup;
}
set
{
this.EnsureChildControls();
this._fooValidator.ValidationGroup = value;
}
}
private readonly CustomValidator _fooValidator = new CustomValidator
{
ID = "foo",
Display = ValidatorDisplay.None,
EnableClientScript = false,
Enabled = true,
ErrorMessage = "FooErrorMessage",
SetFocusOnError = true
};
protected override void CreateChildControls()
{
this.Controls.Add(this._fooValidator);
this._fooValidator.ServerValidate += ValidateFoo;
}
protected static void ValidateFoo(object source, ServerValidateEventArgs args)
{
args.IsValid = false;
}
}
现在我把验证称为
public partial class MyFooPage : Page
{
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
// FooControl was added @ OnLoad
// ValidationGroup of FooControl remains empty
// this.GetValidators() returns the fooValidator of FooControl
this.Validate();
}
}
但是页面仍然有效,并且customValidator不是无效的。
我挖得更深一点:
这个
.Validate()
-方法,调用
.Validate()
在每个验证器上,它内部调用
EvaluateIsValid()
的
CustomValidator
:
protected override bool EvaluateIsValid()
{
string controlValidationValue = string.Empty;
string controlToValidate = base.ControlToValidate;
if (controlToValidate.Length > 0)
{
controlValidationValue = base.GetControlValidationValue(controlToValidate);
if (((controlValidationValue == null) || (controlValidationValue.Trim().Length == 0)) && !this.ValidateEmptyText)
{
return true;
}
}
return this.OnServerValidate(controlValidationValue);
}
(c) 微软
然后打电话来
OnServerValidate
的
控件
:
protected virtual bool OnServerValidate(string value)
{
ServerValidateEventHandler handler = (ServerValidateEventHandler) base.Events[EventServerValidate];
ServerValidateEventArgs args = new ServerValidateEventArgs(value, true);
if (handler != null)
{
handler(this, args);
return args.IsValid;
}
return true;
}
(c) 微软
根据调试器的说法
base.Events
包含单个事件(我附加的验证方法),但不知何故它不会被激发。。。??!!我还试图在
ValidateFoo()
-方法,它实际上不会被命中。
有人能告诉我为什么,我怎样才能让这工作,我做错了什么吗?