我有一个将项目添加到我的arraylist的功能。我的问题是它每次只保留一个项目,因为每次单击我的按钮时它都会重新初始化阵列。如果尚未创建数组,VB中的语法只是初始化?
Dim itemSelectAs 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 = "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 = (ItemSelected.Count - 1)
'Next
Try
conn.Open()
reader = comm.ExecuteReader()
ViewCartlink.Text = "View Cart: (" & ItemSelected.Count & ")"
Finally
conn.Close()
End Try
End Sub
答案 0 :(得分:3)
首先,您需要确定数组列表的尺寸。
Dim array_list as ArrayList()
然后你可以实例化一个
array_list = new ArrayList
或者您可以将其合并为一步:
Dim array_list = new ArrayList()
之后,您可以使用
添加和删除数组列表中的元素 array_list.add(obj)
并删除
array_list.remove(obj)
看起来你的问题与访问arraylist的成员有关。新项目总是添加到arraylist的末尾。要直接访问它们,您需要它们的索引。如果您知道要访问的项目的索引,请使用
array_list(i)
如果不这样做,则需要遍历数组。要做到这一点,你有两个选择。您可以使用“for each”或者您可以使用普通for循环并使用array_list.count作为上限。
每次调用按钮单击菜单时,都会重新创建两个会话值。您需要将它们从Session变量中拉出来并将它们放在局部变量中并将它们放回到会话变量中。
您的按钮方法应为:
Public Sub ShtickDataList_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles ShtickDataList.ItemCommand
if isNothing(itemSelect) Then itemSelect = New ArrayList()
if isNothing(itemQtyOrdered) Then itemQtyOrdered= New ArrayList()
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
将全局通话更改为:
Dim itemSelect As ArrayList() = Session("itemInCart")
Dim Quantities As New ArrayList() = Session("quantities")
答案 1 :(得分:2)
在按钮单击事件之外定义数组。 (表格级别)
然后在按钮点击事件中尝试:
If myArrayList Is Nothing then
'initializes the array list only if that hasn't happened yet
myArrayList = new ArrayList
End If
'adds the item to the existing list without causing it to reintialize
myArrayList.add(item)
如果它没有被初始化,那么它就会被初始化,但如果它已经存在则被初始化。如果它在表单级别初始化,即...已声明为new,那么您可以添加它。
基本上确保你没有在按钮点击事件中为arrayList调用New。
编辑网络表单:
您应该检查初始化arrayList的位置。就像在Page_Load:
中一样If Not Page.IsPostBack Then
myArrayList = New ArrayList
End If