尝试分配会话变量时按钮停止运行

Button Stops Functioning when Trying to Assign Session Variable

我试图在按下按钮后保留一个字符串列表,并且一直在使用会话变量。这是代码片段:

.aspx 页面

<asp:Panel runat="server" ID="pnlAdd" >
     <asp:DropDownList runat="server" ID="ddlOrder" >
          <asp:ListItem Value="-1">Select an Order Type</asp:ListItem>
          <asp:ListItem Value="1">hey there</asp:ListItem>
     </asp:DropDownList>
     <asp:DropDownList runat="server" ID="ddlOrderStatus" >
          <asp:ListItem Value="-1">Select an Order Status</asp:ListItem>
          <asp:ListItem Value="1">another option</asp:ListItem>
     </asp:DropDownList>
</asp:Panel>
<br />
<asp:Button runat="server" ID="btnAdd" Text="Add" />

.vb 页面

Protected Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        Dim list As New List(Of String)

        list = Session("flist")
        list.Add(ddlOrder.SelectedItem.Text + " " + ddlOrderStatus.SelectedItem.Text)
        Session("flist") = list

        For i As Integer = 0 To 10
            MsgBox(i.ToString + "  " + list(i))
        Next

        MsgBox("Here")
 End Sub

当我使用 list = Session("flist")Session 变量分配给 list 时,单击按钮时不会发生任何事情。如果我删除该行,按钮的其余部分将触发。我是否错误地使用了 Session 变量?我怎样才能正确地做到这一点?

在向该列表中添加元素之前,(字符串的)列表不能为空,因此,按如下方式更改代码:

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

    Dim list As New List(Of String)
    Dim objList As Object = Session.Item("flist")

    If objList IsNot Nothing AndAlso TypeOf (objList) Is List(Of String) Then
        list = CType(objList, List(Of String))
    End If

    list.Add(ddlOrder.SelectedItem.Text + " " + ddlOrderStatus.SelectedItem.Text)
    Session("flist") = list

    For i As Integer = 0 To list.Count - 1
        MsgBox(i.ToString + "  " + list(i))
    Next

    MsgBox("Here on end")

End Sub