【问题标题】:Asp.Net - CustomValidator's ErrorMessage - Only show on ValidationSummary - Not other placesAsp.Net - CustomValidator 的 ErrorMessage - 仅显示在 ValidationSummary - 不在其他地方
【发布时间】:2020-01-14 11:32:30
【问题描述】:

这里是 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>

这里是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>

问题是当我点击btnSubmit 按钮时出现两条错误消息。
ValidationSummary 中的一个。
btnSubmit 按钮旁边的另一个。
我如何告诉 CustomValidator 只在 ValidationSummary 中显示它的消息,而不是在其他地方?

【问题讨论】:

    标签: asp.net customvalidator validationsummary


    【解决方案1】:

    您可以使用 display:none 将 CustomValidator 包装在 div 或 span 中;错误仍然显示在摘要中,但没有其他地方:-

    <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>

    您应该为您的 div/span 创建一个类,而不是内联样式,这只是为了证明它有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多