public string RetrieveParentCategories() { try { if (Context.User.IsInRole("User")) { List<Category> myCategories = new CategoriesLogic().RetrieveParentCategories().ToList(); string HTML = ""; foreach (Category myCategory in myCategories) { HTML += "<option value=\"" + myCategory.Id + "\">" + myCategory.Category1 + "</option>"; } return HTML; } else { return ""; } } catch (Exception Exception) { throw Exception; } }
protected void Page_Load(object sender, EventArgs e) { try { MaintainScrollPositionOnPostBack = true; LoadProducts = true; if (!Page.IsPostBack) { IQueryable<Category> myCategories = new CategoriesLogic().RetrieveAllChildCategories(); IQueryable<SuppliersView> mySuppliers = new SuppliersLogic().RetrieveAllSuppliers(); if ((mySuppliers.Count() > 0) && (myCategories.Count() > 0)) { gvProducts.DataSource = new ProductsLogic().RetrieveAllProducts(); gvProducts.DataBind(); ddlStatus.Items.Add(new ListItem("Active", "true")); ddlStatus.Items.Add(new ListItem("Inactive", "false")); ddlCategory.DataSource = myCategories; ddlCategory.DataTextField = "Category1"; ddlCategory.DataValueField = "Id"; ddlCategory.DataBind(); ddlCategory.Items.Insert(0, new ListItem("Select", "0")); ddlSupplier.DataSource = mySuppliers; ddlSupplier.DataTextField = "Supplier"; ddlSupplier.DataValueField = "Id"; ddlSupplier.DataBind(); ddlSupplier.Items.Insert(0, new ListItem("Select", "0")); LoadProducts = true; } else { lblServerSideError.Text = "Suppliers and Categories must be added in order to add Products"; LoadProducts = false; } } } catch (Exception Exception) { throw Exception; } }
/// <summary> /// Occurs when the Sub Categories Grid View Row is being deleted /// Level: External /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void gvSubCategories_RowDeleting(object sender, GridViewDeleteEventArgs e) { try { Category myCategory = new CategoriesLogic().RetrieveCategoryByID(Convert.ToInt32(e.Keys[0])); string PreviousURL = myCategory.ImageURL; if (new CategoriesLogic().DeleteCategory(myCategory.Id) == false) { lblServerSideErrorTop.Text = "Category is Bound to One or More Products and Cannot be Deleted"; } else { File.Delete(Server.MapPath(PreviousURL)); gvCategories.DataSource = new CategoriesLogic().RetrieveParentCategories(); gvCategories.DataBind(); gvSubCategories.DataSource = new CategoriesLogic().RetrieveChildCategories(Convert.ToInt32(myCategory.CategoryFK)); gvSubCategories.DataBind(); gvCategories.SelectedIndex = -1; gvSubCategories.SelectedIndex = -1; txtCategory.Text = ""; imgSelectedCategory.Visible = false; btnUpdate.Visible = false; ReqValImage.Visible = true; btnAdd.Visible = true; lblServerSideErrorBottom.Text = ""; lblServerSideErrorTop.Text = " "; ddlParent.DataSource = new CategoriesLogic().RetrieveParentCategories(); ddlParent.DataTextField = "Category1"; ddlParent.DataValueField = "Id"; ddlParent.DataBind(); ddlParent.Items.Insert(0, new ListItem("Set: Parent", "0")); } } catch (Exception Exception) { throw Exception; } }
/// <summary> /// Occurs when the Sub Categories Grid View Selected index is changed /// Level: External /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void gvSubCategories_SelectedIndexChanged(object sender, EventArgs e) { try { gvCategories.SelectedIndex = -1; ReqValImage.Visible = false; btnAdd.Visible = false; btnUpdate.Visible = true; lblServerSideErrorBottom.Text = ""; lblServerSideErrorTop.Text = " "; Category myCategory = new CategoriesLogic().RetrieveCategoryByID(Convert.ToInt32(gvSubCategories.SelectedValue)); txtCategory.Text = myCategory.Category1; imgSelectedCategory.Visible = true; imgSelectedCategory.ImageUrl = Page.ResolveClientUrl(myCategory.ImageURL); ddlParent.DataSource = new CategoriesLogic().RetrieveParentCategories(); ddlParent.DataTextField = "Category1"; ddlParent.DataValueField = "Id"; ddlParent.DataBind(); ddlParent.Items.Insert(0, new ListItem("Set: Parent", "0")); ddlParent.SelectedValue = myCategory.CategoryFK.ToString(); } catch (Exception Exception) { throw Exception; } }
/// <summary> /// Occurs when the Update Button is Clicked /// Level: External /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnUpdate_Click(object sender, EventArgs e) { try { if (Page.IsValid) { lblServerSideErrorBottom.Text = ""; lblServerSideErrorTop.Text = " "; CategoryShift ShiftedAs = CategoryShift.Nothing; if (gvCategories.SelectedIndex == -1) { //The Category is a Child int CategoryID = Convert.ToInt32(gvSubCategories.SelectedValue); string Category = txtCategory.Text.Trim(); if (ddlParent.SelectedIndex == 0) { //Set the Category as a Parent Category Tuple<string, UploadResult> myTuple = UploadImage(fuImage); if (myTuple.Item2 == UploadResult.Successful) { string PreviousURL = new CategoriesLogic().RetrieveCategoryByID(Convert.ToInt32(gvSubCategories.SelectedValue)).ImageURL; if (new CategoriesLogic().AssignCategoryAsParent(CategoryID, Category, myTuple.Item1)) { File.Delete(Server.MapPath(PreviousURL)); imgSelectedCategory.ImageUrl = Page.ResolveClientUrl(myTuple.Item1); ShiftedAs = CategoryShift.ChildToParent; } else { File.Delete(Server.MapPath(myTuple.Item1)); lblServerSideErrorTop.Text = "Category Already Exists or is Bound to One or More Products and Cannot be Elevated"; ShiftedAs = CategoryShift.ChildCategoryError; } } else if (myTuple.Item2 == UploadResult.NoImageFound) { if (new CategoriesLogic().AssignCategoryAsParent(CategoryID, Category, null)) { ShiftedAs = CategoryShift.ChildToParent; } else { lblServerSideErrorTop.Text = "Category Already Exists or is Bound to One or More Products and Cannot be Elevated"; ShiftedAs = CategoryShift.ChildCategoryError; } } else { //inval extens lblServerSideErrorBottom.Text = "Invalid Image Extension"; ShiftedAs = CategoryShift.Error; } } else { //Set the Category as a Child Category Tuple<string, UploadResult> myTuple = UploadImage(fuImage); if (myTuple.Item2 == UploadResult.Successful) { string PreviousURL = new CategoriesLogic().RetrieveCategoryByID(Convert.ToInt32(gvSubCategories.SelectedValue)).ImageURL; if (new CategoriesLogic().AssignCategoryAsChild(CategoryID, Category, myTuple.Item1, Convert.ToInt32(ddlParent.SelectedValue))) { File.Delete(Server.MapPath(PreviousURL)); imgSelectedCategory.ImageUrl = Page.ResolveClientUrl(myTuple.Item1); ShiftedAs = CategoryShift.ChildToChild; } else { File.Delete(Server.MapPath(myTuple.Item1)); lblServerSideErrorTop.Text = "Child Category Already Exists"; ShiftedAs = CategoryShift.ChildCategoryError; } } else if (myTuple.Item2 == UploadResult.NoImageFound) { if (new CategoriesLogic().AssignCategoryAsChild(CategoryID, Category, null, Convert.ToInt32(ddlParent.SelectedValue))) { ShiftedAs = CategoryShift.ChildToChild; } else { lblServerSideErrorTop.Text = "Child Category Already Exists"; ShiftedAs = CategoryShift.ChildCategoryError; } } else { //extension error lblServerSideErrorBottom.Text = "Invalid Image Extension"; ShiftedAs = CategoryShift.Error; } } } else { //The Category is a Parent int CategoryID = Convert.ToInt32(gvCategories.SelectedValue); string Category = txtCategory.Text.Trim(); if (ddlParent.SelectedIndex == 0) { //Set the Category as a Parent Category Tuple<string, UploadResult> myTuple = UploadImage(fuImage); if (myTuple.Item2 == UploadResult.Successful) { string PreviousURL = new CategoriesLogic().RetrieveCategoryByID(Convert.ToInt32(gvCategories.SelectedValue)).ImageURL; if (new CategoriesLogic().AssignCategoryAsParent(CategoryID, Category, myTuple.Item1)) { File.Delete(Server.MapPath(PreviousURL)); imgSelectedCategory.ImageUrl = Page.ResolveClientUrl(myTuple.Item1); ShiftedAs = CategoryShift.ParentToParent; } else { File.Delete(Server.MapPath(myTuple.Item1)); lblServerSideErrorTop.Text = "Parent Category Already Exists"; ShiftedAs = CategoryShift.Error; } } else if (myTuple.Item2 == UploadResult.NoImageFound) { if (new CategoriesLogic().AssignCategoryAsParent(CategoryID, Category, null)) { ShiftedAs = CategoryShift.ParentToParent; } else { lblServerSideErrorTop.Text = "Parent Category Already Exists"; ShiftedAs = CategoryShift.Error; } } else { //extension error lblServerSideErrorBottom.Text = "Invalid Image Extension"; ShiftedAs = CategoryShift.Error; } } else { //Set the Category as a Child Category Tuple<string, UploadResult> myTuple = UploadImage(fuImage); Category myCategory = new CategoriesLogic().RetrieveCategoryByID(Convert.ToInt32(gvCategories.SelectedValue)); string PreviousURL = myCategory.ImageURL; string PreviousCategory = myCategory.Category1; if (myTuple.Item2 == UploadResult.Successful) { if (new CategoriesLogic().AssignCategoryAsChild(CategoryID, Category, myTuple.Item1, Convert.ToInt32(ddlParent.SelectedValue)) == false) { //error cannot sublevel this category File.Delete(Server.MapPath(myTuple.Item1)); lblServerSideErrorTop.Text = "Category Already Exists or has Existing Child Categories and Cannot be Sub Levelled"; ShiftedAs = CategoryShift.Error; ddlParent.SelectedIndex = 0; txtCategory.Text = PreviousCategory; } else { File.Delete(Server.MapPath(PreviousURL)); ShiftedAs = CategoryShift.ParentToChild; imgSelectedCategory.ImageUrl = Page.ResolveClientUrl(myTuple.Item1); } } else if (myTuple.Item2 == UploadResult.NoImageFound) { if (new CategoriesLogic().AssignCategoryAsChild(CategoryID, Category, null, Convert.ToInt32(ddlParent.SelectedValue)) == false) { //error cannot sublevel this category lblServerSideErrorTop.Text = "Category Already Exists or has Existing Child Categories and Cannot be Sub Levelled"; ShiftedAs = CategoryShift.Error; ddlParent.SelectedIndex = 0; txtCategory.Text = PreviousCategory; } else { ShiftedAs = CategoryShift.ParentToChild; } } else { //extension error lblServerSideErrorBottom.Text = "Invalid Image Extension"; ShiftedAs = CategoryShift.Error; } } } //Setting Grid Views Depending on Update Action if (ShiftedAs == CategoryShift.ChildToChild) { Category myParentCategory = new CategoriesLogic().RetrieveCategoryByID(Convert.ToInt32(ddlParent.SelectedValue)); gvCategories.DataSource = new CategoriesLogic().RetrieveParentCategories(); gvCategories.DataBind(); foreach (GridViewRow myRow in gvCategories.Rows) { if (gvCategories.DataKeys[myRow.RowIndex].Value.Equals(myParentCategory.Id)) { gvCategories.SelectedIndex = myRow.RowIndex; break; } } Category myChildCategory = new CategoriesLogic().RetrieveCategoryByID(Convert.ToInt32(gvSubCategories.SelectedValue)); gvSubCategories.DataSource = new CategoriesLogic().RetrieveChildCategories(Convert.ToInt32(gvCategories.SelectedValue)); gvSubCategories.DataBind(); foreach (GridViewRow myRow in gvSubCategories.Rows) { if (gvSubCategories.DataKeys[myRow.RowIndex].Value.Equals(myChildCategory.Id)) { gvCategories.SelectedIndex = -1; gvSubCategories.SelectedIndex = myRow.RowIndex; break; } } } else if (ShiftedAs == CategoryShift.ChildToParent) { Category myParentCategory = new CategoriesLogic().RetrieveCategoryByID(Convert.ToInt32(gvSubCategories.SelectedValue)); gvCategories.DataSource = new CategoriesLogic().RetrieveParentCategories(); gvCategories.DataBind(); foreach (GridViewRow myRow in gvCategories.Rows) { if (gvCategories.DataKeys[myRow.RowIndex].Value.Equals(myParentCategory.Id)) { gvCategories.SelectedIndex = myRow.RowIndex; break; } } gvSubCategories.DataSource = new CategoriesLogic().RetrieveChildCategories(Convert.ToInt32(gvCategories.SelectedValue)); gvSubCategories.DataBind(); } else if (ShiftedAs == CategoryShift.ParentToChild) { Category myChildCategory = new CategoriesLogic().RetrieveCategoryByID(Convert.ToInt32(gvCategories.SelectedValue)); gvSubCategories.DataSource = new CategoriesLogic().RetrieveChildCategories(Convert.ToInt32(ddlParent.SelectedValue)); gvSubCategories.DataBind(); foreach (GridViewRow myRow in gvSubCategories.Rows) { if (gvSubCategories.DataKeys[myRow.RowIndex].Value.Equals(myChildCategory.Id)) { gvSubCategories.SelectedIndex = myRow.RowIndex; break; } } gvCategories.DataSource = new CategoriesLogic().RetrieveParentCategories(); gvCategories.DataBind(); gvCategories.SelectedIndex = -1; } else if ((ShiftedAs == CategoryShift.ParentToParent) || (ShiftedAs == CategoryShift.Error) || (ShiftedAs == CategoryShift.ChildCategoryError)) { //if ((gvCategories.SelectedIndex == -1) && (gvSubCategories.SelectedIndex == -1)) //{ // gvCategories.DataSource = new CategoriesLogic().RetrieveParentCategories(); // gvCategories.DataBind(); // gvSubCategories.DataSource = new CategoriesLogic().RetrieveChildCategories(Convert.ToInt32(gvCategories.SelectedValue)); // gvSubCategories.DataBind(); //} //Error populating if ((gvSubCategories.SelectedIndex == -1) && (gvCategories.SelectedIndex > -1)) { Category myCategory = new CategoriesLogic().RetrieveCategoryByID(Convert.ToInt32(gvCategories.SelectedValue)); gvCategories.DataSource = new CategoriesLogic().RetrieveParentCategories(); gvCategories.DataBind(); foreach (GridViewRow myRow in gvCategories.Rows) { if (gvCategories.DataKeys[myRow.RowIndex].Value.Equals(myCategory.Id)) { gvCategories.SelectedIndex = myRow.RowIndex; break; } } gvSubCategories.DataSource = new CategoriesLogic().RetrieveChildCategories(Convert.ToInt32(gvCategories.SelectedValue)); gvSubCategories.DataBind(); txtCategory.Text = myCategory.Category1; ddlParent.SelectedValue = "0"; //dropdown?? } else if ((gvCategories.SelectedIndex == -1) && (gvSubCategories.SelectedIndex > -1)) { Category mySubCategory = new CategoriesLogic().RetrieveCategoryByID(Convert.ToInt32(gvSubCategories.SelectedValue)); gvSubCategories.DataSource = new CategoriesLogic().RetrieveChildCategories(Convert.ToInt32(mySubCategory.CategoryFK)); gvSubCategories.DataBind(); foreach (GridViewRow myRow in gvSubCategories.Rows) { if (gvSubCategories.DataKeys[myRow.RowIndex].Value.Equals(mySubCategory.Id)) { gvSubCategories.SelectedIndex = myRow.RowIndex; break; } } txtCategory.Text = mySubCategory.Category1; ddlParent.SelectedValue = mySubCategory.CategoryFK.ToString(); //dropdown?? } } } } catch (Exception Exception) { throw Exception; } }