using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class Admin_Controls_EditItem : WebBlockUserControl { public int ItemID { get { return (ViewState["ItemID"] == null ? 0 : (int)ViewState["ItemID"]); } set { ViewState["ItemID"] = value; } } public int SelectorID { get { return (ViewState["SelectorID"] == null ? 0 : (int)ViewState["SelectorID"]); } set { ViewState["SelectorID"] = value; } } public event EventHandler ItemAdded; public event EventHandler ItemUpdated; public event EventHandler Cancelled; public void PrePopulate(DataRow dr) { ItemID = 0; if (dr["LowInventory"] != DBNull.Value) txtLowInventory.Text = (string)dr["LowInventory"]; if (dr["SKU"] != DBNull.Value) txtAddSKU.Text = (string)dr["SKU"]; if (dr["Name"] != DBNull.Value) txtAddName.Text = (string)dr["Name"]; if (dr["Price"] != DBNull.Value) txtAddPrice.Text = ((decimal)dr["Price"]).ToString("f"); if (dr["VolumePricingTableID"] != DBNull.Value) ddlVolumePricingTable.SelectedValue = dr["VolumePricingTableID"].ToString(); if (dr["Weight"] != DBNull.Value) txtAddWeight.Text = dr["Weight"].ToString(); if (dr["Active"] != DBNull.Value) chkActive.Checked = (bool)dr["Active"]; } public void BindData() { BindVolume(); if (ItemID == 0) { // clear out any data that might have been in there litItemID.Text = "New Item"; txtAddSKU.Text = ""; txtAddName.Text = ""; txtAddPrice.Text = ""; txtAddWeight.Text = "1"; txtAddInventory.Text = "-1"; txtLowInventory.Text = "-1"; ddlVolumePricingTable.SelectedIndex = 0; } else { DataRow dr = Database.Items_Get(ItemID); if (dr != null) { litItemID.Text = ItemID.ToString(); txtAddSKU.Text = (string)dr["SKU"]; txtAddName.Text = (string)dr["Name"]; txtAddPrice.Text = ((decimal)dr["Price"]).ToString("f"); txtAddWeight.Text = dr["Weight"].ToString(); txtAddInventory.Text = dr["Inventory"].ToString(); if(dr["LowInventory"] != DBNull.Value) txtLowInventory.Text = dr["LowInventory"].ToString(); ddlVolumePricingTable.SelectedValue = (dr["VolumePricingTableID"] == DBNull.Value ? "" : dr["VolumePricingTableID"].ToString()); chkActive.Checked = (bool)dr["Active"]; } } } private void BindVolume() { DataTable dt = Database.VolumePricingTable_GetAll(); ddlVolumePricingTable.DataSource = dt; ddlVolumePricingTable.DataBind(); ddlVolumePricingTable.Items.Insert(0, new ListItem("Don't use volume pricing", "")); } private void SaveItem() { int? VolumePricingTableID = null; if (ddlVolumePricingTable.SelectedValue != "") VolumePricingTableID = Int32.Parse(ddlVolumePricingTable.SelectedValue); if (ItemID == 0) Database.Items_Add(SelectorID, Int32.Parse(txtAddInventory.Text), txtAddSKU.Text, txtAddName.Text, Decimal.Parse(txtAddPrice.Text), Decimal.Parse(txtAddWeight.Text), VolumePricingTableID, chkActive.Checked, Int32.Parse(txtLowInventory.Text)); else Database.Items_Update(ItemID, SelectorID, Int32.Parse(txtAddInventory.Text), txtAddSKU.Text, txtAddName.Text, Decimal.Parse(txtAddPrice.Text), Decimal.Parse(txtAddWeight.Text), VolumePricingTableID, chkActive.Checked, Int32.Parse(txtLowInventory.Text)); } protected void btnCancelAddItem_Click(object sender, EventArgs e) { if (Cancelled != null) Cancelled(this, null); } protected void btnAddItem_Click(object sender, EventArgs e) { if (Page.IsValid) { bool IsAdding = (ItemID == 0); SaveItem(); if (IsAdding) ItemAdded(this, null); else ItemUpdated(this, null); } } protected override void OnPreRender(EventArgs e) { txtAddPrice.Enabled = (ddlVolumePricingTable.SelectedValue == ""); base.OnPreRender(e); } protected void SKUCheck(object source, ServerValidateEventArgs arguments) { bool IsAdding = (ItemID == 0); // make sure SKU is not being used by another Item DataTable dtItems = Database.Items_Search(string.Empty, txtAddSKU.Text); if (dtItems.Rows.Count == 0 && IsAdding) arguments.IsValid = true; else if (dtItems.Rows.Count == 1 && !IsAdding) arguments.IsValid = true; else arguments.IsValid = false; } }