0

我试图在单击按钮时增加我的变量。它只增加一次。它似乎在重新加载页面时迷路了。

我正在使用以下代码:

Dim ItemSelect As New ArrayList()
Dim Quantities As New ArrayList()
Dim itemQtyOrdered As Integer

Public Sub ShtickDataList_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles ShtickDataList.ItemCommand

    If e.CommandName = "ViewCart" Then
        Response.Redirect("~/ShoppingCart.aspx")
    End If

    If e.CommandName = "addToCart" Then
        Dim itemQuantity As DropDownList = e.Item.FindControl("QuantityDropDown")
        itemQtyOrdered = itemQuantity.SelectedValue
        ItemSelect.Add(e.CommandArgument)
        Quantities.Add(itemQtyOrdered)

        Session("itemInCart") = ItemSelect
        Session("quantities") = Quantities

        viewInvoice()
    End If

End Sub

Protected Sub viewInvoice()

    Dim itemSelected As ArrayList = DirectCast(Session("itemInCart"), ArrayList)
    Dim QuantityofItem As ArrayList = DirectCast(Session("quantities"), ArrayList)
    Dim conn As SqlConnection
    Dim comm As SqlCommand
    Dim reader As SqlDataReader
    Dim purimConnection2 As String = ConfigurationManager.ConnectionStrings("Purim").ConnectionString
    conn = New SqlConnection(purimConnection2)

    comm = New SqlCommand("SELECT ProductName FROM Products WHERE ProductID = @ProductID", conn)

    Dim i As Integer
    For i = 0 To ItemSelect.Count - 1
        comm.Parameters.Add("@ProductID", Data.SqlDbType.Int)
        comm.Parameters("@ProductID").Value = ItemSelect(i)
    Next

    Try
        conn.Open()
        reader = comm.ExecuteReader()
        ViewCartlink.Text = "View Cart: (" & ItemSelect.Count & ")"
    Finally
        conn.Close()
    End Try
End Sub
4

1 回答 1

1

Ah, you may be referring to ItemSelect and Quantities lists. You need to look for them in Session and only create them if they are not in the Session. I am rusty on VB.NET, so this is C# version. In Page_Load:

ItemSelect = (ArrayList)Session["itemInCart"];
if (ItemSelect == null)
{
  ItemSelect = new ArrayList();
  Session["itemInCart"] = ItemSelect;
}

and the same for Quantities.

Also, your loop in viewInvoice method is wrong. For more than one item in ItemSelect list you are adding multiple parameters with the same name. You probably only wanted to do it once with

comm.Parameters("@ProductID").Value = ItemSelect(ItemSelect.Count - 1)
于 2013-02-28T16:34:46.257 回答