在ValidationSummary中仅显示唯一的错误消息

时间:2013-01-28 12:16:52

标签: asp.net .net validation webforms validationsummary

如何让ValidationSummary控件只显示唯一的错误消息?

我正在使用Repeater执行CRUD操作并对某些字段使用验证控件(在ItemTemplate内)。例如,对于名字/姓氏列RequiredFieldValidator。我在验证器上设置了Text="*",因此很容易找到验证失败的位置。在ValidationSummary中,每列至少应有1条错误消息,其中至少有1个验证器失败。

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

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

  

名字是必需的

     

名字是必需的

     

姓氏是必需的

     

姓氏是必需的

取代期望的行为:

  

名字是必需的

     

姓氏是必需的

我找到的一个解决方案是在每个列的Repeater之外使用CustomValidator,并将它们放在与ValidationGroup相同的ValidationSummary中,但我认为必须是一个更好的解决方案。

1 个答案:

答案 0 :(得分: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时,它作为对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函数以使其可重用,删除了一些冗余代码。