c# web 如何使用RequiredFieldValidator控件验证CheckBoxList
来源:互联网 发布:上市大数据公司排名 编辑:程序博客网 时间:2024/06/04 19:48
<%@ 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>