Exemple #1
0
        //*******************************************************
        //
        // The RegisterBtn_Click event handler is used on this page to
        // add a new user into the IBuySpy 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
                IBuySpy.ShoppingCartDB shoppingCart = new IBuySpy.ShoppingCartDB();
                String tempCartId = shoppingCart.GetShoppingCartId();

                // Add New Customer to CustomerDB database
                IBuySpy.CustomersDB accountSystem = new IBuySpy.CustomersDB();
                String customerId = accountSystem.AddCustomer(Name.Text, Email.Text, 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["IBuySpy_FullName"].Value = Server.HtmlEncode(Name.Text);

                    // Redirect browser back to shopping cart page
                    Response.Redirect("ShoppingCart.aspx");
                }
                else
                {
                    MyError.Text = "Registration failed:&nbsp; That email address is already registered.<br><img align=left height=1 width=92 src=images/1x1.gif>";
                }
            }
        }
Exemple #2
0
        //*******************************************************
        //
        // 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)
            {
                IBuySpy.ShoppingCartDB cart = new IBuySpy.ShoppingCartDB();

                // 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");
        }
Exemple #3
0
        //*******************************************************
        //
        // 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
                IBuySpy.ShoppingCartDB cart = new IBuySpy.ShoppingCartDB();
                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));
            }
        }
Exemple #4
0
        //*******************************************************
        //
        // 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
        //
        //*******************************************************

        private void CheckoutBtn_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            // Update Shopping Cart
            UpdateShoppingCartDatabase();

            // If cart is not empty, proceed on to checkout page
            IBuySpy.ShoppingCartDB cart = new IBuySpy.ShoppingCartDB();

            // 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.";
            }
        }
Exemple #5
0
        //*******************************************************
        //
        // The UpdateShoppingCartDatabase helper method is used to
        // update a user's items within the shopping cart database
        // using client input from the GridControl.
        //
        //*******************************************************

        void UpdateShoppingCartDatabase()
        {
            IBuySpy.ShoppingCartDB cart = new IBuySpy.ShoppingCartDB();

            // 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.";
                }
            }
        }
Exemple #6
0
        //*******************************************************
        //
        // 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)
        {
            IBuySpy.ShoppingCartDB cart = new IBuySpy.ShoppingCartDB();

            // 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
                IBuySpy.OrdersDB ordersDatabase = new IBuySpy.OrdersDB();
                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;
            }
        }
Exemple #7
0
        //*******************************************************
        //
        // 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
                IBuySpy.ShoppingCartDB shoppingCart = new IBuySpy.ShoppingCartDB();
                String tempCartID = shoppingCart.GetShoppingCartId();

                // Attempt to Validate User Credentials using CustomersDB
                IBuySpy.CustomersDB accountSystem = new IBuySpy.CustomersDB();
                String customerId = accountSystem.Login(email.Text, 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
                    IBuySpy.CustomerDetails customerDetails = accountSystem.GetCustomerDetails(customerId);

                    // Store the user's fullname in a cookie for personalization purposes
                    Response.Cookies["IBuySpy_FullName"].Value = customerDetails.FullName;

                    // Make the cookie persistent only if the user selects "persistent" login checkbox
                    if (RememberLogin.Checked == true)
                    {
                        Response.Cookies["IBuySpy_FullName"].Expires = DateTime.Now.AddMonths(1);
                    }

                    // Redirect browser back to originating page
                    FormsAuthentication.RedirectFromLoginPage(customerId, RememberLogin.Checked);
                }
                else
                {
                    Message.Text = "Login Failed!";
                }
            }
        }
Exemple #8
0
        //*******************************************************
        //
        // The PopulateShoppingCartList helper method is used to
        // dynamically populate a GridControl with the contents of
        // the current user's shopping cart.
        //
        //*******************************************************

        void PopulateShoppingCartList()
        {
            IBuySpy.ShoppingCartDB cart = new IBuySpy.ShoppingCartDB();

            // 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));
            }
        }