using System; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Collections.Generic; namespace ADefWebserver.Modules.CategoryAdmin { public partial class View : DotNetNuke.Entities.Modules.PortalModuleBase { List<int> colProcessedCategoryIDs; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { DisplayCatagories(); tvCategories.CollapseAll(); } } #region DisplayCatagories private void DisplayCatagories() { CatagoriesTree colCatagories = new CatagoriesTree(); tvCategories.DataSource = colCatagories; TreeNodeBinding RootBinding = new TreeNodeBinding(); RootBinding.DataMember = "ListItem"; RootBinding.TextField = "Text"; RootBinding.ValueField = "Value"; tvCategories.DataBindings.Add(RootBinding); tvCategories.DataBind(); tvCategories.CollapseAll(); // If a node was selected previously select it again if (txtCategoryID.Text != "") { int intCategoryID = Convert.ToInt32(txtCategoryID.Text); TreeNode objTreeNode = (TreeNode)tvCategories.FindNode(GetNodePath(intCategoryID)); objTreeNode.Select(); objTreeNode.Expand(); // Expand it's parent nodes // Get the value of each parent node string[] strParentNodes = objTreeNode.ValuePath.Split(Convert.ToChar("/")); // Loop through each parent node for (int i = 0; i < objTreeNode.Depth; i++) { // Get the parent node TreeNode objParentTreeNode = (TreeNode)tvCategories.FindNode(GetNodePath(Convert.ToInt32(strParentNodes[i]))); // Expand the parent node objParentTreeNode.Expand(); } } else { //If there is at least one existing category, select it if (tvCategories.Nodes.Count > 0) { tvCategories.Nodes[0].Select(); txtCategoryID.Text = "0"; SelectTreeNode(); } else { // There is no data so set form to Add New SetFormToAddNew(); } } // If a node is selected, remove it from the BindDropDown drop-down int intCategoryNotToShow = -1; TreeNode objSelectedTreeNode = (TreeNode)tvCategories.SelectedNode; if (objSelectedTreeNode != null) { intCategoryNotToShow = Convert.ToInt32(tvCategories.SelectedNode.Value); } BindDropDown(intCategoryNotToShow); } #endregion #region BindDropDown private void BindDropDown(int intCategoryNotToShow) { // Bind drop-down CategoriesDropDown colCategoriesDropDown = new CategoriesDropDown(); ListItemCollection objListItemCollection = colCategoriesDropDown.Categories(intCategoryNotToShow); ddlParentCategory.DataSource = objListItemCollection; ddlParentCategory.DataTextField = "Text"; ddlParentCategory.DataValueField = "Value"; ddlParentCategory.DataBind(); } #endregion #region GetNodePath private string GetNodePath(int intCategoryID) { string strNodePath = intCategoryID.ToString(); CategoryAdminDALDataContext CategoryAdminDALDataContext = new CategoryAdminDALDataContext(); var result = (from AdefWebserverCategories in CategoryAdminDALDataContext.AdefWebserverCategories where AdefWebserverCategories.CategoryID == intCategoryID select AdefWebserverCategories).FirstOrDefault(); // Only build a node path if the current level is not the root if (result.Level > 1) { int intCurrentCategoryID = result.CategoryID; for (int i = 1; i < result.Level; i++) { var CurrentCategory = (from AdefWebserverCategories in CategoryAdminDALDataContext.AdefWebserverCategories where AdefWebserverCategories.CategoryID == intCurrentCategoryID select AdefWebserverCategories).FirstOrDefault(); strNodePath = CurrentCategory.ParentCategoryID.ToString() + @"/" + strNodePath; var ParentCategory = (from AdefWebserverCategories in CategoryAdminDALDataContext.AdefWebserverCategories where AdefWebserverCategories.CategoryID == CurrentCategory.ParentCategoryID select AdefWebserverCategories).FirstOrDefault(); intCurrentCategoryID = ParentCategory.CategoryID; } } return strNodePath; } #endregion #region tvCategories_SelectedNodeChanged protected void tvCategories_SelectedNodeChanged(object sender, EventArgs e) { SelectTreeNode(); ResetForm(); } #endregion #region SelectTreeNode private void SelectTreeNode() { if (tvCategories.SelectedNode != null) { if (tvCategories.SelectedNode.Value != "") { var result = (from AdefWebserverCategories in CategoriesTable.GetCategoriesTable() where AdefWebserverCategories.CategoryID == Convert.ToInt32(tvCategories.SelectedNode.Value) select AdefWebserverCategories).FirstOrDefault(); txtCategory.Text = result.CategoryName; txtCategoryID.Text = result.CategoryID.ToString(); // Remove Node from the Bind DropDown drop-down BindDropDown(result.CategoryID); // Set the Parent drop-down ddlParentCategory.SelectedValue = (result.ParentCategoryID == null) ? "0" : result.ParentCategoryID.ToString(); txtParentCategoryID.Text = (result.ParentCategoryID == null) ? "" : result.ParentCategoryID.ToString(); } } } #endregion #region btnUpdate_Click protected void btnUpdate_Click(object sender, EventArgs e) { CategoryAdminDALDataContext CategoryAdminDALDataContext = new CategoryAdminDALDataContext(); if (btnUpdate.Text == "Update") { var result = (from AdefWebserverCategories in CategoryAdminDALDataContext.AdefWebserverCategories where AdefWebserverCategories.CategoryID == Convert.ToInt32(txtCategoryID.Text) select AdefWebserverCategories).FirstOrDefault(); result.CategoryName = txtCategory.Text.Trim(); result.ParentCategoryID = (ddlParentCategory.SelectedValue == "0") ? (int?)null : Convert.ToInt32(ddlParentCategory.SelectedValue); txtParentCategoryID.Text = (ddlParentCategory.SelectedValue == "0") ? "" : ddlParentCategory.SelectedValue; result.Level = (ddlParentCategory.SelectedValue == "0") ? 1 : GetLevelOfParent(Convert.ToInt32(ddlParentCategory.SelectedValue)) + 1; CategoryAdminDALDataContext.SubmitChanges(); // Update levels off all the Children colProcessedCategoryIDs = new List<int>(); UpdateLevelOfChildren(result); } else { // This is a Save for a new Node txtParentCategoryID.Text = (ddlParentCategory.SelectedValue == "0") ? "" : ddlParentCategory.SelectedValue; AdefWebserverCategory objAdefWebserverCategory = new AdefWebserverCategory(); objAdefWebserverCategory.CategoryName = txtCategory.Text.Trim(); objAdefWebserverCategory.ParentCategoryID = (ddlParentCategory.SelectedValue == "0") ? (int?)null : Convert.ToInt32(ddlParentCategory.SelectedValue); objAdefWebserverCategory.Level = (ddlParentCategory.SelectedValue == "0") ? 1 : GetLevelOfParent(Convert.ToInt32(ddlParentCategory.SelectedValue)) + 1; CategoryAdminDALDataContext.AdefWebserverCategories.InsertOnSubmit(objAdefWebserverCategory); CategoryAdminDALDataContext.SubmitChanges(); // Set the Hidden CategoryID txtCategoryID.Text = objAdefWebserverCategory.CategoryID.ToString(); ResetForm(); } RefreshCache(); DisplayCatagories(); // Set the Parent drop-down if (txtParentCategoryID.Text != "") { ddlParentCategory.SelectedValue = txtParentCategoryID.Text; } } #endregion #region UpdateLevelOfChildren private void UpdateLevelOfChildren(AdefWebserverCategory result) { int? intStartingLevel = result.Level; if (colProcessedCategoryIDs == null) { colProcessedCategoryIDs = new List<int>(); } CategoryAdminDALDataContext CategoryAdminDALDataContext = new CategoryAdminDALDataContext(); // Get the children of the current item // This method may be called from the top level or recuresively by one of the child items var CategoryChildren = from AdefWebserverCategories in CategoryAdminDALDataContext.AdefWebserverCategories where AdefWebserverCategories.ParentCategoryID == result.CategoryID where !colProcessedCategoryIDs.Contains(result.CategoryID) select AdefWebserverCategories; if (CategoryChildren != null) { // Loop thru each item foreach (var objCategory in CategoryChildren) { colProcessedCategoryIDs.Add(objCategory.CategoryID); objCategory.Level = ((intStartingLevel) ?? 0) + 1; CategoryAdminDALDataContext.SubmitChanges(); //Recursively call the UpdateLevelOfChildren method adding all children UpdateLevelOfChildren(objCategory); } } } #endregion #region GetLevelOfParent private int? GetLevelOfParent(int? ParentCategoryID) { CategoryAdminDALDataContext CategoryAdminDALDataContext = new CategoryAdminDALDataContext(); var result = (from AdefWebserverCategories in CategoryAdminDALDataContext.AdefWebserverCategories where AdefWebserverCategories.CategoryID == ParentCategoryID select AdefWebserverCategories).FirstOrDefault(); return (result == null) ? 0 : result.Level; } #endregion #region btnAddNew_Click protected void btnAddNew_Click(object sender, EventArgs e) { if (btnAddNew.Text == "Add New") { SetFormToAddNew(); } else { // This is a Cancel ResetForm(); DisplayCatagories(); SelectTreeNode(); } } #endregion #region SetFormToAddNew private void SetFormToAddNew() { txtCategory.Text = ""; btnAddNew.Text = "Cancel"; btnUpdate.Text = "Save"; btnDelete.Visible = false; BindDropDown(-1); if (tvCategories.SelectedNode == null) { ddlParentCategory.SelectedValue = "0"; } else { ddlParentCategory.SelectedValue = tvCategories.SelectedNode.Value; } } #endregion #region ResetForm private void ResetForm() { btnAddNew.Text = "Add New"; btnUpdate.Text = "Update"; btnDelete.Visible = true; } #endregion #region btnDelete_Click protected void btnDelete_Click(object sender, EventArgs e) { CategoryAdminDALDataContext CategoryAdminDALDataContext = new CategoryAdminDALDataContext(); // Get the node var result = (from AdefWebserverCategories in CategoryAdminDALDataContext.AdefWebserverCategories where AdefWebserverCategories.CategoryID == Convert.ToInt32(txtCategoryID.Text) select AdefWebserverCategories).FirstOrDefault(); // Make a Temp object to use to update the child nodes AdefWebserverCategory TmpAdefWebserverCategory = new AdefWebserverCategory(); TmpAdefWebserverCategory.CategoryID = result.CategoryID; if (result.ParentCategoryID == null) { TmpAdefWebserverCategory.Level = 0; } else { TmpAdefWebserverCategory.Level = GetLevelOfParent(result.ParentCategoryID); } // Delete the node CategoryAdminDALDataContext.AdefWebserverCategories.DeleteOnSubmit(result); CategoryAdminDALDataContext.SubmitChanges(); // Update levels of all the Children UpdateLevelOfChildren(TmpAdefWebserverCategory); // Update all the children nodes to give them a new parent var CategoryChildren = from AdefWebserverCategories in CategoryAdminDALDataContext.AdefWebserverCategories where AdefWebserverCategories.ParentCategoryID == result.CategoryID select AdefWebserverCategories; // Loop thru each item foreach (var objCategory in CategoryChildren) { objCategory.ParentCategoryID = result.ParentCategoryID; CategoryAdminDALDataContext.SubmitChanges(); } RefreshCache(); // Set the CategoryID txtCategoryID.Text = (result.ParentCategoryID == null) ? "" : result.ParentCategoryID.ToString(); DisplayCatagories(); SelectTreeNode(); } #endregion #region RefreshCache private void RefreshCache() { // Get Table out of Cache object objCategoriesTable = HttpContext.Current.Cache.Get("CategoriesTable"); // Is the table in the cache? if (objCategoriesTable != null) { // Remove table from cache HttpContext.Current.Cache.Remove("CategoriesTable"); } } #endregion } }
Buy DotNetNuke Modules from Snowcovered |
DotNetNuke™ is a registered trademark of DotNetNuke Corporation.