Asp.net 将通过会话传递的数组写入标签isn';行不通

Asp.net 将通过会话传递的数组写入标签isn';行不通,asp.net,vb.net,Asp.net,Vb.net,我试图获取一个字符串数组(从上一页的列表框中填充并通过会话传递),并将其显示在标签中,这就是我获取数组的方式: Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles CheckOut.Click Dim x = ListBox1.GetSelectedIndices.Count Dim ListPNames(x) As String Dim i As Integer i

我试图获取一个字符串数组(从上一页的列表框中填充并通过会话传递),并将其显示在标签中,这就是我获取数组的方式:

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles CheckOut.Click
    Dim x = ListBox1.GetSelectedIndices.Count
    Dim ListPNames(x) As String
    Dim i As Integer

    i = 0
    For Each item As String In ListBox1.GetSelectedIndices
        ListPNames(i) = (ListBox1.SelectedItem).ToString
        i = i + 1

    Next


    Session("SlctdPhones") = ListPNames(x)

    Response.Redirect("CheckOut.aspx")


End Sub
这就是我试图显示它的方式:

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load


    Dim SlctdPhones() As String = CType(Session.Item("SlctdPhones"), Array)
    Dim i As Integer

    Label3.Text = ""

    For i = 0 To SlctdPhones.Length - 1

    Label3.Text += SlctdPhones(i).ToString() + Environment.NewLine
    Next


End Sub
它给了我一个错误:对象引用未设置为对象的实例。当它到达SlctdPhones时。长度-1行!!
我不知道如何修复它,我的数组代码是否正确(是否所有内容都正确存储在其中?

在显示页面上,使用文字而不是标签

Dim SlctdPhones() As String = CType(Session.Item("SlctdPhones"), Array)
Dim result as String = string.Join("<br>", SlctdPhones) 'Instead of <br> try Environment.NewLine as well 
YourLitetal = result 
Dim SlctdPhones()作为String=CType(Session.Item(“SlctdPhones”),数组)
将结果变暗为String=String.Join(“
”,SlctdPhones)”,而不是
请尝试Environment.NewLine YourLittal=结果

希望这有帮助

您为循环声明
,如下所示:

For Each item In ...
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles CheckOut.Click
    Dim PNames As New List(Of String)()
    For Each index As Integer In ListBox1.GetSelectedIndices
        PNames.Add(ListBox1.Items(index).Value)
    Next
    Session("SlctdPhones") = PNames

    Response.Redirect("CheckOut.aspx")
End Sub
但是不要在循环体中使用
变量。相反,您将继续使用相同的
SelectedItem
属性。您希望将整个方法更改为如下所示:

For Each item In ...
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles CheckOut.Click
    Dim PNames As New List(Of String)()
    For Each index As Integer In ListBox1.GetSelectedIndices
        PNames.Add(ListBox1.Items(index).Value)
    Next
    Session("SlctdPhones") = PNames

    Response.Redirect("CheckOut.aspx")
End Sub
有了该选项,页面加载可以执行以下操作:

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim SlctdPhones As List(Of String) = TryCast(Session.Item("SlctdPhones"), List(Of String))

    If SlctdPhones Is Nothing OrElse SlctdPhones.Length = 0 Then
        'Something went wrong here!
        Return
    End If

    Label3.Text = String.Join("<br/>", SlctdPhones.ToArray())
End Sub

可能相关:但是数组已经声明并从上一页获取了值,为什么我仍然得到错误?!这就是我不理解的。如果你不能通过调试器自己判断数组是否有正确的数据,你需要回到这里的基础知识。另一个选项可能是,你只需在第一页(而不是数组)创建一个带分隔符的字符串,将其传递到下一页,然后使用文字显示。YourLiteral=Session(“DelimitedSelectedItem”).ToString()感谢这两个选项,并最终使用了列表视图,因为它使用起来更好更干净。谢谢!