//******************************************************* // // The RegisterBtn_Click event handler is used on this page to // add a new user into the Commerce Starter Kit Customers database. // // The event handler then migrates any items stored in the user's // temporary (non-persistent) shopping cart to their // permanent customer account, adds a cookie to the client // (so that we can personalize the home page's welcome // message), and then redirects the browser back to the // originating page. // //******************************************************* private void RegisterBtn_Click(object sender, System.Web.UI.ImageClickEventArgs e) { // Only attempt a login if all form fields on the page are valid if (Page.IsValid == true) { // Store off old temporary shopping cart ID AWC.BusinessLayer.ShoppingCart shoppingCart = new AWC.BusinessLayer.ShoppingCart(); String tempCartId = shoppingCart.GetShoppingCartId(); // Add New Customer to CustomerDB database AWC.BusinessLayer.Customers accountSystem = new AWC.BusinessLayer.Customers(); String customerId = accountSystem.AddCustomer(Name.Text, Email.Text, AWC.BusinessLayer.Security.Encrypt(Password.Text)); if (customerId != "") { // Set the user's authentication name to the customerId FormsAuthentication.SetAuthCookie(customerId, false); // Migrate any existing shopping cart items into the permanent shopping cart shoppingCart.MigrateCart(tempCartId, customerId); // Store the user's fullname in a cookie for personalization purposes Response.Cookies["ASPNETCommerce_FullName"].Value = Server.HtmlEncode(Name.Text); // Redirect browser back to shopping cart page Response.Redirect("ShoppingCart.aspx"); } else { MyError.Text = "Registration failed: That email address is already registered.<br><img align=left height=1 width=92 src=images/1x1.gif>"; } } }
//******************************************************* // // The Page_Load event on this page is used to add the // identified product to the user's shopping cart, and then immediately // redirect to the shoppingcart page (this avoids problems were a user hits // "refresh" and accidentally adds another product to the cart) // // The product to add to the cart is specified using // a querystring argument to the page. // //******************************************************* private void Page_Load(object sender, System.EventArgs e) { if (Request.Params["ProductID"] != null) { AWC.BusinessLayer.ShoppingCart cart = new AWC.BusinessLayer.ShoppingCart(); // Obtain current user's shopping cart ID String cartId = cart.GetShoppingCartId(); // Add Product Item to Cart cart.AddItem(cartId, Int32.Parse(Request.Params["ProductID"]), 1); } Response.Redirect("ShoppingCart.aspx"); }
//******************************************************* // // The Page_Load event on this page is used to load the // ShoppingCart DataGrid *the first time* the page is // accessed. // // Note that subsequent postbacks to the page *do not* // reload the Datagrid. Instead, we rely on the control's // built-in viewstate management to rebuild the control // on the server. // //******************************************************* private void Page_Load(object sender, System.EventArgs e) { if (Page.IsPostBack == false) { // Calculate end-user's shopping cart ID AWC.BusinessLayer.ShoppingCart cart = new AWC.BusinessLayer.ShoppingCart(); String cartId = cart.GetShoppingCartId(); // Populate datagrid with shopping cart data MyDataGrid.DataSource = cart.GetItems(cartId); MyDataGrid.DataBind(); // Update total price label TotalLbl.Text = String.Format( "{0:c}", cart.GetTotal(cartId)); } }
void UpdateShoppingCartDatabase() { AWC.BusinessLayer.ShoppingCart cart = new AWC.BusinessLayer.ShoppingCart(); // Obtain current user's shopping cart ID String cartId = cart.GetShoppingCartId(); // Iterate through all rows within shopping cart list for (int i = 0; i < MyList.Items.Count; i++) { // Obtain references to row's controls TextBox quantityTxt = (TextBox)MyList.Items[i].FindControl("Quantity"); CheckBox remove = (CheckBox)MyList.Items[i].FindControl("Remove"); // Wrap in try/catch block to catch errors in the event that someone types in // an invalid value for quantity int quantity; try { quantity = Int32.Parse(quantityTxt.Text); // If the quantity field is changed or delete is checked if (quantity != (int)MyList.DataKeys[i] || remove.Checked == true) { Label lblProductID = (Label)MyList.Items[i].FindControl("ProductID"); if (quantity == 0 || remove.Checked == true) { cart.RemoveItem(cartId, Int32.Parse(lblProductID.Text)); } else { cart.UpdateItem(cartId, Int32.Parse(lblProductID.Text), quantity); } } } catch { MyError.Text = "There has been a problem with one or more of your inputs."; } } }
protected void CheckoutBtn_Click(object sender, ImageClickEventArgs e) { // Update Shopping Cart UpdateShoppingCartDatabase(); // If cart is not empty, proceed on to checkout page AWC.BusinessLayer.ShoppingCart cart = new AWC.BusinessLayer.ShoppingCart(); // Calculate shopping cart ID String cartId = cart.GetShoppingCartId(); // If the cart isn't empty, navigate to checkout page if (cart.GetItemCount(cartId) != 0) { Response.Redirect("Checkout.aspx"); } else { MyError.Text = "You can't proceed to the Check Out page with an empty cart."; } }
//******************************************************* // // The LoginBtn_Click event is used on this page to // authenticate a customer's supplied username/password // credentials against a database. // // If the supplied username/password are valid, then // the event handler adds a cookie to the client // (so that we can personalize the home page's welcome // message), migrates any items stored in the user's // temporary (non-persistent) shopping cart to their // permanent customer account, and then redirects the browser // back to the originating page. // //******************************************************* private void LoginBtn_Click(object sender, System.Web.UI.ImageClickEventArgs e) { // Only attempt a login if all form fields on the page are valid if (Page.IsValid == true) { // Save old ShoppingCartID AWC.BusinessLayer.ShoppingCart shoppingCart = new AWC.BusinessLayer.ShoppingCart(); String tempCartID = shoppingCart.GetShoppingCartId(); // Attempt to Validate User Credentials using CustomersDB AWC.BusinessLayer.Customers accountSystem = new AWC.BusinessLayer.Customers(); String customerId = accountSystem.Login(email.Text, AWC.BusinessLayer.Security.Encrypt(password.Text)); if (customerId != null) { // Migrate any existing shopping cart items into the permanent shopping cart shoppingCart.MigrateCart(tempCartID, customerId); // Lookup the customer's full account details AWC.Entities.Customer customerDetails = accountSystem.GetCustomerDetails(customerId); // Store the user's fullname in a cookie for personalization purposes Response.Cookies["ASPNETCommerce_FullName"].Value = customerDetails.FullName; // Make the cookie persistent only if the user selects "persistent" login checkbox if (RememberLogin.Checked == true) { Response.Cookies["ASPNETCommerce_FullName"].Expires = DateTime.Now.AddMonths(1); } // Redirect browser back to originating page FormsAuthentication.RedirectFromLoginPage(customerId, RememberLogin.Checked); } else { Message.Text = "Login Failed!"; } } }
void PopulateShoppingCartList() { AWC.BusinessLayer.ShoppingCart cart = new AWC.BusinessLayer.ShoppingCart(); // Obtain current user's shopping cart ID String cartId = cart.GetShoppingCartId(); // If no items, hide details and display message if (cart.GetItemCount(cartId) == 0) { DetailsPanel.Visible = false; MyError.Text = "There are currently no items in your shopping cart."; } else { // Databind Gridcontrol with Shopping Cart Items MyList.DataSource = cart.GetItems(cartId); MyList.DataBind(); //Update Total Price Label lblTotal.Text = String.Format("{0:c}", cart.GetTotal(cartId)); } }
//******************************************************* // // The SubmitBtn_Click event handle is used to order the // items within the current shopping cart. It then // displays the orderid and order status to the screen // (hiding the "SubmitBtn" button to ensure that the // user can't click it twice). // //******************************************************* private void SubmitBtn_Click(object sender, System.Web.UI.ImageClickEventArgs e) { AWC.BusinessLayer.ShoppingCart cart = new AWC.BusinessLayer.ShoppingCart(); // Calculate end-user's shopping cart ID String cartId = cart.GetShoppingCartId(); // Calculate end-user's customerID String customerId = User.Identity.Name; if ((cartId != null) && (customerId != null)) { // Place the order AWC.BusinessLayer.Orders ordersDatabase = new AWC.BusinessLayer.Orders(); int orderId = ordersDatabase.PlaceOrder(customerId, cartId); //Update labels to reflect the fact that the order has taken place Header.Text="Check Out Complete!"; Message.Text = "<b>Your Order Number Is: </b>" + orderId; SubmitBtn.Visible = false; } }