I want to create a sub which accepts a dropdownlist and a comma separated string as a parameter and populates the dropdownlist. The code does do the job when I enter the name of the dropdownlist directly, but it doesn't work when I create a separate sub for general use. I think it's because it's not passing the dropdownlist correctly. How can I do this?
Code:
Private Sub fnPopulateDropdownlist(ByVal pdDropdownlist As DropDownList, ByVal psSeparatedItems As String, ByVal psSeparator As String)
Dim str As String = psSeparator
'Declare a arraylist for getting comma separated string
Dim arr As ArrayList = New ArrayList
'check wether the re is comma in the end of the string
If str.Trim.EndsWith(psSeparator) Then
str = str.Substring(0, (str.Length - 1))
End If
'split the comma separated string into arraylist
arr.AddRange(str.Split(psSeparator))
'loop through the arraylist items & add the item to Dropdownlist
Dim i As Integer = 0
Do While (i < arr.Count)
pdDropdownlist.Items.Insert(i, New ListItem(arr(i).ToString, (i + 1).ToString))
i = (i + 1)
Loop
End Sub