private void AddToShoppingCart(int ProductID) { if (Request.Cookies["ShoppingCart"] == null) { HttpCookie oCookie = new HttpCookie("ShoppingCart"); //Set Cookie to expire in 3 hours oCookie.Expires = DateTime.Now.AddHours(3); oCookie.Value = ProductID.ToString(); Response.Cookies.Add(oCookie); } else { bool bExists = false; char[] sep = { ',' }; HttpCookie oCookie = (HttpCookie)Request.Cookies["ShoppingCart"]; //Set Cookie to expire in 3 hours oCookie.Expires = DateTime.Now.AddHours(3); //Check if Cookie already contain same item string sProdID = oCookie.Value.ToString(); string[] arrCookie = sProdID.Split(sep); for (int i = 0; i < arrCookie.Length; i++) { if (arrCookie[i].Trim() == ProductID.ToString().Trim()) { bExists = true; } } if (!bExists) { if (oCookie.Value.Length == 0) { oCookie.Value = ProductID.ToString(); } else { oCookie.Value = oCookie.Value + "," + ProductID; } } //Add back into the Response Objects. Response.Cookies.Add(oCookie); } }
<asp:GridView ID="grdCart" runat="server" AutoGenerateColumns="False" DataKeyNames="ItemID" CellPadding="4" ForeColor="Black" GridLines="Horizontal" Height="260px" Width="856px" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("ImageUrl","~/Images\\{0}") %>'/> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="ProductName" HeaderText="Product" ReadOnly="True" /> <asp:BoundField DataField="Quantity" HeaderText="Quantity" /> <asp:BoundField DataField="Price" DataFormatString="{0:c}" HeaderText="Price" ReadOnly="True" /> <asp:BoundField DataField="SubTotal" DataFormatString="{0:c}" HeaderText="Total" ReadOnly="True" /> <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" /> </Columns> <EmptyDataTemplate> Your Shopping Cart is empty, add items <a href="Itemss.aspx">Add Products</a> </EmptyDataTemplate> <FooterStyle BackColor="#CCCC99" ForeColor="Black" /> <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" /> <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" /> <SortedAscendingCellStyle BackColor="#F7F7F7" /> <SortedAscendingHeaderStyle BackColor="#4B4B4B" /> <SortedDescendingCellStyle BackColor="#E5E5E5" /> <SortedDescendingHeaderStyle BackColor="#242121" /> </asp:GridView>
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)