Shopping Cart Page

The ShoppingCart.aspx page enables customers to view the current state of their shopping carts. They can also change quantities and remove items. To change quantities, users change the value in a text box, and then click the Update Your Shopping Cart button. If they are finished shopping, they can click the Final Check Out button.

The stored procedure that retrieves the information is named ShoppingCartList and can be seen below:

 CREATE Procedure ShoppingCartList (     @CartID nvarchar(50) ) AS SELECT     Products.ProductID,     Products.ModelName,     Products.ModelNumber,     ShoppingCart.Quantity,     Products.UnitCost,     Cast((Products.UnitCost * ShoppingCart.Quantity) as money)       as ExtendedAmount FROM     Products,     ShoppingCart WHERE     Products.ProductID = ShoppingCart.ProductID AND     ShoppingCart.CartID = @CartID ORDER BY     Products.ModelName,     Products.ModelNumber 

The logic for this page is encapsulated in three event handlers: the Page_Load() method, the UpdateBtn_Click() method for the Update button, and the CheckoutBtn_Click() method for the Check Out button. The page includes some internal methods used to maintain the shopping cart information.

Shopping cart information is maintained in the IBuySpy database. When users add items to their cart, the application is actually writing records to the database. To keep track of which items belong to which cart, the application tags each record with a cart ID, which is created in the ShoppingCartDB class, either from the user's login name, or, if the user has not yet logged in, from a dynamically generated GUID.

When the ShoppingCart.aspx page is first displayed, the Page_Load() method calls the internal PopulateShoppingCartList() method to populate the shopping cart. That method first checks that there are items in the cart. If not, an error is displayed by setting the text of a Label control. The shopping cart items are displayed in a DataGrid control, which includes built-in support for headers and footers, embedded controls in a column, alternating item display (the gray bar), and databinding.

The PopulateShoppingCartList() method obtains a collection of all items in the user's shopping cart by calling the GetItems() method of the ShoppingCartDB class. The GetItems() method in turn uses the ShoppingCartList stored procedure to retrieve the items from the database.

Once the collection of items is retrieved, it is bound to the DataGrid control by setting the control's DataSource property. Data is copied from the collection to the grid by calling the grid's DataBind() method, which causes a grid to loop through the data source to generate a row for each item. The layout of each item is determined by a set of individual column definitions in the DataGrid control.

When users click the Update Your Shopping Cart button, it invokes the UpdateBtn_Click() method, which in turn calls the UpdateShoppingCartDatabase() method to write the shopping cart items back into the database. After the update, the UpdateBtn_Click() method calls the same PopulateShoppingCartList() method used when the page is first displayed, to refresh the shopping cart grid on the page. The update is performed by looping through the Items collection of the DataGrid control. Each item corresponds logically to a row in the grid. To get the value of an individual control in an item, the process calls the Items collection's FindControl() method, which is a shorthand way to locate a control that might be in any column. For each item, the update process determines whether to remove or update that item in the shopping cart.

If the user clicks the Final Check Out button, it invokes the CheckoutBtn_Click() method. This method performs an update of the cart to make sure the database is current, and then redirects the client to the Checkout.aspx page.

A possible optimization would be to store the original (pre-update) quantity value of each row in a hidden field of the grid. Doing so would enable us to determine whether the Quantity text box value had changed and would allow us to call the UpdateItem() method of the ShoppingCartDB class only if the quantity value of that specific row had actually changed. As currently implemented, the page updates every item and refreshes the grid each time the user clicks the Update Your Shopping Cart button, even if there is no change.

ASP. NET Solutions - 24 Case Studies. Best Practices for Developers
ASP. NET Solutions - 24 Case Studies. Best Practices for Developers
ISBN: 321159659
Year: 2003
Pages: 175 © 2008-2017.
If you may any questions please contact us: