Hi.
I trying to build a cookies cart to add / update item. The quantity and price will be updated accordingly for 1st round.
but item won't be updated but add new in 2nd round.
first attempt: 10001, 1, 5.00
second attempt: 10001, 3, 3.20
third attempt: 10002, 1, 2.80
fouth attempt: 10001, 5, 4.80
HTML
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="CookiesManager.aspx.vb" Inherits="CookiesManager" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddl_ID" runat="server">
<asp:ListItem>10001</asp:ListItem>
<asp:ListItem>10002</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="txt_quantity" runat="server" Text="1"></asp:TextBox>
<asp:TextBox ID="txt_price" runat="server" Text="5.00"></asp:TextBox>
<asp:Button ID="btn_addtocart" runat="server" Text="Add to Cart" />
<asp:Button ID="btn_emptycart" runat="server" Text="Empty Cart" />
</div>
<div>
<asp:GridView ID="gvCart" runat="server" AutoGenerateColumns="true">
</asp:GridView>
</div>
</form>
</body>
</html>
CODE BEHIND
Partial Class CookiesManager
Inherits System.Web.UI.Page
Dim DT As New DataTable
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack = False Then
BindCookiesCart()
End If
End Sub
Public Sub BindCookiesCart()
If Request.Cookies("CookieCart") Is Nothing = False Then
Dim CartList As String = Request.Cookies("CookieCart").Value.ToString()
Dim CartListSplitter As String() = CartList.Split("|"c)
DT = New DataTable()
DT.Columns.Add("ID")
DT.Columns.Add("quantity")
DT.Columns.Add("price")
For Each items As String In CartListSplitter
If Not items = "" Then
Dim item As String() = items.Split(","c)
Dim ID = item(0)
Dim quantity = item(1)
Dim price = item(2)
Dim row As DataRow = DT.NewRow()
row("ID") = ID
row("quantity") = quantity
row("price") = price
DT.Rows.Add(row)
End If
Next
Me.gvCart.DataSource = DT.DefaultView
Me.gvCart.DataBind()
If Me.gvCart.Rows.Count > 0 Then
Me.btn_emptycart.Visible = True
Else
Me.btn_emptycart.Visible = False
End If
End If
End Sub
Public Sub CookieCartAddItem(ByVal listCookie As List(Of String))
Dim CartList As String = String.Join(",", listCookie)
If Request.Cookies("CookieCart") Is Nothing Then
Dim CartCookie As New HttpCookie("CookieCart")
Response.Cookies("CookieCart").Value = CartList
Else
Response.Cookies("CookieCart").Value = Request.Cookies("CookieCart").Value & "|" & CartList
End If
If Request.Cookies("CookieCart").Value <> Response.Cookies("CookieCart").Value Then
Request.Cookies("CookieCart").Value = Response.Cookies("CookieCart").Value
End If
End Sub
Public Sub CookieCartEditItem(ByVal ID As String, ByVal Xquantity As Integer, ByVal Xprice As String)
If Request.Cookies("CookieCart") Is Nothing = False Then
Dim CartList As String = Request.Cookies("CookieCart").Value.ToString()
Dim CartListSplitter As String() = CartList.Split("|"c)
For Each items As String In CartListSplitter
If Not items = "" Then
Dim item As String() = items.Split(","c)
Dim quantity As Integer = Convert.ToInt32(item(1))
Dim price As Double = item(2)
If ID = item(0) Then
quantity += Xquantity
price += Xprice
Dim itemSplitter As String() = items.Split(",")
itemSplitter(1) = quantity
itemSplitter(2) = price
Dim itemSplittedUpdated = itemSplitter
Dim CartListUpdated = CartList.Replace(items, String.Join(",", itemSplittedUpdated))
Response.Cookies("CookieCart").Value = CartListUpdated
End If
End If
Next
End If
End Sub
Private Sub addCookieCart()
Dim ID As String = Me.ddl_ID.SelectedValue
Dim quantity As Integer = Me.txt_quantity.Text
Dim price As Double = Me.txt_price.Text
'start
Dim cartListItems = New List(Of String)() From {
ID,
quantity,
price
}
If Request.Cookies("CookieCart") Is Nothing Then
CookieCartAddItem(cartListItems)
Else
Dim CartList As String = Request.Cookies("CookieCart").Value
Dim CartListSplitter As String() = CartList.Split("|"c)
Dim ItemMatched As Boolean
For Each items As String In CartListSplitter
If Not items = "" Then
Dim item As String() = items.Split(","c)
If item(0) = ID Then
ItemMatched = True
Else
ItemMatched = False
End If
End If
Next
If ItemMatched = False Then
CookieCartAddItem(cartListItems)
Else
CookieCartEditItem(ID, quantity, price)
End If
End If
'end
End Sub
Protected Sub btn_addtocart_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_addtocart.Click
addCookieCart()
Response.Redirect(Request.RawUrl)
End Sub
Protected Sub btn_emptycart_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_emptycart.Click
Dim myCookie As HttpCookie
myCookie = New HttpCookie("CookieCart")
myCookie.Expires = DateTime.Now.AddDays(-1D)
Response.Cookies.Add(myCookie)
Response.Redirect(Request.RawUrl)
End Sub
End Class