一个自定义验证类继承于BaseValidator,在ValidationSummary无法提示验证消息?请问如何解决?
代码如下:
public class SmartValidator : BaseValidator
{
#region .ctor
public SmartValidator()
{
//
// TODO: Add constructor logic here
//
}
#endregion
#region Properties
public string ErrorMessage
{
get
{
ViewState["ErrorMessage"] = "您输入的身份证号码无效!";
return (string)ViewState["ErrorMessage"];
}
set { ViewState["ErrorMessage"] = value; }
}
#endregion
#region Methods
protected override bool ControlPropertiesValid()
{
string controlToValidate = base.ControlToValidate;
if (controlToValidate.Length > 0)
{
base.CheckControlValidationProperty(controlToValidate, "ControlToValidate");
}
return true;
}
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);
if (base.RenderUplevel)
{
string clientID = this.ClientID;
AddExpandoAttribute(writer, clientID, "evaluationfunction", "CustomValidatorEvaluateIsValid", false);
AddExpandoAttribute(writer, clientID, "errormessage", this.ErrorMessage, false);
}
}
private void AddExpandoAttribute(HtmlTextWriter writer, string controlId, string attributeName, string attributeValue)
{
this.AddExpandoAttribute(writer, controlId, attributeName, attributeValue, true);
}
private void AddExpandoAttribute(HtmlTextWriter writer, string controlId, string attributeName, string attributeValue, bool encode)
{
AddExpandoAttribute(this, writer, controlId, attributeName, attributeValue, encode);
}
private static void AddExpandoAttribute(Control control, HtmlTextWriter writer, string controlId, string attributeName, string attributeValue, bool encode)
{
if (writer != null)
{
writer.AddAttribute(attributeName, attributeValue, encode);
}
else
{
Page page = control.Page;
page.ClientScript.RegisterExpandoAttribute(controlId, attributeName, attributeValue, encode);
}
}
protected override bool EvaluateIsValid()
{
string inputValue = GetControlValidationValue(this.ControlToValidate);
return this.OnServerValidate(inputValue);
}
protected bool OnServerValidate(string inputValue)
{
//....
return false;
}
}