protected void btnAddToCart_Click(object sender, EventArgs e)
    {
        if (drpBookSelection.SelectedValue != "-1" && Session["shoppingcart"] != null)
        {
            // Retrieve selected book from the session
            Book selectedBook = (Book)Session["book"];

            // get user entered quqntity
            int quantity = int.Parse(txtQuantity.Text);
            // Create a book order with selected book and quantity
            BookOrder order = new BookOrder(selectedBook, quantity);
            //Retrieve to cart from the session
            ShoppingCart cart = (ShoppingCart)Session["shoppingcart"];
            // Add book order to the shopping cart
            cart.AddBookOrder(order);
            // Remove the selected item from the dropdown list
            drpBookSelection.Items.Remove(new ListItem(selectedBook.Title, selectedBook.Id));
            // Set the dropdown list's selected value as "-1"
            drpBookSelection.SelectedValue = "-1";
            //Set the description to show title and quantity of the book user added to the shopping cart
            lblDescription.Text = quantity + " copy of " + selectedBook.Title + " is added to the shopping cart";
            //Update the number of items in shopping cart displayed next to the link to ShoppingCartView.aspx
            lblNumItems.Text = cart.NumOfItems.ToString() + " items";
        }
    }
示例#2
0
    protected void btnAddToCart_Click(object sender, EventArgs e)
    {
        if (drpBookSelection.SelectedValue != "-1" && Session["shoppingcart"] != null)
        {
            //todo: Retrieve selected book from the session - OK
            Book   selectedBook = (Book)Session["selectedBooksSession"];
            String bookTitle    = selectedBook.Title; //creating book title string

            //todo: get user entered quqntity - OK
            int bookQuantity = int.Parse(txtQuantity.Text);

            //todo: Create a book order with selected book and quantity - OK
            BookOrder newOrder = new BookOrder(selectedBook, bookQuantity);

            //todo: Retrieve to cart from the session - OK
            ShoppingCart newCart = new ShoppingCart();
            if (Session["shoppingcart"] != null)
            {
                newCart = (ShoppingCart)Session["shoppingcart"];
            }

            //todo: Add book order to the shopping cart - OK
            newCart.AddBookOrder(newOrder);
            Session["shoppingcart"] = newCart;

            //todo: Remove the selected item from the dropdown list - OK
            drpBookSelection.Items.Remove(drpBookSelection.SelectedItem);

            //todo: Set the description to show title and quantity of the book user added to the shopping cart - OK
            lblDescription.Text = "";
            lblPrice.Text       = "";
            lblDescription.Text = txtQuantity.Text + " copy(s) of " + bookTitle + " was added to the shopping cart.";
            txtQuantity.Text    = "";

            //todo: Set the dropdown list's selected value as "-1" - OK
            drpBookSelection.SelectedIndex = -1;

            //todo: Update the number of items in shopping cart displayed next to the link to ShoppingCartView.aspx - OK
            if (newCart.NumOfItems == 0)
            {
                lblNumItems.Text = "empty";
            }
            else if (newCart.NumOfItems == 1)
            {
                lblNumItems.Text = "1 item";
            }
            else
            {
                lblNumItems.Text = newCart.NumOfItems.ToString() + " items";
            }
        }
    }
示例#3
0
    protected void btnAddToCart_Click(object sender, EventArgs e)
    {
        Page.Validate();

        if (quantityValidator.IsValid)
        {
            quantityValidator.CssClass = "hide";
        }

        if (Page.IsValid && drpBookSelection.SelectedValue != "-1" && Session["shoppingcart"] != null)
        {
            //todo: Retrieve selected book from the session
            Book selectedBook = (Book)Session["currentBook"];

            //todo: get user entered quqntity
            string str = txtQuantity.Text.ToString().Trim();

            int qnt = 0;
            try
            {
                qnt = Convert.ToInt32(str);
            }
            catch (Exception exc)
            {
                return;
            }

            if (qnt > 0)
            {
                //todo: Create a book order with selected book and quantity
                BookOrder order = new BookOrder(selectedBook, qnt);

                //todo: Retrieve to cart from the session
                ShoppingCart shoppingCart = (ShoppingCart)Session["shoppingcart"];

                //todo: Add book order to the shopping cart
                shoppingCart.AddBookOrder(order);
                Session["shoppingcart"] = shoppingCart;

                //todo: Remove the selected item from the dropdown list
                Response.Redirect("BookStore.aspx");
            }

            //todo: Set the dropdown list's selected value as "-1"

            //todo: Set the description to show title and quantity of the book user added to the shopping cart

            //todo: Update the number of items in shopping cart displayed next to the link to ShoppingCartView.aspx
        }
    }
