2

这是我的checkbox列表代码:Data is bounded with Look Up Table

 <asp:CheckBoxList ID="cbl_AppliancePresent" ClientIDMode="Static"
 CssClass="cbl_AppliancePresent"
                     runat="server" DataTextField="Appliance" DataValueField="ApplianceID" RepeatDirection="Horizontal">
                 </asp:CheckBoxList>

这是Custom Validator

 <asp:CustomValidator ID="SpecifyAppliances" ClientIDMode="Static"
  runat="server" EnableClientScript="true" ErrorMessage="Please select
     Vacant-not secure reasons"  Font-Bold="True"
     ClientValidationFunction="ValidateNotSecure" ForeColor="Red" 
     Font-Size="Small"></asp:CustomValidator>

功能:

function ValidateNotSecure(source, args) {

    var options = $('.cbl_AppliancePresent');
    for (var i = 0; i < options.length; i++) {
              if (options[i].checked) {
        EnableValidator('SpecifyAppliances'); 
                   args.IsValid = true;
        return false;
               }
    }
    DisableValidator('SpecifyAppliances');
    args.IsValid = false; }
4

1 回答 1

3

如果您希望通过简单地使用 jquery 而不使用自定义验证器来解决它,那么这就是解决方案。

<body>
    <form id="form1" runat="server">
    <div id="chl1">
     <asp:CheckBoxList ID="CheckBoxList1" runat="server">
            <asp:ListItem>Item1</asp:ListItem>
            <asp:ListItem>Item2</asp:ListItem>
            <asp:ListItem>Item3</asp:ListItem>
            <asp:ListItem>Item4</asp:ListItem>
        </asp:CheckBoxList>
        <asp:Label ID="Label1" runat="server" Text="" ClientIDMode="Static"></asp:Label>

        <asp:Button ID="btncheck" runat="server" Text="Button" ClientIDMode="Static" />

    </div>
    </form>
    <script type="text/javascript">
        $(document).ready(function () {
           $("#btncheck").click(function () {
                var a = 0;
                $(":checkbox").each(function () {
                    if (this.checked) {
                        a = a + 1;
                    }
                });
                if (a == 0) {
                    $("span[id$='Label1']").text('Please select');
                    return false;
                }                           
            });
        });

    </script>
</body>
于 2013-08-10T18:12:15.580 回答