I have been working on a code snippet to retrieve a list of items from a listbox. The listbox can contain any number of 10-digit numbers in multiple rows. However, when I run the code, the "NPIListBox.Items.Count" function only returns a count of 1, even when there are 3 items in the list box. Can anyone help me understand why this discrepancy occurs? My main objective is to store all listbox items in a session so that I can access the values on another page. Thank you!
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack Then
PopulateListBox()
End If
End Sub
Protected Sub cmdNext_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles cmdNext.Click
Dim n As Integer = NPIListbox.Items.Count
Dim arr As String() = New String(n - 1) {}
For i As Integer = 0 To arr.Length - 1
arr(i) = NPIListbox.Items(i).ToString.Substring(NPIListbox.Items(i).ToString.Length - 10)
Next
Session("arr") = arr
Response.Redirect("~/frmDescription.aspx")
End Sub
Private Sub PopulateListBox()
If DoctorNameTextBox.Text = "" Then
Else
' Get value from text box
Dim textBoxValue As String = Me.DoctorNameTextBox.Text
' Create new item to add to list box
Dim newItem As New ListItem(textBoxValue)
' Add item to list box and set selected index
NPIListbox.Items.Add(newItem)
NPIListbox.SelectedIndex = NPIListbox.Items.Count - 1
End If
The list box is populated using the following javascript code
<script language="javascript" type="text/javascript">
function getSelected(source, eventArgs) {
var s = $get("<%=DoctorNameTextBox.ClientID %>").value;
var opt = document.createElement("option");
opt.text = s.substring(s.length - 10);
opt.value = s.substring(s.length - 10);
document.getElementById('<%= NPIListbox.ClientID %>').options.add(opt);
}