ValidationSummary中未显示CustomValidator错误消息

本文关键字:错误 消息 CustomValidator 显示 ValidationSummary | 更新日期: 2023-09-27 18:22:46

我有点困惑。有人能帮我指出为什么我的验证摘要没有显示自定义验证器吗?当我逐步完成自定义验证器中的代码时,args。IsValid设置为false。

<asp:ValidationSummary ID="ValidationSummary1" runat="server" CssClass="alert alert-danger" DisplayMode="List" ShowSummary="true" ValidationGroup="ValidateUser" /> 
    <fieldset class="form-horizontal">                
        <legend><asp:Literal runat="server" ID="litFirstName" /> <asp:Literal runat="server" ID="litLastName" /><asp:Literal runat="server" id="litLockedBadge" /></legend>   <div class="control-group">
            <label class="control-label" for="inputEmail">
            <asp:CustomValidator ID="cvEmailAddress" runat="server" ValidationGroup="ValidateUser" Text="*" CssClass="required" ControlToValidate="txtEmailAddress" Display="Dynamic" OnServerValidate="cvValidateEmail" /> 
            <asp:RequiredFieldValidator runat="server" CssClass="required" Display="Dynamic" ControlToValidate="txtEmailAddress" Text="*" ErrorMessage="Email Address Required" ValidationGroup="ValidateUser" /> Email Address</label>
                <div class="controls">
                    <asp:Textbox Text="test" runat="server" id="txtEmailAddress" ClientIDMode="Static" CssClass="span3" />                                
                </div>
            </div>
            <div class="control-group  input-append">
                <label class="control-label" for="txtUserName"><asp:CustomValidator runat="server" ID="cvUserName" Text="*" CssClass="required" Display="Dynamic" OnServerValidate="cvValidateUserName" ValidationGroup="ValidateUser" /><asp:RequiredFieldValidator runat="server" Display="Dynamic" CssClass="required" ControlToValidate="txtUserName" Text="*" ErrorMessage="Username Required" ValidationGroup="ValidateUser" /> Username</label>
                    <div class="controls">
                        <asp:Textbox Text="test" runat="server" id="txtUserName" ClientIDMode="Static" CssClass="span3" />
                        <asp:LinkButton data-attr="email" ID="lnkEmailUsername" runat="server" CssClass="btn" OnClick="btn_ClickEmailUsername" ClientIDMode="Static"><i class="icon-envelope"></i></asp:LinkButton>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="inputEmail"></label>
                    <div class="controls">
                        <div class="btn-group">
                            <asp:Button runat="server" ID="btnUnlockAccount" CssClass="btn" Text="Unlock Account" />
                            <asp:Button runat="server" ID="btnResetPassword" CssClass="btn" Text="Reset Password" />
                        </div>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="inputEmail"></label>
                    <div class="controls">                                
                        <asp:Button runat="server" ID="btnSubmit" CssClass="btn btn-primary" Text="Update User Account" OnClick="btn_UpdateUserAccount" ValidationGroup="ValidateUser" />
                    </div>
                </div>
            </fieldset>

这是背后的代码

//Validate the Username
        protected void cvValidateUserName(object source, ServerValidateEventArgs args)
        {
            string userName = txtUserName.Text;
            cvUserName.ErrorMessage = "";
            args.IsValid = true;
            if (userName.Contains(" "))
            {
                cvUserName.ErrorMessage = "Username cannot contain spaces. ";
                cvUserName.IsValid = false;
            }
            if (userName.Length > 100 || userName.Length < 8)
            {
                cvUserName.ErrorMessage += "Username must be between 8 and 100 characters. ";
                cvUserName.IsValid = false;
                return;
            }
        }
        //Validate Email Address
        protected void cvValidateEmail(object source, ServerValidateEventArgs args)
        {
            var emailAddress = txtEmailAddress.Text;
            try
            {
                new MailAddress(emailAddress);
            }
            catch (Exception)
            {
                args.IsValid = false;
                return;
            }
        }

ValidationSummary中未显示CustomValidator错误消息

为了在ValidationSummary中显示Validator的错误消息,您需要设置ValidatorDisplay="none"。我还设置了Text=""