0

我正在使用下面的代码来尝试获取列表框中的项目列表。所以可能有任意数量的项目/行,它们是一个 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);

  }

4

1 回答 1

1

由于您使用 JS 添加选项,因此您的服务器不知道这一点。当您发回这些值时,它们在运行时并不存在,因此它会获取原始计数 (1)。

您必须使用另一种方法来获取新添加的值,例如:将它们添加到隐藏字段runat='server'并从中获取值。

于 2013-09-13T18:55:39.557 回答