我正在尝试验证至少选中复选框列表中的一个复选框。我收到错误,如'cvDaysofWeeklist'的ControlToValidate属性引用的Control'CheckBoxListDaysofWeek'无法验证。下面是我的asp代码。
<asp:CheckBoxList ID="CheckBoxListDaysofWeek" runat="server" RepeatDirection="Horizontal" CssClass="formatcheckboxlist" ValidationGroup="ReportSearch" CausesValidation="true">
<asp:ListItem Text="Mon" Value="1" Selected="True"></asp:ListItem>
<asp:ListItem Text="Tue" Value="2" Selected="True"></asp:ListItem>
<asp:ListItem Text="Wed" Value="3" Selected="True"></asp:ListItem>
<asp:ListItem Text="Thu" Value="4" Selected="True"></asp:ListItem>
<asp:ListItem Text="Fri" Value="5" Selected="True"></asp:ListItem>
<asp:ListItem Text="Sat" Value="6"></asp:ListItem>
<asp:ListItem Text="Sun" Value="7"></asp:ListItem>
</asp:CheckBoxList>
<ams:AMSCustomValidator ID="cvDaysofWeeklist" runat="server" ClientValidationFunction="ValidateDaysofWeekList" ControlToValidate="CheckBoxListDaysofWeek"
Display="None" ValidationGroup="ReportSearch"
Resource="Content" ResourceName="TAGSEARCH_TEXTBOXMANUFACTURESTARTDATEVALIDATOR" />
<ajaxToolkit:ValidatorCalloutExtender ID="vce_DaysofWeek" runat="server" TargetControlID="cvDaysofWeeklist" HighlightCssClass="HighlightField"></ajaxToolkit:ValidatorCalloutExtender>`
我的javascript函数是
function ValidateDaysofWeekList(source, args) {
var chkListDow = document.getElementById('<%= CheckBoxListDaysofWeek.ClientID %>');
var chkListinputs = chkListDow.getElementsByTagName("input");
for (var i = 0; i < chkListinputs.length; i++) {
if (chkListinputs[i].checked) {
args.IsValid = true;
return;
}
}
args.IsValid = false;
}
答案 0 :(得分:0)
如果使用CustomValidator,则不应在CheckBoxList上设置ControlToValidate属性。
如果将ControlToValidate设置为CheckBoxList,.NET将引发异常。