Why The error message for custom validator is not shown in message box?

Learner picture Learner · Jan 16, 2011 · Viewed 7.5k times · Source

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>

Answer

Sarawut Positwinyu picture Sarawut Positwinyu · Jun 1, 2011

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.