.ascx中的自定义验证程序不显示错误消息

时间:2015-03-27 15:18:46

标签: c# ascx customvalidator

我是C#的新手。我在页面上有这个控件如下:

     <asp:ValidationSummary runat="server" HeaderText="There were errors on the page:" ShowSummary="true" ForeColor="Red"/>
        <asp:TextBox id="email" runat="server" />
        <asp:CustomValidator ID="emailValidator" runat="server" ErrorMessage="Email is invalid." ForeColor="Red" OnServerValidate="ValidateEmail" ControlToValidate="email" Display="Dynamic">*</asp:CustomValidator>

在cs代码中:

protected void ValidateEmail(object o, ServerValidateEventArgs e)
{
  ValidateEmailAttribute emailValidator = new ValidateEmailAttribute();
  if (!emailValidator.Validate(this.email.Text))
  {
    e.IsValid = false;
  }
  if (this.email.Text.Contains("#"))
  {
      e.IsValid = false;
  }
  return;
}

当电子邮件无效时,它不会显示错误消息。页面上以红色显示的所有内容均为:

    There were errors on the page:    

但这还不足以让用户了解错误的位置。为什么“电子邮件无效”。没显示?

由于

1 个答案:

答案 0 :(得分:0)

最后能够通过添加以下内容来解决此问题:

  this.emailValidator.ErrorMessage = "Email is invalid. '#' is not allowed in email.";