Sometimes the form must be non-empty, the checkbox is troublesome, can not be used RequiredFieldValidator control, so I use JS and CustomValidator control combination to achieve.
JS Code:
<script language= "javascript" type= "Text/javascript" >//Verify that the checkbox is not empty function checkisnotnull (source, args) {var ch1 = document.getElementById (' <%= type1. ClientID%> '); var CH2 = document.getElementById (' <%= type2. ClientID%> '); var CH3 = document.getElementById (' <%= type3. ClientID%> '); var CH4 = document.getElementById (' <%= type4. ClientID%> '); var ch5 = document.getElementById (' <%= type5. ClientID%> '); var ch6 = document.getElementById (' <%= type6. ClientID%> '); if (ch1.checked) {args. IsValid = true; Return } if (ch2.checked) {args. IsValid = true; Return } if (ch3.checked) {args. IsValid = true; Return } if (ch4.checked) {args. IsValid = true; Return } if (ch5.checked) {args. IsValid = true; Return } if (ch6.checked) {args. IsValid = true; Return } args. IsValid = false;}
ASPX code:
<TD colspan="2"> <asp:checkbox runat="Server"ID ="type1"Font-size="Large"Text ="Game Video Book"/> <asp:checkbox runat="Server"ID ="type2"Font-size="Large"Text ="Home Entertainment Book"/> <asp:checkbox runat="Server"ID ="Type3"Font-size="Large"Text ="HD AV book"/> <asp:checkbox runat="Server"ID ="Type4"Font-size="Large"Text ="Business Office Book"/> <asp:checkbox runat="Server"ID ="Type5"Font-size="Large"Text ="the All-round student"/> <asp:checkbox runat="Server"ID ="Type6"Font-size="Large"Text ="Lightweight and Portable book"/> <asp:customvalidator id="CustomValidator1"runat="Server"Forecolor="Red"Errormessage="can't be empty! "clientvalidationfunction="Checkisnotnull"></asp:customvalidator> </td>
Show Results:
A method of Asp:checkbox verifying non-null