为什么自定义验证器的错误消息没有显示在消息框中?

发布于 2024-10-12 20:17:41 字数 1942 浏览 2 评论 0原文

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

I have tried in many way but the error message for custom validator is not shown in validation summary but it(ValidationSummary) shows error message for every other type of validator.

<%@ 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

简单 2024-10-19 20:17:42

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

I solved this issue by registering a client script block, which updates the validation summary content. I have used jQuery for this:

 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());
 }

The method has to be called if validation fails:

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

        if (!args.IsValid)
        {
          DisplayCustomValidationMessage((CustomValidator) source, vsYourValidationSummaryControl);
        }
    }
小ぇ时光︴ 2024-10-19 20:17:41

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

Custom Validator, when placing in formview will not show its error message after server-side validation (though it has been validated and result is invalid) the mean to fix this in to wrap it by a Update Panel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文