简体   繁体   English

Asp.Net - CustomValidator 的 ErrorMessage - 仅在 ValidationSummary 上显示 - 其他地方不显示

[英]Asp.Net - CustomValidator's ErrorMessage - Only show on ValidationSummary - Not other places

Here is ASPX :这是 ASPX :

<form id="form1" runat="server">
    <div>
        <fieldset style="width: 160px">

            <legend>Select your skills</legend>

            <asp:ValidationSummary ID="ValidationSummary1" runat="server" />

            <asp:CheckBoxList ID="cblCourses" runat="server" RepeatColumns="2">

                <asp:ListItem>Asp.net</asp:ListItem>

                <asp:ListItem>C#</asp:ListItem>

                <asp:ListItem>VB</asp:ListItem>

                <asp:ListItem>WCF</asp:ListItem>

                <asp:ListItem>Jquery</asp:ListItem>

                <asp:ListItem>JavaScript</asp:ListItem>

            </asp:CheckBoxList>

            <asp:Label ID="lblStatus" runat="server" Text=""></asp:Label>

            <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Please select at least one skills." ClientValidationFunction="validateCheckBoxList" Display="Dynamic" ForeColor="Red"></asp:CustomValidator>

            <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" Style="height: 26px" />

        </fieldset>

    </div>
</form>

And here is javascript :这是 javascript :

<script type = "text/javascript">

    function validateCheckBoxList(source, args) {

        var chkListModules = document.getElementById('<%= cblCourses.ClientID %>');

        var chkListinputs = chkListModules.getElementsByTagName("input");

        for (var i = 0; i < chkListinputs.length; i++) {

            if (chkListinputs[i].checked) {

                args.IsValid = true;

                return;

            }

        }

        args.IsValid = false;

    }

</script>

The problem is when i click on btnSubmit button two error messages show up.问题是当我单击btnSubmit按钮时会出现两条错误消息。
One in ValidationSummary. ValidationSummary 中的一个。
The other one next to btnSubmit button. btnSubmit按钮旁边的btnSubmit
How can i tell CustomValidator to show it's message only in ValidationSummary, not other places?我如何告诉 CustomValidator 只在 ValidationSummary 中显示它的消息,而不是在其他地方?

You can wrap the CustomValidator in a div or span with display:none;您可以使用 display:none; 将 CustomValidator 包装在 div 或 span 中。 the error still shows in the summary but nowhere else: -错误仍然显示在摘要中,但没有其他地方:-

<span style="display:none;"> <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Please select at least one skills." ClientValidationFunction="validateCheckBoxList" Display="Dynamic" ForeColor="Red"></asp:CustomValidator> </span>

You should create a class for your div/span instead of inlining the style, this is just to demonstrate it works.您应该为您的 div/span 创建一个类而不是内联样式,这只是为了演示它的工作原理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM