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; using System.Collections.Specialized; using System.Text; public partial class Admin_EditDiscount : WebBlocks.AdministrativePage { public int DiscountID { get { return (ViewState["DiscountID"] == null ? 0 : (int)ViewState["DiscountID"]); } set { ViewState["DiscountID"] = value; } } protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { if (Request["DiscountID"] != null) DiscountID = Int32.Parse(Request["DiscountID"]); if (DiscountID > 0) BindData(); } } private void BindData() { DataRow dr = Database.Discounts_Get(DiscountID); if (dr != null) { txtDiscountTitle.Text = (string)dr["DiscountTitle"]; txtDiscountCode.Text = (string)dr["DiscountCode"]; calStartDate.SelectedDate = (DateTime)dr["StartDate"]; calEndDate.SelectedDate = (DateTime)dr["EndDate"]; txtRequiredSubtotal.Text = ((decimal)dr["RequiredSubtotal"]).ToString("f"); txtDiscountDollarAmount.Text = ((decimal)dr["DiscountDollarAmount"]).ToString("f"); txtDiscountPercentage.Text = ((int)dr["DiscountPercentage"]).ToString(); ddlGrantsFreeShipping.SelectedValue = ((bool)dr["GrantsFreeShipping"] ? "1" : "0"); ddlIsOneTimeDiscount.SelectedValue = ((bool)dr["IsOneTimeDiscount"] ? "1" : "0"); chkAppliesToAll.Checked = (bool)dr["AppliesToAllItems"]; } } protected void lbGenerateNew_Click(object sender, EventArgs e) { ASCIIEncoding asc = new ASCIIEncoding(); Random r = new Random(); byte []bytes = new byte[8]; r.NextBytes(bytes); for(int i = 0; i < bytes.Length; i++) bytes[i] = (byte)((bytes[i] % 26) + 65); txtDiscountCode.Text = asc.GetString(bytes); } protected override void OnPreRender(EventArgs e) { if (!chkAppliesToAll.Checked) { Menu1.Items[1].Enabled = true; Menu1.Items[2].Enabled = true; } else { Menu1.Items[1].Enabled = false; Menu1.Items[2].Enabled = false; } base.OnPreRender(e); } #region Item Database Functions protected void btnSearch_Click(object sender, EventArgs e) { if (txtSearchName.Text.Length == 0 && txtSearchSKU.Text.Length == 0) { BindFindItem(false); } else { BindFindItem(true); } } protected void btnAdd_Click(object sender, EventArgs e) { foreach (GridViewRow gvr in gvItemDatabase.Rows) { if (gvr.RowType == DataControlRowType.DataRow) { CheckBox cb = (CheckBox)gvr.Cells[0].Controls[1]; if (cb.Checked) { int ItemID = (int)gvItemDatabase.DataKeys[gvr.RowIndex].Value; Database.DiscountItemMap_Add(DiscountID, ItemID); } } } Menu1.Items[1].Selected = true; mvContent.ActiveViewIndex = 1; BindItemList(); } protected void btnRemoveSelected_Click(object sender, EventArgs e) { foreach (GridViewRow gvr in gvItems.Rows) { if (gvr.RowType == DataControlRowType.DataRow) { CheckBox cb = (CheckBox)gvr.Cells[0].Controls[1]; if (cb.Checked) { int ItemID = (int)gvItems.DataKeys[gvr.RowIndex].Value; Database.DiscountItemMap_Delete(DiscountID, ItemID); } } } BindItemList(); } #endregion protected void Menu1_MenuItemClick(object sender, MenuEventArgs e) { mvContent.ActiveViewIndex = Int32.Parse(e.Item.Value); if (mvContent.ActiveViewIndex == 1) BindItemList(); else if (mvContent.ActiveViewIndex == 2) BindFindItem(false); } private void BindItemList() { DataTable dt = Database.Items_GetByDiscountID(DiscountID); gvItems.DataSource = dt; gvItems.DataBind(); } private void BindFindItem(bool FullSearch) { DataTable dt; if (FullSearch) { string srchSKU = "%"; if (txtSearchSKU.Text.Length > 0) srchSKU += txtSearchSKU.Text + "%"; string srchName = "%"; if (txtSearchName.Text.Length > 0) srchName += txtSearchName.Text + "%"; dt = Database.DiscountItemMap_SearchForItemsNotIncluded(DiscountID, srchSKU, srchName); } else { dt = Database.DiscountItemMap_GetAllItemsNotIncluded(DiscountID); } gvItemDatabase.DataSource = dt; gvItemDatabase.DataBind(); } protected void chkAppliesToAll_CheckedChanged(object sender, EventArgs e) { if (!chkAppliesToAll.Checked) { Menu1.Items[1].Enabled = true; Menu1.Items[2].Enabled = true; BindFindItem(false); Menu1.Items[2].Selected = true; mvContent.ActiveViewIndex = 2; } } protected void btnSave_Click(object sender, EventArgs e) { if (DiscountID == 0) { DiscountID = Database.Discounts_Add(txtDiscountTitle.Text, txtDiscountCode.Text, calStartDate.SelectedValue.Value, calEndDate.SelectedValue.Value, (ddlIsOneTimeDiscount.SelectedValue == "1"), false, Decimal.Parse(txtRequiredSubtotal.Text), Decimal.Parse(txtDiscountDollarAmount.Text), Int32.Parse(txtDiscountPercentage.Text), (ddlGrantsFreeShipping.SelectedValue == "1"), chkAppliesToAll.Checked); Database.DiscountItemMap_UpdateDiscountID(DiscountID); } else { Database.Discounts_Update(DiscountID, txtDiscountTitle.Text, txtDiscountCode.Text, calStartDate.SelectedValue.Value, calEndDate.SelectedValue.Value, (ddlIsOneTimeDiscount.SelectedValue == "1"), false, Decimal.Parse(txtRequiredSubtotal.Text), Decimal.Parse(txtDiscountDollarAmount.Text), Int32.Parse(txtDiscountPercentage.Text), (ddlGrantsFreeShipping.SelectedValue == "1"), chkAppliesToAll.Checked); } Response.Redirect("Discounts.aspx"); } protected void btnDelete_Click(object sender, EventArgs e) { Database.Discounts_Delete(DiscountID); Response.Redirect("Discounts.aspx"); } protected void btnSelectAll_Click(object sender, EventArgs e) { foreach (GridViewRow gvr in gvItemDatabase.Rows) { if (gvr.RowType == DataControlRowType.DataRow) { CheckBox cb = (CheckBox)gvr.Cells[0].Controls[1]; cb.Checked = true; } } } }