关于验证CheckBoxList非空的方法

 

常用的验证控件不能验证CheckBoxList,若在后台验证是否为空又增大了前后台交互,这里可以用一个简单的方法验证checkboxlist是否为空。

... ...

<tr>
        <td class="kpms-fim-l">
            是否存在内外业<span style="color: Red;">*</span>
        </td>
        <td class="kpms-fim-r" colspan="5">
            <asp:CheckBoxList ID="cblIsHasInnerExter" runat="server"    RepeatDirection="Horizontal">
                <asp:ListItem Value="内业">内业</asp:ListItem>
                <asp:ListItem Value="外业">外业</asp:ListItem>
            </asp:CheckBoxList>
            <input type="hidden" runat="server" id="hiIsHasInner" field="IsHasInner" tablename="ProjectReportOfCompleteForm" />
            <input type="hidden" runat="server" id="hiIsHasExter" field="IsHasExter" tablename="ProjectReportOfCompleteForm" />
            <asp:CustomValidator ID="cvcblIsHasInnerExter" runat="server" ClientValidationFunction="CheckcblIsHasInnerExter"
                ErrorMessage="请选择是否存在内外业">*</asp:CustomValidator>
        </td>
    </tr>

..........

function CheckcblIsHasInnerExter(source, args) {
        var obj = document.getElementById("<%=cblIsHasInnerExter.ClientID %>");
        var tags = obj.all.tags('input');
        var isChecked = false;
        for (var i = 0; i < 2; i++) {
            if (tags[i].checked) {
                isChecked = true;
                break;
            }
        }
        if (isChecked) {
            args.IsValid = true;
        }
        else {
            args.IsValid = false;
        }
    }

 

 

你可能感兴趣的:(关于验证CheckBoxList非空的方法)