Check if at least one checkbox is selected

I have a checkboxlist and I would like to check if at least one checkbox is checked. If none of them are installed, I want to show a warning message that says, please select at least one item. I want to do this in code, if possible. I started, but I don’t know if it is right or not, but I couldn’t finish it.

public void alert() { foreach (ListItem listItem in cblCustomerList.Items) { if (!listItem.Selected) { } } } 

here is the checkboxlist in aspx:

  <asp:CheckBoxList ID="cblCustomerList" runat="server" DataSourceID="SqlDataSource1" CssClass="CheckBoxList" DataTextField="GroupName" DataValueField="GroupName" onclick="readCheckBoxList()" > </asp:CheckBoxList> 

here is the button:

  <asp:Button ID="Button1" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" OnClientClick="return Validate_Checkbox()" /> 

Thank you for your help.

+6
source share
8 answers

Edit:

Here is one sample code. his work with me

Must add this script file: <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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 src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script type="text/javascript"> function Validate_Checkbox() { var chks = $("#<%= cblCustomerList.ClientID %> input:checkbox"); var hasChecked = false; for (var i = 0; i < chks.length; i++) { if (chks[i].checked) { hasChecked = true; break; } } if (hasChecked == false) { alert("Please select at least one checkbox..!"); return false; } return true; } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:CheckBoxList ID="cblCustomerList" runat="server" CssClass="CheckBoxList"> <asp:ListItem Value="0">xx</asp:ListItem> <asp:ListItem Value="1">yy</asp:ListItem> </asp:CheckBoxList> <asp:Button ID="xx" runat="server" OnClientClick="javascript:Validate_Checkbox();return true;" /> </div> </form> </body> </html> 

and you change

 <asp:CheckBoxList ID="cblCustomerList" runat="server" DataSourceID="SqlDataSource1" CssClass="CheckBoxList" DataTextField="GroupName" DataValueField="GroupName"> </asp:CheckBoxList> 

instead of my sample code. and call the javascript function in the control.look button my sample code.

Chairs!!!

Edit

add this script file

 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> 

Instead of <script src="scripts/jquery-1.8.3.min.js" type="text/javascript"></script>

+6
source
 if(cblCustomerList.Items.Cast<ListItem>().Any(item => item.Selected)) { // at least one selected } 
+9
source

Try it;

 boolean b = cblCustomerList.Items.Cast<ListItem>().Any(i => i.Selected) 

if b is true , at least one of them is selected in your list.

Remember to use the System.Linq namespace.

+4
source
 // using System.Linq; // Considering that items are of ListItem type, otherwise use Cast<ListItem>() if (!cblCustomerList.Items.Any(i => i.Selected)) { // TODO: Warn an user } 
+3
source

You just use cblCustomerList.SelectedItem == null , SelectedItem will return the lowest order element in the list of marked items, so if nothing is returned, nothing will be checked.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkboxlist.aspx

+3
source
  if(! cblCustomerList.Items.Cast<ListItem>().AsParallel().Any(i => i.Selected)) { ShowAlert("Please select at least one option"); } 
+1
source

jQuery

 if (!$(".CheckBoxList").find("input:checked").length) { alert("Houston, we've had a problem!"); } 
+1
source

The easiest way I can think of ....

 public void alert() { int i=0; foreach (ListItem listItem in cblCustomerList.Items) { if (listItem.Selected) { i++; } } if(i !=0) { Response.Write("Please check at least one option"); } } } 
0
source

All Articles