using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Text; using System.Web.UI.WebControls; using Northwind45.BusinessObject; namespace Northwind45 { public partial class GridViewInline_Products : System.Web.UI.Page { private string _validationErrors = String.Empty; protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) { Functions.GridViewRowDataBound(sender, e, 1); } protected void GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) { Functions.GridViewRowCreated(sender, e, 1); } public ProductsCollection GetGridData(int maximumRows, int startRowIndex, out int totalRowCount, string sortByExpression) { return Products.SelectSkipAndTake(maximumRows, startRowIndex, out totalRowCount, sortByExpression); } protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e) { string productNameValue = (string)e.NewValues[0]; string unitPriceValue = (string)e.NewValues[4]; string unitsInStockValue = (string)e.NewValues[5]; string unitsOnOrderValue = (string)e.NewValues[6]; string reorderLevelValue = (string)e.NewValues[7]; if (!IsFormValid(productNameValue, unitPriceValue, unitsInStockValue, unitsOnOrderValue, reorderLevelValue)) Functions.ShowModalHtmlError(_validationErrors, this, "An error occured on item update!"); } public SuppliersCollection GetSuppliersDropDownListData() { return Suppliers.SelectSuppliersDropDownListData(); } public CategoriesCollection GetCategoriesDropDownListData() { return Categories.SelectCategoriesDropDownListData(); } public void DeleteGridItem(int productID) { try { Products.Delete(productID); } catch (Exception ex) { Functions.ShowModalError(ex, this); } } public void UpdateItem(Products objProducts) { TryUpdateModel(objProducts); if (ModelState.IsValid) { try { objProducts.Update(); } catch (Exception ex) { Functions.ShowModalError(ex, this, "An error occured on item update!"); } } } protected void IBtnAdd_Click(object sender, System.Web.UI.ImageClickEventArgs e) { ShowAddPanel(); } protected void LbtnAdd_Click(object sender, EventArgs e) { ShowAddPanel(); } protected void IBtnAddNewRecord_Click(object sender, System.Web.UI.ImageClickEventArgs e) { if (IsFormValid(TxtProductName.Text, TxtUnitPrice.Text, TxtUnitsInStock.Text, TxtUnitsOnOrder.Text, TxtReorderLevel.Text)) { bool isErrorOccured = false; try { Products objProducts = new Products(); if (!String.IsNullOrEmpty(TxtProductName.Text)) objProducts.ProductName = TxtProductName.Text; if (!String.IsNullOrEmpty(DdlSupplierID.SelectedValue)) objProducts.SupplierID = Convert.ToInt32(DdlSupplierID.SelectedValue); if (!String.IsNullOrEmpty(DdlCategoryID.SelectedValue)) objProducts.CategoryID = Convert.ToInt32(DdlCategoryID.SelectedValue); if (!String.IsNullOrEmpty(TxtQuantityPerUnit.Text)) objProducts.QuantityPerUnit = TxtQuantityPerUnit.Text; if (!String.IsNullOrEmpty(TxtUnitPrice.Text)) objProducts.UnitPrice = Convert.ToDecimal(TxtUnitPrice.Text); if (!String.IsNullOrEmpty(TxtUnitsInStock.Text)) objProducts.UnitsInStock = Convert.ToInt16(TxtUnitsInStock.Text); if (!String.IsNullOrEmpty(TxtUnitsOnOrder.Text)) objProducts.UnitsOnOrder = Convert.ToInt16(TxtUnitsOnOrder.Text); if (!String.IsNullOrEmpty(TxtReorderLevel.Text)) objProducts.ReorderLevel = Convert.ToInt16(TxtReorderLevel.Text); objProducts.Discontinued = CbxDiscontinued.Checked; // save record objProducts.Insert(); ClearControlValues(); } catch (Exception ex) { Functions.ShowModalError(ex, this, "An error occured on item addition!"); isErrorOccured = true; } if (!isErrorOccured) HideAddPanel(); } else { Functions.ShowModalHtmlError(_validationErrors, this, "Validation error occured"); } } protected void IBtnCancelAdd_Click(object sender, System.Web.UI.ImageClickEventArgs e) { ClearControlValues(); HideAddPanel(); } protected void IBtnDiscontinued_Click(object sender, System.Web.UI.ImageClickEventArgs e) { // toggle checked and unchecked image, then save ImageButton ibtn = sender as ImageButton; Products objProducts = Products.SelectByPrimaryKey(Convert.ToInt32(ibtn.AlternateText)); if (objProducts != null) { if (ibtn.ImageUrl.Contains("CheckBoxTrue.png")) { ibtn.ImageUrl = "~/Images/CheckBoxFalse.png"; objProducts.Discontinued = false; } else { ibtn.ImageUrl = "~/Images/CheckBoxTrue.png"; objProducts.Discontinued = true; } objProducts.Update(); } } private void ShowAddPanel() { GridView1.ShowHeader = false; PnlAddNewRecord.Visible = true; } private void HideAddPanel() { GridView1.ShowHeader = true; PnlAddNewRecord.Visible = false; } private void ClearControlValues() { TxtProductName.Text = String.Empty; DdlSupplierID.SelectedValue = String.Empty; DdlCategoryID.SelectedValue = String.Empty; TxtQuantityPerUnit.Text = String.Empty; TxtUnitPrice.Text = String.Empty; TxtUnitsInStock.Text = String.Empty; TxtUnitsOnOrder.Text = String.Empty; TxtReorderLevel.Text = String.Empty; CbxDiscontinued.Checked = false; } private bool IsFormValid(string productNameValue, string unitPriceValue, string unitsInStockValue, string unitsOnOrderValue, string reorderLevelValue) { bool isValid = true; StringBuilder sb = new StringBuilder(); _validationErrors = String.Empty; // validate required fields if (String.IsNullOrEmpty(productNameValue)) { sb.Append("- Product Name is required<br>"); isValid = false; } // validate field data type if (!String.IsNullOrEmpty(unitPriceValue)) { decimal unitPrice; bool isUnitPriceValid = Decimal.TryParse(unitPriceValue, out unitPrice); if (!isUnitPriceValid) { sb.Append("- Unit Price is an invalid number<br>"); isValid = false; } } if (!String.IsNullOrEmpty(unitsInStockValue)) { Int16 unitsInStock; bool isUnitsInStockValid = Int16.TryParse(unitsInStockValue, out unitsInStock); if (!isUnitsInStockValid) { sb.Append("- Units In Stock is an invalid number<br>"); isValid = false; } } if (!String.IsNullOrEmpty(unitsOnOrderValue)) { Int16 unitsOnOrder; bool isUnitsOnOrderValid = Int16.TryParse(unitsOnOrderValue, out unitsOnOrder); if (!isUnitsOnOrderValid) { sb.Append("- Units On Order is an invalid number<br>"); isValid = false; } } if (!String.IsNullOrEmpty(reorderLevelValue)) { Int16 reorderLevel; bool isReorderLevelValid = Int16.TryParse(reorderLevelValue, out reorderLevel); if (!isReorderLevelValid) { sb.Append("- Reorder Level is an invalid number<br>"); isValid = false; } } if (!isValid) _validationErrors = sb.ToString(); return isValid; } } }