private void AddLinkStyle(MenuList menu) { int minWidth = 15; foreach (Control li in menu.Controls) { Literal l = (Literal)li.Controls[0].Controls[1]; int len = l.Text.Length; if (len > minWidth) { minWidth = len; } } string style = (minWidth * 6.25) + "px"; menu.Style.Add("width", style); foreach (Control li in menu.Controls) { ((HyperLink)li.Controls[0]).Style.Add("width", style); foreach (Control c in li.Controls) { if (c is MenuList) { MenuList sub = (MenuList)c; //((HyperLink)li.Controls[0]).Style.Add("background-image", "url('../../Images/icon_calnextArrow.gif')"); //((HyperLink)li.Controls[0]).Style.Add("background-repeat", "no-repeat"); //((HyperLink)li.Controls[0]).Style.Add("background-position", "center right"); sub.Style.Add("left", (minWidth * 6.25) + "px"); //AddLinkStyle(sub); } } } }
/// <summary> /// Recursively binds a /// </summary> /// <param name="list"></param> /// <param name="node"></param> private void RecursiveMenuBind(MenuList list, XmlNode node) { // Only bind Element nodes, not comments if (pedc.IsDisplayInMainMenu(node) && pedc.IsDisplayInView(node) && node.NodeType == XmlNodeType.Element) { MenuListItem Item = GetFormattedListItem(node); list.Controls.Add(Item); if (node.HasChildNodes) { howdeep++; if (howdeep < DEPTH || DEPTH < 0) { MenuList SubMenu = new MenuList(); Item.Controls.Add(SubMenu); foreach (XmlNode child in node.ChildNodes) { RecursiveMenuBind(SubMenu, child); AddLinkStyle(SubMenu); } // Logic to set static heights and overflow // Need to set widths inline to prevent collapsing in som DOM browsers if (!string.IsNullOrEmpty(StaticHeight) && SubMenu.Controls.Count > 20) { SubMenu.Style.Add("height", StaticHeight + "px"); if (SubMenu.Controls[0] is MenuListItem) { if (SubMenu.Controls[0].HasControls()) { if (SubMenu.Controls[0].Controls[0] is HyperLink) { string width = ((HyperLink)SubMenu.Controls[0].Controls[0]).Style["width"]; if (!string.IsNullOrEmpty(width)) { string subMenuWidth = int.Parse(width.Replace("em", "")) + 2 + "em"; SubMenu.Style.Add("width", subMenuWidth); } } } } SubMenu.Style.Add("overflow", "auto"); } if (!SubMenu.HasControls() && SubMenu.Parent != null) { SubMenu.Parent.Controls.Remove(SubMenu); } } howdeep--; } } }
/// <summary> /// Returns an hierarchical bound HTML unordered list /// </summary> /// <returns></returns> private MenuList LoadMenusFromXml() { // If no datasource or xmlfilename is specified, return empty menu if (this.DataSource == null && this.DataSourceID == "" && this.XMLMenuFile == "" && this.xDoc == null) { return(new MenuList()); } // Else construct a new menu based on datasource else { if (this.xDoc != null) { } else { xDoc = new XmlDocument(); // If an XmlDataSource is specified, use as binding for menu if (this.DataSource != null) { if (this.DataSource is XmlDataSource) { XmlDataSource source = (XmlDataSource)this.DataSource; xDoc = source.GetXmlDocument(); XPath = source.XPath; } else if (this.DataSource is XmlDocument) { XmlDocument source = (XmlDocument)this.DataSource; xDoc = source; } } /// Else, use the DataSourceID to find the XMLDataSource Control else if (this.DataSourceID != "") { Control con = this.NamingContainer.FindControl(this.DataSourceID); if (con is XmlDataSource) { XmlDataSource source = (XmlDataSource)con; xDoc = source.GetXmlDocument(); XPath = source.XPath; } } // Else, use the file specified by the XMLMenuFile property else { xDoc.Load(this.MapPathSecure(XMLMenuFile)); } } // Setup Main Menu MenuList MainMenu = new MenuList(); MainMenu.ID = this.ID; XmlNodeList ParentNodes; if (XPath == "") { ParentNodes = xDoc.DocumentElement.ChildNodes; } else { ParentNodes = xDoc.SelectNodes(XPath); } foreach (XmlNode Parent in ParentNodes) { RecursiveMenuBind(MainMenu, Parent); } return(MainMenu); } }