I have checkboxlist and I would like to check if at least one checkbox is checked. If none is checked then I want to show alert message that says please select at least one item. I want to do this in code behind if possible. I have started but don't know if it is right or wrong but not able to 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()" />
Thanks for your help.
if(cblCustomerList.Items.Cast<ListItem>().Any(item => item.Selected))
{
// at least one selected
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With