CustomValidator错误消息未显示在ValidationSummary中

时间:2013-07-16 19:26:44

标签: c# asp.net validation

我有点困惑。有人可以帮我指出为什么我的验证摘要没有显示自定义验证器吗?当我单步执行自定义验证器中的代码时,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;
            }
        }

1 个答案:

答案 0 :(得分:0)

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