<%@ Page Language="JScript" %> <%@ Register TagPrefix="IBuySpy" TagName="Header" Src="_Header.ascx" %> <%@ Register TagPrefix="IBuySpy" TagName="Menu" Src="_Menu.ascx" %> <%@ Import Namespace="System.Data.SqlClient" %> <script runat="server"> //******************************************************* // // 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. // //******************************************************* function Page_Load(sender: Object, e: EventArgs) : void { // Populate the shopping cart the first time the page is accessed. if (Page.IsPostBack == false) { PopulateShoppingCartList(); } } //******************************************************* // // The UpdateBtn_Click event is raised when a user clicks // the "update" button on the client. The event handler // updates all items in the cart back to the database, // and then repopulates the datagrid with the current // cart contents. // //******************************************************* function UpdateBtn_Click(sender: Object, e: ImageClickEventArgs) : void { // Update the Shopping Cart and then Repopulate the List UpdateShoppingCartDatabase(); PopulateShoppingCartList(); } //******************************************************* // // The CheckoutBtn_Click event is raised when a user clicks // the "checkout" button on the client. The event handler // updates all items in the cart back to the database, // and then redirects the user to the checkout.aspx page // //******************************************************* function CheckoutBtn_Click(sender: Object, e: ImageClickEventArgs) : void { // Update Shopping Cart UpdateShoppingCartDatabase(); // If cart is not empty, proceed on to checkout page var cart : IBuySpy.ShoppingCartDB = new IBuySpy.ShoppingCartDB(); // Calculate shopping cart ID var cartId : String = 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 PopulateShoppingCartList helper method is used to // dynamically populate a GridControl with the contents of // the current user's shopping cart. // //******************************************************* function PopulateShoppingCartList() : void { var cart : IBuySpy.ShoppingCartDB = new IBuySpy.ShoppingCartDB(); // Obtain current user's shopping cart ID var cartId : String = 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 UpdateShoppingCartDatabase helper method is used to // update a user's items within the shopping cart database // using client input from the GridControl. // //******************************************************* function UpdateShoppingCartDatabase() : void { var cart : IBuySpy.ShoppingCartDB = new IBuySpy.ShoppingCartDB(); // Obtain current user's shopping cart ID var cartId : String = cart.GetShoppingCartId(); // Iterate through all rows within shopping cart list for (var i: int=0; i < MyList.Items.Count; i++) { // Obtain references to rows controls var quantityTxt : TextBox = MyList.Items[i].FindControl("Quantity"); var 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 var quantity : int; try { quantity = Int32.Parse(quantityTxt.Text); // If the quantity field is changed or delete is checked if (quantity != MyList.DataKeys[i] || remove.Checked == true) { var 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 (e) { MyError.Text = "There has been a problem with one or more of your inputs."; } } } </script> <html> <head> <link rel="stylesheet" type="text/css" href="IBuySpy.css"> </head> <body background="images/sitebkgrd.gif" leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0" marginheight="0" marginwidth="0"> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tr> <td colspan="2"> <IBuySpy:Header ID="Header1" runat="server" /> </td> </tr> <tr> <td valign="top"> <IBuySpy:Menu id="Menu1" runat="server" /> <img height="1" src="images/1x1.gif" width="145"> </td> <td align="left" valign="top" width="100%" nowrap> <table height="100%" align="left" cellspacing="0" cellpadding="0" width="100%" border="0"> <tr valign="top"> <td nowrap> <br> <form runat="server"> <img align="left" width="24" src="images/1x1.gif"> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tr> <td class="ContentHead"> <img align="left" height="32" width="60" src="images/1x1.gif">Shopping Cart <br> </td> </tr> </table> <img align="left" height="4" width="110" src="images/1x1.gif"> <font color="red"> <asp:Label id="MyError" class="ErrorText" EnableViewState="false" runat="Server" /> </font> <br> <img align="left" height="15" width="24" src="images/1x1.gif" border="0"> <asp:panel id="DetailsPanel" runat="server"> <img align="left" height="1" width="50" src="images/1x1.gif"> <table height="100%" cellspacing="0" cellpadding="0" width="550" border="0"> <tr valign="top"> <td width="550"> <asp:DataGrid id="MyList" runat="server" BorderColor="black" GridLines="Vertical" cellpadding="4" cellspacing="0" Font-Name="Verdana" Font-Size="8pt" ShowFooter="true" HeaderStyle-CssClass="CartListHead" FooterStyle-CssClass="CartListFooter" ItemStyle-CssClass="CartListItem" AlternatingItemStyle-CssClass="CartListItemAlt" DataKeyField="Quantity" AutoGenerateColumns="false"> <Columns> <asp:TemplateColumn HeaderText="Product ID"> <ItemTemplate> <asp:Label id="ProductID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ProductID") %>' /> </ItemTemplate> </asp:TemplateColumn> <asp:BoundColumn HeaderText="Product Name" DataField="ModelName" /> <asp:BoundColumn HeaderText="Model" DataField="ModelNumber" /> <asp:TemplateColumn HeaderText="Quantity"> <ItemTemplate> <asp:TextBox id="Quantity" runat="server" Columns="4" MaxLength="3" Text='<%# DataBinder.Eval(Container.DataItem, "Quantity") %>' width="40px" /> </ItemTemplate> </asp:TemplateColumn> <asp:BoundColumn HeaderText="Price" DataField="UnitCost" DataFormatString="{0:c}" /> <asp:BoundColumn HeaderText="Subtotal" DataField="ExtendedAmount" DataFormatString="{0:c}" /> <asp:TemplateColumn HeaderText="Remove"> <ItemTemplate> <center> <asp:CheckBox id="Remove" runat="server" /> </center> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:DataGrid> <img src="Images/1x1.gif" width="350" height="1"> <span class="NormalBold">Total: </span> <asp:Label class="NormalBold" ID="lblTotal" EnableViewState="false" runat="server" /> <br> <br> <img src="Images/1x1.gif" width="60" height="1"> <asp:imagebutton id="update" OnClick="UpdateBtn_Click" ImageURL="images/update_cart.gif" runat="server" /> <img height="1" width="15" src="Images/1x1.gif"> <asp:imagebutton id="checkout" OnClick="CheckoutBtn_Click" ImageURL="images/final_checkout.gif" runat="server" /> <br> </td> </tr> </table> </asp:panel> </form> </td> </tr> </table> </td> </tr> </table> </body> </html>