日期:2014-05-19  浏览次数:20411 次

checkBoxList验证问题
怎么用CustomValidator验证CheckBoxList是否为空。
用js能实现吗?

------解决方案--------------------
if(checkboxid.checked)
{
///代码
}
------解决方案--------------------
asp.net 2.0默认没有验证控件可以对这个情况进行验证,那我就试试customvalidtor吧。这东西还真tm不好用,发现其也不能验证checkboxlist,而且我试验验证textbox还是没有成功。经过查资料研究发现,customvalidtor的controltovalidate属性不要进行设置!利用clientvalidation来通过客户端的方式进行验证。方法如下

<asp:CheckBoxList ID= "chkorigin " runat= "server " RepeatDirection= "Horizontal " RepeatColumns= "4 ">
<asp:ListItem> Baidu </asp:ListItem>
<asp:ListItem> Google </asp:ListItem>
<asp:ListItem> EASY FM91.5 </asp:ListItem>
<asp:ListItem> HIT FM88.7 </asp:ListItem>
<asp:ListItem> 朋友介绍 </asp:ListItem>
<asp:ListItem> 路过(发光字) </asp:ListItem>
<asp:ListItem> 其他 </asp:ListItem>
<asp:ListItem> 不记得 </asp:ListItem>
</asp:CheckBoxList>
<asp:CustomValidator ID= "CustomValidator1 " runat= "server " ErrorMessage= "请至少选择一项 " ClientValidationFunction= "ClientValidate " Width= "85px "> </asp:CustomValidator>

<script language= "javascript ">
<!--
function ClientValidate(sender, args)
{
var flag = false;
var inarr=form1.all.tags( "input ");
for (var i=0; i <inarr.length; i++)
{
if(inarr[i].type== "checkbox ")
{
if(inarr[i].checked==true)
{
flag = true;
}
}
}
if (flag)
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
-->
</script>