1

The ValidationSummary panel shows up, even if the validator does not have an 'ErrorMessage'. When I click on Submit with invalid login, the error shows up in summary. That part's fine. When I submit with empty textboxes, it should show only the '*', but it also shows an empty summary panel. How can I prevent this. [Have a css class for .summary,header, summary ul, .summary ul li].

JS:

function validateTextBox(sender, args) {
        var target = document.getElementById(sender.controltovalidate);
        var is_valid = target.value != "";
        if (is_valid) {
            target.className = "";
        }
        else {
            target.className = "validate";
        }
        args.IsValid = is_valid;
    }

ASPX:

<asp:ValidationSummary ID="vs" runat="server" CssClass="summary"   
 ValidationGroup="vsGroup" DisplayMode="BulletList" EnableClientScript="true" 
 HeaderText="<div class='header'>&nbsp;&nbsp;Please Correct The 
 Following</div>" />

 <asp:TextBox ID="txtSurname" runat="server" class=""></asp:TextBox>
 <asp:CustomValidator ID="CustomValidator1" runat="server" 
 ValidationGroup="vsGroup" ControlToValidate="txtSurname"  
 ClientValidationFunction="validateTextBox" Text="*" 
 ForeColor="Red" ValidateEmptyText="true" ></asp:CustomValidator>

 <asp:TextBox ID="txtLogin" runat="server" class=""></asp:TextBox>
 //....CustomValidator2 just as above..
 <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" 
 ControlToValidate="txtLogin" Text="*" ForeColor="Red" 
 ValidationExpression="^[a-z]{8,10}$" Display="Dynamic" 
 ErrorMessage="Login Does Not Match The Requirements" ValidationGroup="vsGroup" 
 EnableClientScript="true"></asp:RegularExpressionValidator>

<asp:Button ID="btnSubmit" runat="server" Text="Submit" ValidationGroup="vsGroup" />
4

2 回答 2

2

尝试将ShowValidationErrors属性设置falseValidationSummary控件。如果应该显示来自验证器控件的验证摘要,则将此值设置为 true;否则为假。默认情况下,此值为true.

在此处阅读MSDN

编辑:

如果您需要为某些控件而不是全部禁用 ValidationSummary,您可以在客户端和服务器端执行此操作。

var CustomVal = document.getElementById('CustomValidator1ClientID'); 
ValidatorEnable(CustomVal, false);

检查此链接: http: //www.codeproject.com/Articles/8635/Custom-Validation-Summary-in-ASP-NET 和这个链接: ASP.NET 验证摘要:如何使用 JavaScript 禁用验证器?

在服务器端,您可以执行以下操作Page_Load来检查是否有任何验证器已ErrorMessage设置,如果没有,则禁用验证器:

if( String.IsNullOrEmpty(CustomValidator1.ErrorMessage))
 //if ErrorMessage is empty, disable this validator only
CustomValidator1.Enabled=false; 

IInd Case::在这种情况下,当您只想禁用ValidationSummary验证器而不是验证器时,您只需将ValidationGroup属性设置如下:

// 标记

<asp:ValidationSummary ID="vs" runat="server" ValidationGroup="Group1"...

// 后面的代码

protected override void Page_Load(object sender, EventArgs e)
 {
    if( String.IsNullOrEmpty(CustomValidator1.ErrorMessage))
         //if ErrorMessage is empty, exclude this from Validation Summary
        CustomValidator1.ValidationGroup=""; 
        CustomValidator2.ValidationGroup="Group1";
 }

现在确保您将ValidationGroup属性分配给您的ValidationSummary控件。所以,想法是:所有应该在 中显示错误消息的控件ValidationSummary,将它们的属性设置为与您的ValidationGroup属性相同。ValidationSummaryValidationGroup

那些不应该显示错误消息的控件ValidationSummary,只需将它们的ValidationGroup属性设置为空。

注意:您也可以在后面的代码中设置ValidationGroup 属性。也。ValidationSummary确保您还将ValidationGroup属性和CausesValidation属性添加到您的提交按钮标记中,例如:<asp:Button ID="Submit" runat="server" ValidationGroup="vsGroup" CausesValidation="true"/>

注意 2 ::即使没有错误,ValidationSummary 使用以下类似的 HTML 仍然可以看到

<div id="vs" class="validationSummary" style="">
<ul></ul>

这是 Validation summary 的默认行为,如果您想这么说,这是一种错误。检查这篇文章:http ://forums.asp.net/t/1657025.aspx/2/10 。

因此,如果您想在没有错误的情况下隐藏上述 HTML,只需检查是否有任何li元素,如果没有,则ValidationSummary完全隐藏错误。

<script type="text/javascript">
     $(document).ready(function () {
          if ($(".validationSummary li:visible").length === 0) {
               $(".validationSummary").hide();
          }
     });
</script>
</div>
于 2013-08-21T12:45:14.450 回答
1

您可以将其设置为Enabled="false"orVisible="false"然后当CustomValidator触发时(如果需要)您可以在后面的代码中将其启用为vs.Enabled="true"or 和vs.Visible="true"

于 2013-08-21T12:51:54.093 回答