I'm throwing myself into ASP.NET with VB and there is no better way to learn it than by actually doing it. I am currently working on a project where I want to utilize a SQLDataSource control, a ListBox control, a Button control, an array and a constructed URL to post a page.
I'm writing a test page with the SQLDataSource object that populates a ListBox control. I also have a Button control and a second ListBox control. SQLDataSource control is pulling in data from oracle to populate the first ListBox. I would like to add the Selected Items to the second ListBox control once I click the button control. I am using an array to store the selected items which is the size of the number of items in the ListBox control. If my array is of size 20, which is the total number of values in the ListBox, and I select three items from the listbox. Once I click the button, 20 items are added to the 2nd control due to the size of the array. I need to find out how to skip over the array items that are empty so that I do not add empty items to the listbox control. My code is below.
It's fairly straightforward and I understand that I would need to add an IF block in the last For loop of this code block. I am finding out that ASP.NET doesn't like using an IF statement in a For loop. Any thoughts or suggestions on what I am trying to do would be helpful.
I'm writing a test page with the SQLDataSource object that populates a ListBox control. I also have a Button control and a second ListBox control. SQLDataSource control is pulling in data from oracle to populate the first ListBox. I would like to add the Selected Items to the second ListBox control once I click the button control. I am using an array to store the selected items which is the size of the number of items in the ListBox control. If my array is of size 20, which is the total number of values in the ListBox, and I select three items from the listbox. Once I click the button, 20 items are added to the 2nd control due to the size of the array. I need to find out how to skip over the array items that are empty so that I do not add empty items to the listbox control. My code is below.
Code:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'//Clear ListBox1 Items
ListBox1.Items.Clear()
'//ADD ITEMS INTO ARRAY
'//FIRST FIND HOW MANY ITEMS IN LISTBOX AND REDIM THE ARRAY FOR SIZE
Dim ArrayItems(countyListBox.Items.Count) As String
For i = 0 To countyListBox.Items.Count - 1
If countyListBox.Items(i).Selected Then
ArrayItems(i) = countyListBox.Items(i).Value
End If
Next
'//Add Items to ListBox1
For i = 0 To countyListBox.Items.Count
ListBox1.Items.Add(ArrayItems(i))
Next i
End Sub
It's fairly straightforward and I understand that I would need to add an IF block in the last For loop of this code block. I am finding out that ASP.NET doesn't like using an IF statement in a For loop. Any thoughts or suggestions on what I am trying to do would be helpful.