Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with Multi Select Listbox

Status
Not open for further replies.

MayoorPatel

Programmer
Apr 10, 2006
35
GB
I have a multi select listbox on a form which posts to the server and then returns to the form prepoulated with the items it just posted.

I have the following code to populate the dropdown (subcategory) but for some reason it only highlights the first row, when actually ive selected 4 or 5 options.

Code:
Dim i As Integer
            Dim arySelectedSubcategoryItems() As String
            If Not Request(subcategory.ID.ToString).Equals(String.Empty) Then
                arySelectedSubcategoryItems = Request(subcategory.ID.ToString).ToString().Split(",")

                For Each i In arySelectedSubcategoryItems
                    For Each item As ListItem In subcategory.Items
                        If Not (i.Equals(String.Empty)) Then
                            subcategory.Items.FindByValue(i).Selected = True
                        End If
                    Next
                Next
            End If

Can anyone help?
 
For Each i In arySelectedSubcategoryItems ???

i is Integer.
 
i is integer as TipGiver has pointed out.
Try
Dim i as ListItem
 
Ok have changed the i to type ListItem

and it's now got a problem with the array "arySelectedSubcategoryItems". It's saying "Value of type string cannot be converted to System.Web.UI.Webcontrols.ListItem"

and it underlines "arySelectedSubcategoryItems" on the line For Each i In arySelectedSubcategoryItems

Code:
Dim i As ListItem
            Dim arySelectedSubcategoryItems() As String
            subcategory.SelectionMode = ListSelectionMode.Multiple

            If Not Request(subcategory.ID.ToString).Equals(String.Empty) Then
                arySelectedSubcategoryItems = Request(subcategory.ID.ToString).ToString().Split(",")

                For Each i In arySelectedSubcategoryItems
                    For Each item As ListItem In subcategory.Items
                        If Not (i.Equals(String.Empty)) Then
                            subcategory.Items.FindByValue(i).Selected = True
                        End If
                    Next
                Next
            End If

 
My mistake.. you want to loop throgh the array...?

Dim i as integer

Instead of For Each.. use
For i = 0 to arySelectedSubcategoryItems.Length - 1
...
Next i

 
ok tried that no its saying

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.


the line the error occurs on is

subcategory.Items.FindByValue(i).Selected = True
 
Then it is not finding a value in the list with the value of "i". You need to debug that code as it is looping though to see what the problem is.
 
Right the reason why its failing is because its trying to compare 0 which is what the array loop starts on with a listbox item reference which doesnt exist.

the listboxitems selected are

119
120
 
Try:
Code:
subcategory.Items.FindByValue(arySelectedSubcategoryItems(i)).Selected = True
 
Nope thats now giving

Index was outside the bounds of the array.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IndexOutOfRangeException: Index was outside the bounds of the array.

Source Error:

subcategory.Items.FindByValue(arySelectedSubcategoryItems(i)).Selected = True
 
you really need to debug and step throught the code to find the problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top