<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CheckboxlistDemo._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script language="javascript" type="text/javascript">
function checkIssueType(source, args) {
var chkListaTipoModificaciones = document.getElementById('<%= IssueTypeCheckBox.ClientID %>');
var chkLista = chkListaTipoModificaciones.getElementsByTagName("input");
for (var i = 0; i < chkLista.length; i++) {
if (chkLista[i].checked) {
args.IsValid = true;
return;
}
}
args.IsValid = false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<asp:CheckBoxList ID="IssueTypeCheckBox" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Value="1">Hardware</asp:ListItem>
<asp:ListItem Value="2">Software </asp:ListItem>
<asp:ListItem Value="3">Mechanical</asp:ListItem>
<asp:ListItem Value="4">Others</asp:ListItem>
</asp:CheckBoxList>
</td>
<td>
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="*" ClientValidationFunction="checkIssueType"></asp:CustomValidator>
</td>
</tr>
<tr>
<td><asp:Button runat="server" ID="btn" Text="Submit"/></td>
<td> </td>
</tr>
</table>
</div>
</form>
</body>
</html>c# web 如何使用RequiredFieldValidator控件验证CheckBoxList
最新推荐文章于 2024-04-16 08:55:20 发布
本文介绍了一个使用ASP.NET实现的简单示例,通过客户端JavaScript验证CheckboxList中至少有一个选项被选中。示例包括了HTML结构、服务器端代码以及客户端验证脚本。
555

被折叠的 条评论
为什么被折叠?



