1

如何使ValidationSummary控件仅显示唯一的错误消息?

我正在使用 a来执行 CRUD 操作并为某些字段Repeater使用验证控件(内部)。ItemTemplate例如 aRequiredFieldValidator用于 First Name/Last Name 列。我设置Text="*"了验证器,因此很容易发现验证失败的地方。对于ValidationSummary至少有 1 个验证器失败的每一列,应该有 1 条错误消息。

问题是ValidationSummary对于验证失败的每一行都会显示相同的消息(这是预期的行为,但能够删除重复的错误消息会很好)。

示例,假设 2 行且未输入数据:

名字是必填项

名字是必填项

姓氏是必填项

姓氏是必填项

而不是所需的行为:

名字是必填项

姓氏是必填项

我发现的一个解决方案是在每一列之外使用一个CustomValidatorRepeater并将它们放在相同ValidationGroup的位置,ValidationSummary但我认为必须有一个更好的解决方案。

4

1 回答 1

0

我还出现了重复的消息,并找到了以下对我有用的 javascript 解决方案。

Javascript:

<script language="javascript" type="text/javascript">

function RemoveValidationDuplicates(validationGroup) {
    Page_ClientValidate(validationGroup);

    if (typeof (Page_ValidationSummaries) == "undefined")
        return;
    var summary, sums, s;
    for (sums = 0; sums < Page_ValidationSummaries.length; sums++) {
        summary = Page_ValidationSummaries[sums];
        summary.style.display = "none";
        if (!Page_IsValid) {

            if (summary.showsummary != "False") {
                summary.style.display = "";
                if (typeof (summary.displaymode) != "string") {
                    summary.displaymode = "BulletList";
                }
                switch (summary.displaymode) {
                    case "List":
                        headerSep = "<br/>";
                        first = "";
                        pre = "";
                        post = "<br/>";
                        final = "";
                        break;
                    case "BulletList":
                    default:
                        headerSep = "";
                        first = "<ul>";
                        pre = "<li>";
                        post = "</li>";
                        final = "</ul>";
                        break;
                    case "SingleParagraph":
                        headerSep = " ";
                        first = "";
                        pre = "";
                        post = " ";
                        final = "<br/>";
                        break;
                }
                s = "";
                if (typeof (summary.headertext) == "string") {
                    s += summary.headertext + headerSep;
                }
                s += first;

                for (i = 0; i < Page_Validators.length; i++) {
                    if (!Page_Validators[i].isvalid && typeof (Page_Validators[i].errormessage) == "string") {
                        var tempstr = pre + Page_Validators[i].errormessage + post;
                        var isExist = s.search(tempstr);
                        if (isExist == -1)
                            s += pre + Page_Validators[i].errormessage + post;
                    }
                }
                s += final;
                summary.innerHTML = s;
                window.scrollTo(0, 0);
            }
        }
    }
}    
</script>

摘要和验证器共享一个 ValidationGroup,当提交按钮的 OnClientClick 被触发时,该 ValidationGroup 用作调用 RemoveValidationDuplicates 的输入:

<div>
    <asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="validationGroupName"/>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ControlToValidate="TextBox1" ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ValidationGroup="validationGroupName"></asp:RequiredFieldValidator>
    <br/>
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ControlToValidate="TextBox2" ID="RequiredFieldValidator2" runat="server" ErrorMessage="RequiredFieldValidator" ValidationGroup="validationGroupName"></asp:RequiredFieldValidator>
    <br/>
    <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ControlToValidate="TextBox4" ID="RequiredFieldValidator4" runat="server" ErrorMessage="RequiredFieldValidator1" ValidationGroup="validationGroupName"></asp:RequiredFieldValidator>
    <br/>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" OnClientClick="RemoveValidationDuplicates('validationGroupName')"/>
</div>

最初由 KIRAN 在csharpquery.blogspot.nl上发布的解决方案

3 月 23 日更新:更改了 javascript 函数以使其可重用,删除了一些冗余代码。

于 2013-03-20T18:46:34.013 回答