I am using the code below to try and get a list of the items that are in the listbox. So there could be any number of items/rows and they are a 10 digit number. When I use the code below the "NPIListBox.Items.Count" only returns a count of 1 even when there are 3 items/rows in the list box. Any idea why I can get an accurate count when Next is clicked? My goal is to pass all items in the list box into a session so that I can use the values on another page. Thanks!
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
End Sub
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);
}
Since you're adding the options with JS, your server doesn't know this. When you post the values back, they didn't exist at run-time so it grabs the original count (1).
You'll have to use another method to get the newly added values, example: add them to a hidden field with runat='server'
and grab the values from that.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.