RequiredFieldValidatorForCheckBoxLists Demo

This demo illustrates the custom validation control RequiredFieldValidatorForCheckBoxLists, which is designed to validate CheckBoxLists (the normal RequiredFieldValidator cannot validate such Web controls, unfortunately). Try submitting the form without any of the checkboxes checked. Note that an error message is displayed.



 


Source Code
<%@ Register TagPrefix="CustomValidators" Namespace="CustomValidators" 
                        Assembly="filename_of_DLL_file" %>
<HTML>
  <body>
    <form runat="server">
      <asp:CheckBoxList id="CBList" runat="server">
        <asp:ListItem Value="Hello" />
        <asp:ListItem Value="World!" />
      </asp:CheckBoxList>

      <CustomValidators:RequiredFieldValidatorForCheckBoxLists 
              id="reqCBList" runat="server" ControlToValidate="CBList" 
              ErrorMessage="Please select at least one checkbox..." />
      
      <asp:Button id="btnSubmit"  runat="server" Text="Submit" OnClick="btnSubmit_Click" />      
    </form>
  </body>
</HTML>

<script language="vb" runat="server">
  Sub btnSubmit_Click(sender as Object, e as EventArgs)
    If Page.IsValid then
      Response.Write("<i>You have selected at least one checkbox!</i>")
    End If
  End Sub
</script>


Return to the article