asp.netvalidationwebformsvalidationsummaryvalidationgroup

ValidationSummary is not displaying error message on server side after group assignment


I have a ValidationSummary control that was initially working with all my validation controls at the beginning, however, later on when I assigned my validation controls to groups, ie, give them a group name in the ValidationGroups attribute, it does not seem to show the error messages of the validation controls that fails the validation on the server side. I'm using the call to Page.Validate() to validate all the controls on the server side. Has anyone ran into this issue before?

I experimented a bit with a simple page and noticed that this happens when you assign a CustomerValidator to a group but the problem goes away without the group.

Here is Start.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Start.aspx.cs" Inherits="WebApplication3.Start" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" CausesValidation ="true" OnClick="Button1_Click" style="height: 26px"/>
        <asp:ValidationSummary ID="ValidationSummary1" runat="server" />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ValidationGroup="Group1" Display="None" ErrorMessage="RequiredFieldValidator1"></asp:RequiredFieldValidator>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox2" ValidationGroup="Group2" Display="None" ErrorMessage="RequiredFieldValidator2"></asp:RequiredFieldValidator>
        <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="TextBox2" Display="None" ValidationGroup="Group2" ErrorMessage="Message for display in validation summary" OnServerValidate="cusCustom_ServerValidate"></asp:CustomValidator>
    </div>
    </form>
</body>
</html>

Here is Start.aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication3
{
    public partial class Start : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void cusCustom_ServerValidate(object sender, ServerValidateEventArgs e)
        {
            e.IsValid = false;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Page.Validate();
        }
    }
}

Solution

  • When you assign ValidationGroups to RequiredFieldValidators and CustomValidators, you also need to assign a ValidationGroup to your ValidationSummary, as you're using Display="None" (which means your validation controls will only show their ErrorMessage in their associated ValidationSummary control).

    Since you have Group1 and Group2, you need to add 2 ValidationSummary controls, one for Group1, and the other for Group2. Like this...

    <asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="Group1" />
    <asp:ValidationSummary ID="ValidationSummary2" runat="server" ValidationGroup="Group2" />
    

    Another thing to note is that CustomValidators will only validate after RequiredFieldValidators, so you won't see the error message from your CustomValidator until your RequiredFieldValidators pass validation.

    Hope this helps.