using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.UI; using System.Collections.Generic; public partial class Admin_Controls_PageHierarchy : WebBlockUserControl { #region Private Members private bool AlternatingRow = true; private DataTable BreadcrumbTable = null; private string expandUrl; private string collapseUrl; #endregion #region Public Properties private int _CategoryID = 0; public int CategoryID { get { return _CategoryID; } set { _CategoryID = value; } } #endregion protected void Page_Load(object sender, EventArgs e) { // get our web resources expandUrl = Page.ClientScript.GetWebResourceUrl(typeof(System.Web.UI.WebControls.TreeView), "TreeView_Default_Expand.gif"); collapseUrl = Page.ClientScript.GetWebResourceUrl(typeof(System.Web.UI.WebControls.TreeView), "TreeView_Default_Collapse.gif"); string jsText = String.Format("var expandUrl='{0}'; var collapseUrl='{1}';", expandUrl, collapseUrl); Page.ClientScript.RegisterClientScriptBlock(typeof(string), "urls", jsText, true); } protected override void Render(HtmlTextWriter writer) { DataTable dtCats = Database.Categories_GetRootCategories(); DataTable dtProds = Database.Selectors_GetByCategoryID(0); // if we need to open up to a specific category, we'll need the breadcrumb trail to get there if (CategoryID > 0) BreadcrumbTable = Database.Categories_GetBreadcrumbTrail(_CategoryID, 0, false); // now call a function to render out this datatable correctly // at the render depth of 0 RenderDataTable(writer, dtCats, dtProds, 0); } private void RenderDataTable(HtmlTextWriter writer, DataTable Categories, DataTable Products, int RenderDepth) { // we want to iterate through the first layer of subcategories // until we hit the category we need to expand on // this is returned as the last row of GetBreadcrumbTrail int TargetCategoryID = 0; if (BreadcrumbTable != null && BreadcrumbTable.Rows.Count - RenderDepth > 0) TargetCategoryID = (int)BreadcrumbTable.Rows[BreadcrumbTable.Rows.Count - RenderDepth - 1]["CategoryID"]; // enclosing table writer.Write(""); if (Categories.Rows.Count == 0) { writer.Write(""); } else { // render the sort/add links if (RenderDepth == 0) { // for the root.. writer.Write(""); } else { // for everyone else writer.Write(""); } foreach (DataRow dr in Categories.Rows) { AlternatingRow = !AlternatingRow; writer.Write(""); if ((int)dr["CategoryID"] == TargetCategoryID) { writer.Write(""); } } if (RenderDepth > 0) { if (Products.Rows.Count == 0) { writer.Write(""); } else { // render the sort/add links writer.Write(""); foreach (DataRow dr in Products.Rows) { AlternatingRow = !AlternatingRow; writer.Write(""); writer.Write(""); } } } // close the table writer.Write("
0 Categories: Add New Category
"); writer.Write(Categories.Rows.Count.ToString()); writer.Write(" Categories: Sort | Add New Category
"); writer.Write(Categories.Rows.Count.ToString()); writer.Write(" Sub-Categories: Sort | Add New Category
"); writer.Write(""); } else { writer.Write(""); writer.Write(""); } writer.Write(""); writer.Write((string)dr["CategoryName"]); writer.Write(""); if ((int)dr["CategoryID"] == TargetCategoryID) RenderSubCategories(writer, RenderDepth + 1); writer.Write("
0 Product Pages: Add New Product Page
"); writer.Write(Products.Rows.Count.ToString()); writer.Write(" Product Pages: Sort | Add New Category
"); writer.Write(""); if (dr["SKU"] != DBNull.Value && ((string)dr["SKU"]).Length > 0) { writer.Write("("); writer.Write((string)dr["SKU"]); writer.Write(") "); } writer.Write((string)dr["Name"]); writer.Write(""); writer.Write("
"); } private void RenderSubCategories(HtmlTextWriter writer, int RenderDepth) { int ThisCategoryID = (int)BreadcrumbTable.Rows[BreadcrumbTable.Rows.Count - RenderDepth]["CategoryID"]; writer.Write("
"); DataTable dtCats = Database.Categories_GetChildren(ThisCategoryID); DataTable dtProds = Database.Selectors_GetByCategoryID(ThisCategoryID); CategoryID = ThisCategoryID; RenderDataTable(writer, dtCats, dtProds, RenderDepth); writer.Write("
"); } }