if statements inside of for each blocks

  • Thread starter Thread starter Andy B
  • Start date Start date
A

Andy B

I have the code below. It checks to see if there are any selected items in a
GridView displayed in wizardstep 1. It then shows the results of the
selection in step 2. I put this in the wizard.NextButton_Click event so I
can test for previous/next wizard steps. Everything works good except what
happens when nothing is selected. I want it to show a list item in an
unordered list the items that are selected (works good). If nothing is
selected, it should say that nothing is selected in an unordered listitem.
What I get instead is the "No news articles were selected" as well as the
items that were selected when there are selected items. Is there any way to
fix this problem?

Code:
Dim StartList As String = "<UL>"

Dim EndList As String = "</UL>"

Dim ItemsInList As String = ""

' Iterate through the NewsToDelete.Rows property

For Each row As GridViewRow In NewsToDeleteGridView.Rows

' Access the CheckBox

Dim cb = DirectCast(row.FindControl("SelectCheckBox"), CheckBox)

If cb IsNot Nothing AndAlso cb.Checked Then

' Delete row! (Well, not really...)

' First, get the News ID for the selected row

Dim NewsID As New
Guid(NewsToDeleteGridView.DataKeys(row.RowIndex).Value.ToString())

' "Delete" the row

ItemsInList += String.Format( _

"<li>{0}({1})</li>", row.Cells(1).Text, NewsID)

'*** put delete code here [for future reference]

Else

ItemsInList = "<LI>No news articles were selected</LI>"

End If

Next

NewsToDeleteLabel.Text = StartList + ItemsInList + EndList
 
Andy said:
I have the code below. It checks to see if there are any selected items in a
GridView displayed in wizardstep 1. It then shows the results of the
selection in step 2. I put this in the wizard.NextButton_Click event so I
can test for previous/next wizard steps. Everything works good except what
happens when nothing is selected. I want it to show a list item in an
unordered list the items that are selected (works good). If nothing is
selected, it should say that nothing is selected in an unordered listitem.
What I get instead is the "No news articles were selected" as well as the
items that were selected when there are selected items. Is there any way to
fix this problem?

There is a logical error in how you determine if there are any selected
items or not. Instead of determining this after the loop, you determine
it for each item based only on that single item. This means that when an
item is not selected you throw away the information about the selected
items earlier in the list.

Pseudo code for what you do is:

for each item in list
if item is selected
add item to result
else
set result to "no items"
display result

Instead you should determine if no items is selected after the loop:

for each item in list
if item is selected
add item to result
if result is empty
set result to "no items"
 
I ended up putting a boolean test flag in the code to test if anything has
been selected or not. Works good now.
 
Back
Top