为什么自定义验证程序的错误消息未显示在消息框中?

时间:2011-01-16 09:16:03

标签: asp.net validationsummary custom-validators

我尝试过很多方法,但自定义验证程序的错误消息未显示在验证摘要中,但它(ValidationSummary)显示每种其他类型验证程序的错误消息。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Expt_Custom Validator.aspx.cs" Inherits="Expt_Custom_Validator" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <script runat="server">
        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (args.Value.Equals("Jagdeep"))
                args.IsValid = false;
            else
                args.IsValid = true;

        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblName" runat="server" Text="Enter Your Name"></asp:Label>
        <asp:TextBox ID="txtbxName" runat="server"></asp:TextBox>

        <asp:CustomValidator ID="CustomValidator1" runat="server" 
            ErrorMessage="You are Not allowed" Display="None" 
            onservervalidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
        <br />
        <asp:Label ID="lblClass" runat="server" Text="Class"></asp:Label>
        <asp:TextBox ID="txtClass" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
            ErrorMessage="Please enter Clas" ControlToValidate="txtClass" Display="None"></asp:RequiredFieldValidator>
        <br />

        <asp:ValidationSummary ID="ValidationSummary1" runat="server" />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Validate" />

    </div>
    </form>
</body>
</html>

2 个答案:

答案 0 :(得分:4)

自定义验证程序在放置在窗体视图中时,在服务器端验证后不会显示其错误消息(虽然它已经过验证且结果无效),修复此问题的意思是通过更新面板将其包装起来。

答案 1 :(得分:0)

我通过注册客户端脚本块来解决此问题,该脚本块更新了验证摘要内容。我已经使用了jQuery:

 private void DisplayCustomValidationMessage(CustomValidator cv, ValidationSummary vs)
 {
      if ((cv == null) || (vs == null)) return;

      Type csType = this.GetType();

      if (ClientScript.IsClientScriptBlockRegistered(csType, cv.ID)) return;

      StringBuilder sb = new StringBuilder(@"<script type='text/javascript'>");
      sb.Append(@"$(function () {");
      sb.Append(@"var $vs = $('#" + vs.ClientID + "');");
      sb.Append(@"if($vs.find('ul').length){");
      sb.Append(@"$vs.find('ul').append('<li>" + cv.ErrorMessage + "</li>');");
      sb.Append(@"}else{");
      sb.Append(@"$vs.html('Fehlerhafte Eingabe(n):<ul><li>" + cv.ErrorMessage + "</li></ul>'); }");
      sb.Append(@"$vs.css('display', 'block');");
      sb.Append(@"});");
      sb.Append(@"</script>");

      ClientScript.RegisterClientScriptBlock(csType, cv.ID, sb.ToString());
 }

如果验证失败,则必须调用该方法:

protected void cvYourCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
    {
        args.IsValid = YourCustomValidationMethod();

        if (!args.IsValid)
        {
          DisplayCustomValidationMessage((CustomValidator) source, vsYourValidationSummaryControl);
        }
    }