question

AypnCNN-4494 avatar image
0 Votes"
AypnCNN-4494 asked YijingSun-MSFT edited

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

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.

dotnet-aspnet-webpages
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,
I tried this, but I expect your response simply.

 Protected Sub ValiCblist()
    
 For Each Li As ListItem In CBL_DD.Items
 If (Li.Selected) Then
 If Li.Value <> "0" Then
 CboxVal = CboxVal + 1
 Else
 CboxVal = 0
 End If
 lblMessage.Text = CboxVal
 End If
 Next
    
 End sub
0 Votes 0 ·

Hi,

Pls ref my code, I almost completed it.

  Protected Sub Validate_ChboxList()
         Dim count As Integer = 0
         For i As Integer = 0 To CBL_DD.Items.Count - 1
             If CBL_DD.Items(i).Selected = True Then
                 count += 1
             End If
         Next
         lblMessage.Text = count.ToString()
     End Sub
0 Votes 0 ·

1 Answer

YijingSun-MSFT avatar image
0 Votes"
YijingSun-MSFT answered YijingSun-MSFT edited

Hi @AypnCNN-4494,
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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.