Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
In this articleby Erik Reitan Download Wingtip Toys Sample Project (C#) or Download E-book (PDF)
This tutorial describes the business logic required to add a shopping cart to the Wingtip Toys sample ASP.NET Web Forms application. This tutorial builds on the previous tutorial "Display Data Items and Details" and is part of the Wingtip Toy Store tutorial series. When you've completed this tutorial, the users of your sample app will be able to add, remove, and modify the products in their shopping cart. What you'll learn:
Code features in this tutorial:
Earlier in this tutorial series, you added pages and code to view product data from a database. In this tutorial, you'll create a shopping cart to manage the products that users are interested in buying. Users will be able to browse and add items to the shopping cart even if they are not registered or logged in. To manage shopping cart access, you will assign users a unique
Note The ASP.NET Session state is a convenient place to store user-specific information which will expire after the user leaves the site. While misuse of session state can have performance implications on larger sites, light use of session state works well for demonstration purposes. The Wingtip Toys sample project shows how to use session state without an external provider, where session state is stored in-process on the web server hosting the site. For larger sites that provide multiple instances of an application or for sites that run multiple instances of an application on different servers, consider using Windows Azure Cache Service. This Cache Service provides a distributed caching service that is external to the web site and solves the problem of using in-process session state. For more information see, How to Use ASP.NET Session State with Windows Azure Web Sites. Add CartItem as a Model ClassEarlier in this tutorial series, you defined the schema for the category and product data by creating the
The The Update the Product ContextIn addition to adding the
As mentioned previously in this tutorial series, the code in the ProductContext.cs file adds the Managing the Shopping Cart Business LogicNext, you'll create the The shopping cart logic that you will add will contain the functionality to manage the following actions:
A shopping cart page (ShoppingCart.aspx) and the shopping cart class will be used together to access shopping cart data. The shopping cart page will display all the items the user adds to the shopping cart. Besides the shopping cart page and class, you'll create a page (AddToCart.aspx) to add products to the shopping cart. You will also add code to the ProductList.aspx page and the ProductDetails.aspx page that will provide a link to the AddToCart.aspx page, so that the user can add products to the shopping cart. The following diagram shows the basic process that occurs when the user adds a product to the shopping cart. When the user clicks the Add To Cart link on either the ProductList.aspx page or the ProductDetails.aspx page, the application will navigate to the AddToCart.aspx page and then automatically to the ShoppingCart.aspx page. The AddToCart.aspx page will add the select product to the shopping cart by calling a method in the ShoppingCart class. The ShoppingCart.aspx page will display the products that have been added to the shopping cart. Creating the Shopping Cart ClassThe
The The The Creating the Add-To-Cart FunctionalityAs mentioned earlier, you will create a processing page named AddToCart.aspx that will be used to add new products to the shopping cart of the user. This page will call the Note You will be modifying the code-behind (AddToCart.aspx.cs) for this page, not the page UI (AddToCart.aspx). To create the Add-To-Cart functionality:
When the AddToCart.aspx page is loaded, the product As previously
mentioned, a user Creating the Shopping Cart UIThe ShoppingCart.aspx page will display the products that the user has added to their shopping cart. It will also provide the ability to add, remove and update items in the shopping cart.
The ShoppingCart.aspx page includes a GridView control named Retrieving the Shopping Cart ItemsNext, you add code to the ShoppingCart.aspx.cs code-behind to retrieve and populate the Shopping Cart UI.
As mentioned above, the Adding Products to the Shopping CartWhen either the ProductList.aspx or the ProductDetails.aspx page is displayed, the user will be able to add the product to the shopping cart using a link. When they click the link, the application navigates to the processing page named AddToCart.aspx. The AddToCart.aspx page will call the Now, you'll add an Add to Cart link to both the
ProductList.aspx page and the ProductDetails.aspx page. This link will include the product
Testing the Shopping CartRun the application to see how you add products to the shopping cart.
Calculating and Displaying the Order TotalIn addition to adding products to the shopping cart, you will add a
First, the Note The above code uses the nullable type " Modify the Shopping Cart DisplayNext you'll modify the code for the ShoppingCart.aspx page to call the
When the ShoppingCart.aspx page loads, it loads the shopping cart object and then retrieves the shopping cart total by calling the Testing the Shopping Cart TotalRun the application now to see how you can not only add a product to the shopping cart, but you can see the shopping cart total.
Adding Update and Checkout Buttons to the Shopping CartTo allow the users to modify the shopping cart, you'll add an Update button and a Checkout button to the shopping cart page. The Checkout button is not used until later in this tutorial series.
When the user clicks the Update button, the Next, you can update the code contained in the ShoppingCart.aspx.cs file to loop through the cart items and call the
When the user clicks the Update button on the ShoppingCart.aspx page, the UpdateCartItems method is called. The UpdateCartItems method gets the updated values for each item in the shopping cart. Then, the UpdateCartItems method calls the Updating and Removing Shopping Cart ItemsOn the ShoppingCart.aspx page, you can see controls have been added for updating the quantity of an item and removing an item. Now, add the code that will make these controls work.
The The In the next tutorial, you will use the Adding a Shopping Cart CounterTo allow the user to view the total number of items in the shopping cart, you will add a counter to the Site.Master page. This counter will also act as a link to the shopping cart.
Before the page is rendered as HTML, the You can run the application now to see how you can add, delete, and update items in the shopping cart. The shopping cart total will reflect the total cost of all items in the shopping cart.
SummaryIn this tutorial, you have created a shopping cart for the Wingtip Toys Web Forms sample application. During this tutorial you have used Entity Framework Code First, data annotations, strongly typed data controls, and model binding. The shopping cart supports adding, deleting, and updating items that the user has selected for purchase. In addition to implementing the shopping cart functionality, you have learned how to display shopping cart items in a GridView control and calculate the order total. In order to understand how the described functionality works in a real business application, you can view the example of nopCommerce - ASP.NET based open source eCommerce shopping cart. Originally, it was built on Web Forms and over the years it moved to MVC and now to ASP.NET Core. Addition InformationASP.NET Session State Overview Which of the following kinds of certificates is a notice that guarantees a user or website is legitimate?SSL/TLS certificates authenticate and secure websites and facilitate secure, encrypted connections. They let users know they are visiting a genuine website by displaying a padlock icon in the web browser.
What is the term for a website that uses encryption?Most legitimate websites use the encryption protection called “secure sockets layer” (SSL), which is a form of encrypting data that is sent to and from a website. This keeps attackers from accessing that data while it is in transit.
Which of the following categories are the rights to which creators are entitled for their work?Copyright (or author's right) is a legal term used to describe the rights that creators have over their literary and artistic works.
Which of the following gives authors and artists the exclusive rights to duplicate publish and sell their materials?A copyright is a type of intellectual property that gives its owner the exclusive right to copy, distribute, adapt, display, and perform a creative work, usually for a limited time. The creative work may be in a literary, artistic, educational, or musical form.
|