示例#4
0
    protected void btnAddToCart_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            if (drpBookSelection.SelectedValue != "-1" && Session["selectedbook"] != null)
            {
                //todo: Retrieve selected book from the session
                Book selectedBook = Session["selectedbook"] as Book;

                //todo: get user entered quqntity
                int quantity = int.Parse(txtQuantity.Text);

                //todo: Create a book order with selected book and quantity
                BookOrder order = new BookOrder(selectedBook, quantity);

                //todo: Retrieve to cart from the session
                ShoppingCart shoppingcart = Session["shoppingcart"] as ShoppingCart;

                //todo: Add book order to the shopping cart
                shoppingcart.AddBookOrder(order);

                //todo: Remove the selected item from the dropdown list
                drpBookSelection.Items.Remove(drpBookSelection.SelectedItem);

                //todo: Set the dropdown list's selected value as "-1"
                drpBookSelection.SelectedIndex = 0;

                //todo: Set the description to show title and quantity of the book user added to the shopping cart
                lblDescription.Text = quantity.ToString() + (quantity == 1 ? " copy " : " copies ") + " of " + selectedBook.Title + " is ordered";

                //todo: Update the number of items in shopping cart displayed next to the link to ShoppingCartView.aspx
                if (shoppingcart.NumOfItems == 0)
                {
                    lblNumItems.Text = "empty";
                }
                else if (shoppingcart.NumOfItems == 1)
                {
                    lblNumItems.Text = "1 item";
                }
                else
                {
                    lblNumItems.Text = shoppingcart.NumOfItems.ToString() + " items";
                }
            }
        }
    }
示例#5
0
    protected void btnAddToCart_Click(object sender, EventArgs e)
    {
        Validate();
        if (Page.IsValid)
        {
            Book book     = BookCatalogDataAccess.GetABookById(BookList.SelectedValue);
            int  quantity = Int16.Parse(txtQauntity.Text);

            BookOrder order = new BookOrder(book, quantity);
            cart.AddBookOrder(order);
            BookList.Items.RemoveAt(BookList.SelectedIndex);
            string amount = quantity > 1 ? "copies" : "copy";
            lblDescription.Text = quantity + " " + amount + " of " + book.Title + " have been added to your cart";
            btnViewCart.Text    = "View Cart (" + cart.NumOfItems + " items)";
            BookList.ClearSelection();
            txtQauntity.Text = "";
        }
    }
示例#6
0
    protected void btnAddToCart_Click(object sender, EventArgs e)
    {
        if (drpBookSelection.SelectedValue != "-1" && Session["shoppingcart"] != null)
        {
            string bookId       = drpBookSelection.SelectedItem.Value;
            Book   selectedBook = BookCatalogDataAccess.GetBookById(bookId);

            //todo: Retrieve selected book from the session
            //Book book = Session["book"] as Book;
            //todo: get user entered quqntity
            int quantity = int.Parse(txtQuantity.Text);
            //todo: Create a book order with selected book and quantity
            BookOrder order = new BookOrder(selectedBook, quantity);
            //todo: Retrieve to cart from the session
            ShoppingCart cart = Session["shoppingcart"] as ShoppingCart;
            //todo: Add book order to the shopping cart
            cart.AddBookOrder(order);
            //todo: Remove the selected item from the dropdown list
            drpBookSelection.Items.Remove(drpBookSelection.SelectedItem);
            //todo: Set the dropdown list's selected value as "-1"
            //drpBookSelection.SelectedValue = "-1";
            //todo: Set the description to show title and quantity of the book user added to the shopping cart
            lblDescription.Text = quantity.ToString() + " copy(s) of " + selectedBook.Title + " is added to the shopping cart";
            //todo: Update the number of items in shopping cart displayed next to the link to ShoppingCartView.aspx
            if (cart.NumOfItems == 0)
            {
                lblNumItems.Text = "empty";
            }
            else if (cart.NumOfItems == 1)
            {
                lblNumItems.Text = "1 item";
            }
            else
            {
                lblNumItems.Text = cart.NumOfItems.ToString() + " items";
            }
        }
    }