// builds items table private void BuildItemsTable() { // get items list List <object> _items = ((ShopMaster)Master).GetItems(); // current row TableRow _row = new TableRow(); tableItems.Rows.Add(_row); foreach (object _o in _items) { Item_S1 _item = (Item_S1)_o; TableCell _cell = CreateCell(_item); // check cell count of row if (_row.Cells.Count >= 5) { // check row count if (tableItems.Rows.Count >= 5) { break; } _row = new TableRow(); tableItems.Rows.Add(_row); } // add cell _row.Cells.Add(_cell); } }
// creates table cell private TableCell CreateCell(Item_S1 _item) { // ItemCell, CellImage, CellText TableCell _cell = new TableCell(); _cell.CssClass = "ItemCell"; _cell.Width = 340; // thumbnail Image _image = ((ShopMaster)Master).GetItemImageThumb(_item); _image.CssClass = "CellImage"; _cell.Controls.Add(_image); // textlink HyperLink _link = new HyperLink(); _link.CssClass = "CellText"; _link.Text = string.Format("{0}<br/>{1:c2}", _item.Name, _item.Price); _link.NavigateUrl = string.Format("Item.aspx?id={0}", _item.ID); _cell.Controls.Add(_link); return(_cell); }
// page_load protected void Page_Load(object sender, EventArgs e) { // pull query string string _itemIDstr = Request.QueryString["id"]; // check if a string was returned if (_itemIDstr == null) { Trace.Warn("Shop", "No query for Item ID"); Response.Redirect("Browse.aspx", true); //return; } // try to parse to an int int _itemID; if (!Int32.TryParse(_itemIDstr, out _itemID)) { Trace.Warn("Shop", string.Format("Failed to parse item ID=\"{0}\"", _itemIDstr)); Response.Redirect("Browse.aspx", true); //return; } // attempt to get the item info try { item = (Item_S1)((ShopMaster)Master).GetItem(_itemID); BuildDisplay(); } catch (Exception _e) { Trace.Warn("Shop", string.Format("Failed to fetch item ID={0}", _itemID), _e); Response.Redirect("Browse.aspx", true); //return; } }
// get item # public object GetItem(int _id) { if (!OpenConnection()) { throw new InvalidOperationException("Failed to open DB"); } // get item SqlCommand _cmd = new SqlCommand("SELECT * FROM Items WHERE ItemID=@ID", connection); _cmd.Parameters.AddWithValue("@ID", _id); SqlDataReader _reader = _cmd.ExecuteReader(); if (!_reader.Read()) { _reader.Close(); throw new ArgumentException("Failed to find item"); } Item_S1 _item = new Item_S1(_reader); _reader.Close(); return(_item); }
// gets item image public Image GetItemImage(object _o) { Item_S1 _item = (Item_S1)_o; string _imagepath = GetImagePath(_item.ImageName); Image _image = new Image(); _image.ImageUrl = _imagepath; _image.Attributes["onerror"] = "this.src='./img/noimage.jpg';"; return(_image); }
// remove from cart public bool Remove(Item_S1 _item) { return(Remove(_item.ID, 1)); }
public bool Add(Item_S1 _item, int _quantity) { return(Add(_item.ID, _quantity)); }
// add to cart public bool Add(Item_S1 _item) { return(Add(_item.ID, 1)); }
public ShoppingCartItem_S1(Item_S1 _item, int _quantity) : this(_item.ID, 0) { }
// constructors public ShoppingCartItem_S1(Item_S1 _item) : this(_item.ID) { }
// get quantity of item public int Quantity(Item_S1 _item) { return(Quantity(_item.ID)); }
public bool Remove(Item_S1 _item, int _quantity) { return(Remove(_item.ID, _quantity)); }
// builds page for given item ID private void BuildDisplay() { Item_S1 _item = item; // image Image _image = ((ShopMaster)Master).GetItemImage(_item); _image.CssClass = "ItemImage"; contentItemArea.Controls.Add(_image); // item info // table Table _table = new Table(); _table.CssClass = "ItemInfo"; _table.CellPadding = 5; _table.CellSpacing = 15; TableRow _row; TableCell _cell; // name _row = new TableRow(); _cell = new TableCell(); _cell.Text = _item.Name; _row.Cells.Add(_cell); _table.Rows.Add(_row); // weight _row = new TableRow(); _cell = new TableCell(); _cell.Text = string.Format("{0:f2}lb", _item.Weight); _row.Cells.Add(_cell); _table.Rows.Add(_row); // price _row = new TableRow(); _cell = new TableCell(); _cell.Text = string.Format("{0:c2}", _item.Price); _row.Cells.Add(_cell); _table.Rows.Add(_row); // buttons // add to cart (button) Button _toCart = new Button(); _toCart.CssClass = "ItemButton"; _toCart.Text = "Add to cart"; _toCart.Click += ClickAddToCart; // view cart (button) Button _viewCart = new Button(); _viewCart.CssClass = "ItemButton"; _viewCart.Text = "View cart"; _viewCart.Click += ClickToCart; // label for if add to cart response Label _labelMsg = new Label(); _labelMsg.ID = "LabelCartAdded"; _row = new TableRow(); _cell = new TableCell(); _cell.Controls.Add(_labelMsg); _cell.Controls.Add(_toCart); _cell.Controls.Add(_viewCart); _row.Cells.Add(_cell); _table.Rows.Add(_row); contentItemArea.Controls.Add(_table); }
// fills cart table private void PopulateTable(IReadOnlyCollection <ShoppingCart_S1.ShoppingCartItem_S1> _items) { TableHeaderRow _rowh; TableRow _row; TableHeaderCell _cellh; TableCell _cell; // build title row _rowh = new TableHeaderRow(); _rowh.CssClass = "TitleRow"; _cellh = new TableHeaderCell(); _cellh.CssClass = "TitleCell"; _cellh.Text = "ID"; _rowh.Cells.Add(_cellh); _cellh = new TableHeaderCell(); _cellh.CssClass = "TitleCell"; _cellh.Text = "Name"; _rowh.Cells.Add(_cellh); _cellh = new TableHeaderCell(); _cellh.CssClass = "TitleCell"; _cellh.Text = "Price"; _rowh.Cells.Add(_cellh); _cellh = new TableHeaderCell(); _cellh.CssClass = "TitleCell"; _cellh.Text = "Quantity"; _rowh.Cells.Add(_cellh); _cellh = new TableHeaderCell(); _cellh.CssClass = "TitleCell"; _cellh.Text = " "; _rowh.Cells.Add(_cellh); tableCart.Rows.Add(_rowh); // populate table rows foreach (ShoppingCart_S1.ShoppingCartItem_S1 _cartItem in _items) { // get item Item_S1 _item = (Item_S1)((ShopMaster)Master).GetItem(_cartItem.ItemID); Trace.Warn("cart", string.Format("row for {0}", _cartItem.ItemID)); _row = new TableRow(); _row.CssClass = "ItemRow"; // ID _cell = new TableCell(); _cell.CssClass = "BasicCell"; HyperLink _link = new HyperLink(); _link.Text = _item.ID.ToString(); _link.NavigateUrl = string.Format("Item.aspx?id={0}", _item.ID); _cell.Controls.Add(_link); _row.Cells.Add(_cell); // name _cell = new TableCell(); _cell.CssClass = "BasicCell"; _cell.Text = _item.Name; _row.Cells.Add(_cell); // price _cell = new TableCell(); _cell.CssClass = "BasicCell"; _cell.Text = _item.Price.ToString("C2"); _row.Cells.Add(_cell); // quantity _cell = new TableCell(); _cell.CssClass = "BasicCell"; _cell.Text = _cartItem.Quantity.ToString(); _row.Cells.Add(_cell); // button for remove _cell = new TableCell(); _cell.CssClass = "ButtonCell"; Button _button = new Button(); _button.ID = string.Format("buttonRemove{0}", _item.ID); _button.CssClass = "Button"; _button.Text = "Remove"; _button.Command += CommandRemove; _button.CommandName = "ItemID"; _button.CommandArgument = _item.ID.ToString(); _cell.Controls.Add(_button); _row.Cells.Add(_cell); tableCart.Rows.Add(_row); } }