Beispiel #1
0
        private void FillPage()
        {
            if (!String.IsNullOrWhiteSpace(Request.QueryString["id"]))
            {
                int id = Convert.ToInt32(Request.QueryString["id"]);
                Models.ProductModel     productModel = new Models.ProductModel();
                WebApplication1.Product product      = productModel.GetProduct(id);

                lblPrice.Text       = "Price per unit: <br/>$ " + product.Price;
                lblTitle.Text       = product.Name;
                lblDescription.Text = product.Description;
                lblItemNr.Text      = id.ToString();
                imgProduct.ImageUrl = "~/Images/Products/" + product.Image;

                int[] amount = Enumerable.Range(1, 20).ToArray();
                ddlAmount.DataSource           = amount;
                ddlAmount.AppendDataBoundItems = true;
                ddlAmount.DataBind();
            }
        }
        private void CreateShopTable(List <Purchase> purchaseList, out double subTotal)
        {
            subTotal = new Double();
            ProductModel model = new ProductModel();

            foreach (Purchase purchase in purchaseList)
            {
                WebApplication1.Product product = model.GetProduct(purchase.ProductID);


                ImageButton btnImage = new ImageButton
                {
                    ImageUrl    = string.Format("~/Images/Products/{0}", product.Image),
                    PostBackUrl = string.Format("~/Pages/Product.aspx?id={0}", product.ID),
                    CssClass    = "productImage"
                };

                LinkButton lnkDelete = new LinkButton
                {
                    PostBackUrl = string.Format("~/Pages/ShoppingCart.aspx?id={0}", purchase.ID),
                    Text        = "Delete Item",
                    ID          = "del" + purchase.ID
                };

                lnkDelete.Click += Delete_Item;

                int[]        amount    = Enumerable.Range(1, 20).ToArray();
                DropDownList ddlAmount = new DropDownList
                {
                    DataSource           = amount,
                    AppendDataBoundItems = true,
                    AutoPostBack         = true,
                    ID = purchase.ID.ToString()
                };

                ddlAmount.DataBind();
                ddlAmount.SelectedValue         = purchase.Amount.ToString();
                ddlAmount.SelectedIndexChanged += ddlAmount_SelectedIndexChanged;

                Table table = new Table {
                    CssClass = "CartTable"
                };
                TableRow row1 = new TableRow();
                TableRow row2 = new TableRow();

                TableCell cell1_1 = new TableCell {
                    RowSpan = 2, Width = 50
                };
                TableCell cell1_2 = new TableCell
                {
                    Text = string.Format("<h4>{0}</h4><br />{1}<br/>In Stock",
                                         product.Name, "Item No:" + product.ID),
                    HorizontalAlign = HorizontalAlign.Left,
                    Width           = 350,
                };
                TableCell cell1_3 = new TableCell {
                    Text = "Unit Price<hr/>"
                };
                TableCell cell1_4 = new TableCell {
                    Text = "Quantity<hr/>"
                };
                TableCell cell1_5 = new TableCell {
                    Text = "Item Total<hr/>"
                };
                TableCell cell1_6 = new TableCell();

                TableCell cell2_1 = new TableCell();
                TableCell cell2_2 = new TableCell {
                    Text = "$ " + product.Price
                };
                TableCell cell2_3 = new TableCell();
                TableCell cell2_4 = new TableCell {
                    Text = "$ " + Math.Round((purchase.Amount * product.Price), 2)
                };
                TableCell cell2_5 = new TableCell();

                // Set custom controls
                cell1_1.Controls.Add(btnImage);
                cell1_6.Controls.Add(lnkDelete);
                cell2_3.Controls.Add(ddlAmount);

                // Add rows & cells to table
                row1.Cells.Add(cell1_1);
                row1.Cells.Add(cell1_2);
                row1.Cells.Add(cell1_3);
                row1.Cells.Add(cell1_4);
                row1.Cells.Add(cell1_5);
                row1.Cells.Add(cell1_6);

                row2.Cells.Add(cell2_1);
                row2.Cells.Add(cell2_2);
                row2.Cells.Add(cell2_3);
                row2.Cells.Add(cell2_4);
                row2.Cells.Add(cell2_5);
                table.Rows.Add(row1);
                table.Rows.Add(row2);
                pnlShoppingCart.Controls.Add(table);

                // Add total of current purchased item to subtotal
                subTotal += (purchase.Amount * Convert.ToInt32(product.Price));
            }

            // Add selected objects to Session
            Session[User.Identity.GetUserId()] = purchaseList;
        }