How I verify at-least one is checked in checkedlistbox in VB Codebhine Asp.net

Aypn CNN 446 Reputation points
2021-06-06T02:46:58.5+00:00

Hi,

How I check at-least one is checkbox is checked in CheckBoxList control, I want to verify this in VB Code-Behind after clicking Button (Asp.net)

<asp:CheckBoxList ID="CBL_DD" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Text="D1" Value="1" />
<asp:ListItem Text="D2" Value="2" />
<asp:ListItem Text="D3" Value="3" />
<asp:ListItem Text="D4" Value="4" />
<asp:ListItem Text="D5" Value="5" />
</asp:CheckBoxList>

<asp:Button ID="Btn_Verify_ChkBoxList" runat="server"  Text="VerifyChkList" Width="60px" />

Thanks in advance.

Developer technologies | ASP.NET | Other
{count} votes

1 answer

Sort by: Most helpful
  1. Yijing Sun-MSFT 7,096 Reputation points
    2021-06-07T06:50:45.777+00:00

    Hi @Aypn CNN ,
    If you want to check if there is at-least one checked, you could check if SelectedIndex is equal with -1. And if you want to know how many listboxes are checked,you could use count.
    Just like this:

    For i As Integer = 0 To CBL_DD.Items.Count - 1  
        Dim b As Boolean = (If((CBL_DD.SelectedIndex <> -1), True, False))  
        lblMessage.Text = b.ToString()  
    Next  
    

    Best regards,
    Yijing Sun


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.