Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法返回列表框中的项目_Javascript_Asp.net_Vb.net - Fatal编程技术网

Javascript 无法返回列表框中的项目

Javascript 无法返回列表框中的项目,javascript,asp.net,vb.net,Javascript,Asp.net,Vb.net,我正在使用下面的代码尝试获取列表框中的项目列表。因此可以有任意数量的项目/行,它们是10位数字。使用“NPIListBox.Items.Count”下面的代码时,即使列表框中有3个项目/行,也只返回1的计数。你知道为什么单击“下一步”时我可以得到准确的计数吗?我的目标是将列表框中的所有项目传递到会话中,以便我可以使用另一页上的值。谢谢 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Hand

我正在使用下面的代码尝试获取列表框中的项目列表。因此可以有任意数量的项目/行,它们是10位数字。使用“NPIListBox.Items.Count”下面的代码时,即使列表框中有3个项目/行,也只返回1的计数。你知道为什么单击“下一步”时我可以得到准确的计数吗?我的目标是将列表框中的所有项目传递到会话中,以便我可以使用另一页上的值。谢谢

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);

  }

函数getSelected(源、事件参数){
var s=$get(“”).value;
var opt=document.createElement(“选项”);
opt.text=s.substring(s.length-10);
opt.value=s.substring(s.length-10);
document.getElementById(“”).options.add(opt);
}

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


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

我不知道将有多少个值,可能是20、30等等。。。因此,我不知道要创建多少隐藏字段如果我使用文本框而不是列表框,没有隐藏字段可以实现吗?@user1342164--您只需要一个隐藏字段,用逗号或冒号分隔,然后在服务器上,将大字符串拆分!