I used a tutorial online to build a shopping cart for my website. (http://csharpdotnetfreak.blogspot.com/2009/05/aspnet-creating-shopping-cart-example.html) The entire cart works perfectly except that the GridView delete link doesn’t do anything. No one commented that they had experienced this issue except me. I have looked over my code and everything seems to be exactly the same as the tutorial.
THe code behind for the cart is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ShoppingCartActions;
using System.Collections.Specialized;
using System.Collections;
using ShoppingCartActions;
public partial class Cart : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Profile.SCart == null)
{
Profile.SCart = new ShoppingCartActions.Cart();
}
if (!Page.IsPostBack)
{
ReBindGrid();
}
if (Profile.SCart.Items == null)
{
TotalLabel.Visible = false;
}
}
protected void grdCart_RowEditing
(object sender, GridViewEditEventArgs e)
{
grdCart.EditIndex = e.NewEditIndex;
ReBindGrid();
}
protected void grdCart_RowUpdating
(object sender, GridViewUpdateEventArgs e)
{
TextBox txtQuantity = (TextBox)
grdCart.Rows[e.RowIndex].Cells[2].Controls[0];
int Quantity = Convert.ToInt32(txtQuantity.Text);
if (Quantity == 0)
{
Profile.SCart.Items.RemoveAt(e.RowIndex);
}
else
{
Profile.SCart.Items[e.RowIndex].Quantity
= Quantity;
}
grdCart.EditIndex = -1;
ReBindGrid();
}
protected void grdCart_RowCancelingEdit
(object sender, GridViewCancelEditEventArgs e)
{
grdCart.EditIndex = -1;
ReBindGrid();
}
[B]protected void grdCart_RowDeleting
(object sender, GridViewDeleteEventArgs e)
{
Profile.SCart.Items.RemoveAt(e.RowIndex);
ReBindGrid();
}[/B] private void ReBindGrid()
{
grdCart.DataSource = Profile.SCart.Items;
DataBind();
TotalLabel.Text = string.Format("Total:{0,19:C}",
Profile.SCart.Total);
}
The cart.aspx file that contains the grid is:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Cart.aspx.cs" Inherits="Cart" %>
<%@ Register Src="CartControl.ascx" TagName="CartControl" TagPrefix="uc1" %>
<!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 id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="grdCart" runat="server"
AutoGenerateColumns="False"
DataKeyNames="ProductID"
OnRowCancelingEdit="grdCart_RowCancelingEdit"
OnRowDeleting="grdCart_RowDeleting"
OnRowEditing="grdCart_RowEditing"
OnRowUpdating="grdCart_RowUpdating"
>
<Columns>
<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"/>
<asp:TemplateField HeaderText="Remove Item">
<ItemTemplate>
<asp:CheckBox id="RemovedProducts" runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
Your Shopping Cart is empty, add items
<a href="Products.aspx">Add Products</a>
</EmptyDataTemplate>
</asp:GridView>
<asp:Label ID="TotalLabel" runat="server"></asp:Label>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Products.aspx">Return to Products Page</asp:HyperLink>
</form>
</body>
</html>
And the shopping cart class file is:
using System;
using System.Collections.Generic;
namespace ShoppingCartActions
{
/// <summary>
/// Summary description for ShoppingCart
/// </summary>
[Serializable]
public class CartItem
{
private int _productID;
private string _productName;
private string _imageUrl;
private int _quantity;
private double _price;
private double _subTotal;
public CartItem()
{
}
public CartItem(int ProductID, string ProductName,
string ImageUrl, int Quantity, double Price)
{
_productID = ProductID;
_productName = ProductName;
_imageUrl = ImageUrl;
_quantity = Quantity;
_price = Price;
_subTotal = Quantity * Price;
}
public int ProductID
{
get
{
return _productID;
}
set
{
_productID = value;
}
}
public string ProductName
{
get { return _productName; }
set { _productName = value; }
}
public string ImageUrl
{
get { return _imageUrl; }
set { _imageUrl = value; }
}
public int Quantity
{
get { return _quantity; }
set { _quantity = value; }
}
public double Price
{
get { return _price; }
set { _price = value; }
}
public double SubTotal
{
get { return _quantity * _price; }
}
}
[Serializable]
public class Cart
{
private DateTime _dateCreated;
private DateTime _lastUpdate;
private List<CartItem> _items;
public Cart()
{
if (this._items == null)
{
this._items = new List<CartItem>();
this._dateCreated = DateTime.Now;
}
}
public List<CartItem> Items
{
get { return _items; }
set { _items = value; }
}
public void Insert(int ProductID, double Price,
int Quantity, string ProductName, string ImageUrl)
{
int ItemIndex = ItemIndexOfID(ProductID);
if (ItemIndex == -1)
{
CartItem NewItem = new CartItem();
NewItem.ProductID = ProductID;
NewItem.Quantity = Quantity;
NewItem.Price = Price;
NewItem.ProductName = ProductName;
NewItem.ImageUrl = ImageUrl;
_items.Add(NewItem);
}
else
{
_items[ItemIndex].Quantity += 1;
}
_lastUpdate = DateTime.Now;
}
public void Update(int RowID, int ProductID,
int Quantity, double Price)
{
CartItem Item = _items[RowID];
Item.ProductID = ProductID;
Item.Quantity = Quantity;
Item.Price = Price;
_lastUpdate = DateTime.Now;
}
public void DeleteItem(int rowID)
{
_items.RemoveAt(rowID);
_lastUpdate = DateTime.Now;
}
private int ItemIndexOfID(int ProductID)
{
int index = 0;
foreach (CartItem item in _items)
{
if (item.ProductID == ProductID)
{
return index;
}
index += 1;
}
return -1;
}
public struct ShoppingCartUpdates
{
public int ProductId;
public int PurchaseQuantity;
public bool RemoveItem;
}
public double Total
{
get
{
double t = 0;
if (_items == null)
{
return 0;
}
foreach (CartItem Item in _items)
{
t += Item.SubTotal;
}
return t;
}
}
}
}
Is there something about working with Gridview’s delete method that I don’t know about?