1

How can I make the ValidationSummary control display only unique error messages?

I am using a Repeater to do CRUD operations and using validation controls (inside ItemTemplate) for some of the fields. For example a RequiredFieldValidator for First Name/Last Name columns. I set Text="*" on the validators so it would be easy to spot where the validation fails. In ValidationSummary there should be 1 error message for each column in which at least 1 validator fails.

The problem is that ValidationSummary will show the same message for each row in which a validation fails (which is expected behavior, but it would be nice to be able to remove the duplicate error messages).

Example, assuming 2 rows and no data entered:

First name is required

First name is required

Last name is required

Last name is required

Instead of the desired behaviour:

First name is required

Last name is required

One solution I found is to use a CustomValidator outside the Repeater for each column and place them in the same ValidationGroup as the ValidationSummary but I am thinking there must be a better solution.

Community
  • 1
  • 1
Răzvan Flavius Panda
  • 21,730
  • 17
  • 111
  • 169

1 Answers1

0

I also had duplicate messages showing up and found the following javascript solution that worked for me.

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>

The summary and the validators share a ValidationGroup which serves as input for the call to RemoveValidationDuplicates when the OnClientClick of the submit button is fired:

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

Solution originally posted by KIRAN on csharpquery.blogspot.nl

Update March 23rd: changed the javascript function to make it reusable, removed some redundant code.

alpha pecap
  • 353
  • 3
  • 11