0

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
Attachments Untitled.png 4.09 KB
4
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by MamathaS
0

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
0

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
0

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.

0

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
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.