Hi
i want when no item selected after Submit show this message "Nothing Selected" what should i do ?
Thanks

Protected Sub Btnsub_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Btnsub.Click
        lblresult.Text = "You Chose:<br/>"
        Dim Lstitems As ListItem
        For Each Lstitems In CheckBoxList1.Items
            If Lstitems.Selected Then
                lblresult.Text &= "<br/>" & Lstitems.Text & "<br/>"
            End If
        Next
    End Sub

Recommended Answers

All 4 Replies

Well you need to use status variable to test whether a checkbox item is checked or not.

Dim Lstitems As ListItem
 Dim IsSelected as Boolean=false
 For Each Lstitems In CheckBoxList1.Items
      If Lstitems.Selected Then
           IsSelected=true
           Exit For
      End If
 End 

If Not IsSelected Then
  'statement
End If

It works but another problem exist,when i select apple it shows "You Chose:Apple" but when i select other items it doesn't show anything
Please Help Me
thanks

lblresult.Text = "You Chose:<br/>"
        Dim Lstitems As ListItem
        Dim IsSelected As Boolean
        For Each Lstitems In CheckBoxList1.Items
            If Lstitems.Selected Then
                IsSelected = True
                lblresult.Text &= "<br/>" & Lstitems.Text & "<br/>"
            End If
            Exit For
        Next
        If IsSelected = False Then
            lblresult.Text = "Nothing Selected"

        End If

123mehran,

I mostly use C#, so my VB may be a little rusty, but from what I see, the Exit For statement will be executed on the first iteration of the loop every time. From what I think you are trying to do, simply removing that line should do the trick.

adatapost's post (thats a mouthful!) showed logic to run through all items in the list and exit if any were checked. I believe you are trying to go through all options and collect a list of all that are checked, correct? If so, you would never want to exit th loop until each item has been examined.

Hope that helps.

Hi, Try this

Protected Sub _btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _btnSubmit.Click
        lblResult.Text = "You Chose:<br/>"
        Dim IsSelected = False
        Dim Lstitems As ListItem
        For Each Lstitems In _chkList.Items
            If Lstitems.Selected Then
                lblResult.Text &= "<br/>" & Lstitems.Text & "<br/>"
                IsSelected = True
            End If
        Next
        If IsSelected Then
        Else
            _lblErrMsg.Text = "Nothing to Show"
        End If
    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.