我正在使用下面的代码来尝试获取列表框中的项目列表。所以可能有任意数量的项目/行,它们是一个 10 位数字。当我使用“NPIListBox.Items.Count”下面的代码时,即使列表框中有 3 个项目/行,也只会返回 1 的计数。知道为什么单击 Next 时我可以得到准确的计数吗?我的目标是将列表框中的所有项目传递到会话中,以便我可以在另一个页面上使用这些值。谢谢!
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
使用以下 javascript 代码填充列表框
<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);
}