private void AppendOrderToRow(OrderEnt order, HtmlTableRow row) { // ID, to be clicked. Redirect to the OrderItems page with the id set. HtmlTableCell cell = new HtmlTableCell(); Button btn = new Button(); btn.ID = order.OrderID.ToString(); btn.Text = order.OrderID.ToString(); btn.BackColor = System.Drawing.Color.Transparent; btn.Click += new EventHandler(delegate(object s, EventArgs e) { HttpContext.Current.Session.Add("OrderID", (s as Button).ID); HttpContext.Current.Response.Redirect("OrderInfo.aspx"); }); cell.Controls.Add(btn); row.Cells.Add(cell); // Date, Address, and Price simply need to be displayed. cell = new HtmlTableCell(); cell.InnerHtml = order.OrderDate.ToString(); row.Cells.Add(cell); cell = new HtmlTableCell(); cell.InnerHtml = order.OrderAddress; row.Cells.Add(cell); // Find our the total price for the order by summing up all its items. decimal total = 0; BadgerDatabaseEntities context = new BadgerDatabaseEntities(); List <OrderItemEnt> orderItems = context.OrderItemEnts.ToList(); foreach (OrderItemEnt orderItem in orderItems) { ProductEnt item = context.ProductEnts.Find(orderItem.ProductID); if (item != null) { total += item.ProductPrice * orderItem.Qty; } } cell = new HtmlTableCell(); cell.InnerHtml = total.ToString("c"); row.Cells.Add(cell); }
// Add in information related to an order. protected void Page_Load(object sender, EventArgs e) { // Clear any existing information. orderTable.Controls.Clear(); if (HttpContext.Current.Session["CurrentAccount"] == null) { orderTotalValue.InnerHtml = "Please login to view order information."; return; } BadgerDatabaseEntities context = new BadgerDatabaseEntities(); // If we do not have a specific order specified, display all orders. if (HttpContext.Current.Session["OrderID"] == null) { // Hide our total and return. orderTotal.Visible = false; // Prepare and add our heading row. HtmlTableRow heading = new HtmlTableRow(); PrepareOrderHeading(heading); orderTable.Controls.Add(heading); // Load our orders from the database. List <OrderEnt> orders = context.OrderEnts.ToList(); // List any that belong to our current user. for (int x = 0; x < orders.Count; x++) { OrderEnt order = orders[x]; if (order.Username.ToLower() == ((string)HttpContext.Current.Session["CurrentAccount"]).ToLower()) { HtmlTableRow orderRow = new HtmlTableRow(); AppendOrderToRow(order, orderRow); orderTable.Controls.Add(orderRow); } } } // If we DO have a specific order specified, display its items instead. else { // Show our total and reutrn. orderTotal.Visible = true; // Prepare and add our heading row. HtmlTableRow heading = new HtmlTableRow(); PrepareItemHeading(heading); orderTable.Controls.Add(heading); // Get our order based on id. OrderEnt order = context.OrderEnts.Find(int.Parse((string)HttpContext.Current.Session["OrderID"])); List <OrderItemEnt> orderItems = context.OrderItemEnts.ToList(); // Prepare our rows (hardcoded example for now.) // Also calculate our total while here. decimal total = 0; for (int x = 0; x < orderItems.Count; x++) { OrderItemEnt orderItem = orderItems[x]; if (order.OrderID == orderItem.OrderID) { HtmlTableRow orderRow = new HtmlTableRow(); AppendOrderItemToRow(orderItem, orderRow); orderTable.Controls.Add(orderRow); ProductEnt item = context.ProductEnts.Find(orderItem.ProductID); if (item != null) { total += item.ProductPrice * orderItem.Qty; } } } // Add our total. orderTotalValue.InnerHtml = total.ToString("c"); } // After displaying, clear our session variable. if (HttpContext.Current.Session["OrderID"] != null) { HttpContext.Current.Session.Remove("OrderID"); } }