public partial class ShoppingCart : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// popluate the control only in the initial page load
if (!IsPostBack)
PopulateControls();
}
// fill the shopping cart controls with data
private void PopulateControls()
{
// display product recommendations
recommendations.LoadCartRecommendations();
// get the items in the shopping cart
DataTable dt = ShoppingCartAccess.GetItems();
// if the shopping cart is empty...
if (dt.Rows.Count == 0)
{
titleLabel.Text = "Your Shopping Cart is empty!";
grid.Visible = false;
updateButton.Enabled = false;
//checkoutButton.Enabled = false;
totalAmountLabel.Text = String.Format("{0:c}", 0);
}
else
// if the shopping cart is not empty...
{
// populate the list with the shopping cart content
grid.DataSource = dt;
grid.DataBind();
// set up controls
titleLabel.Text = "These are the products in your Shopping Cart:";
grid.Visible = true;
updateButton.Enabled = true;
//checkoutButton.Enabled = true;
// display the total amount
decimal amount = ShoppingCartAccess.GetTotalAmount();
totalAmountLabel.Text = String.Format("{0:c}", amount);
}
}
// remove a product from the cart
protected void grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
// index of the row beeing deleted
int rowIndex = e.RowIndex;
// the ID of the product being deleted
string productId = grid.DataKeys[rowIndex].Value.ToString();
// remove the product from the shopping cart
bool success = ShoppingCartAccess.RemoveItem(productId);
// display the status
statusLabel.Text = success ? "Product successfuly removed! " :
"There was an error removing the product! ";
// repopulate the control
PopulateControls();
}
// updat shopping cart product quantities
protected void updateButton_Click(object sender, EventArgs e)
{
// number of rows in the grid view
int rowsCount = grid.Rows.Count;
// will store a row of the gridview
GridViewRow gridRow;
// will reference a quantity textBox in the GridView
TextBox quantityTextBox;
DropDownList editColor;
// variable to store productId and quantity
string productId;
int quantity;
// was the update successful?
bool success = true;
// go through the rows of the gridview
for (int i = 0; i < rowsCount; i++)
{
// get a row
gridRow = grid.Rows[i];
// the Id of the product being updated
productId = grid.DataKeys[i].Value.ToString();
// get the quantity textbox in the row
quantityTextBox = (TextBox)gridRow.FindControl("editQuantity");
editColor = (DropDownList)gridRow.FindControl("editColor");
string attributes = editColor.SelectedValue;
// get the quantity, gaurding against bogus values
if (Int32.TryParse(quantityTextBox.Text, out quantity))
{
// update the product quantity
success = success && ShoppingCartAccess.UpdateItem(productId, quantity, attributes);
}
else
{
// if TryParse did not succeed
success = false;
}
// display the status message
statusLabel.Text = success ?
"Your Shopping Cart was successfuly updated!" :
"Some quantity updates failed! Please verify your cart!";
}
// repopulate the control
PopulateControls();
}
// redirect to login page
protected void loginButton_Click(object sender, EventArgs e)
{
Response.Redirect("Checkout.aspx");
}
// redirect to register page
protected void registerButton_Click(object sender, EventArgs e)
{
Response.Redirect("Register.aspx");
}
}