【问题标题】:Why The error message for custom validator is not shown in message box?为什么自定义验证器的错误消息未显示在消息框中?
【发布时间】:2011-01-16 09:16:03
【问题描述】:

我尝试了多种方式,但自定义验证器的错误消息未显示在验证摘要中,但它(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>

【问题讨论】:

标签: asp.net validationsummary custom-validators


【解决方案1】:

自定义验证器,当放置在表单视图中时,在服务器端验证后不会显示其错误消息(尽管它已经过验证并且结果无效),这意味着修复它以通过更新面板将其包装起来。

【讨论】:

    【解决方案2】:

    我通过注册一个更新验证摘要内容的客户端脚本块解决了这个问题。我为此使用了 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);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-23
      • 2018-02-08
      • 2014-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多