using OboutInc.EasyMenu_Pro; using System.Web.UI.WebControls; using System.Web; using System; public class EasyMenuBindToSiteMap { #region "PROPERTIES" private int menuIndex = 1; private MenuShowEvent _ShowEvent = MenuShowEvent.MouseOver; public MenuShowEvent ShowEvent { get { return this._ShowEvent; } set { this._ShowEvent = value; } } private string _StyleFolder = string.Empty; public string StyleFolder { get { return this._StyleFolder; } set { this._StyleFolder = value; } } private bool _IsVertical = true; public bool IsVertical { get { return this._IsVertical; } set { this._IsVertical = value; } } private string _Width = "100px"; public string Width { get { return this._Width; } set { this._Width = value; } } private string _IDPrefix; public string IDPrefix { get { return this._IDPrefix; } set { this._IDPrefix = value; } } #endregion #region "METHODS" // use as starting menu the startmenu // place the new created menus inside the menuContainer // use as datasource the siteMapDataSource public void BuildMenu(EasyMenu startMenu, PlaceHolder menuContainer, SiteMapDataSource siteMapDataSource) { if (siteMapDataSource.Provider.RootNode == null) { startMenu.Visible = false; return; } int menuItemIndex = 1; SiteMapNodeCollection currentNodeCollection = siteMapDataSource.Provider.RootNode.ChildNodes; if (siteMapDataSource.ShowStartingNode) { // use the starting node as the root menu item SiteMapNode rootNode = siteMapDataSource.Provider.RootNode; string itemID = String.Format(IDPrefix + "Menu{0}_Item{1}", menuIndex.ToString(), menuItemIndex.ToString()); startMenu.AddMenuItem(itemID, rootNode.Title, string.Empty, rootNode.Url, string.Empty, string.Empty); } else { // use the first site map level as the root menu items foreach (SiteMapNode node in currentNodeCollection) { string itemID = String.Format(IDPrefix + "Menu{0}_Item{1}", menuIndex.ToString(), menuItemIndex.ToString()); startMenu.AddMenuItem(itemID, node.Title, string.Empty, node.Url, string.Empty, string.Empty); menuItemIndex++; } } // recusively build new menu for each new site map level menuItemIndex = 1; int currentMenuIndex = menuIndex; foreach (SiteMapNode node in currentNodeCollection) { if (node.ChildNodes.Count > 0) { string attachTo = String.Format(IDPrefix + "Menu{0}_Item{1}", currentMenuIndex.ToString(), menuItemIndex.ToString()); BuildSubMenu(attachTo, menuContainer, node.ChildNodes, 0); } menuItemIndex++; } menuContainer.Controls.Add(startMenu); } private void BuildSubMenu(string AttachTo, PlaceHolder menuContainer, SiteMapNodeCollection currentNodeCollection, int menuLevel) { EasyMenu SubMenu = new EasyMenu(); SubMenu.AttachTo = AttachTo; SubMenu.ShowEvent = ShowEvent; SubMenu.Position = MenuPosition.Vertical; SubMenu.Align = MenuAlign.Right; if (!IsVertical && menuLevel == 0) SubMenu.Align = MenuAlign.Under; SubMenu.Width = Width; SubMenu.ID = String.Format(IDPrefix + "Menu{0}", menuIndex); if (StyleFolder != string.Empty) SubMenu.StyleFolder = StyleFolder; menuContainer.Controls.Add(SubMenu); menuIndex++; int menuItemIndex = 1; foreach (SiteMapNode node in currentNodeCollection) { string itemID = String.Format(IDPrefix + "Menu{0}_Item{1}", menuIndex.ToString(), menuItemIndex.ToString()); SubMenu.AddMenuItem(itemID, node.Title, string.Empty, node.Url, string.Empty, string.Empty); menuItemIndex++; } menuItemIndex = 1; int currentMenuIndex = menuIndex; foreach (SiteMapNode node in currentNodeCollection) { if (node.ChildNodes.Count > 0) { string attachTo = String.Format(IDPrefix + "Menu{0}_Item{1}", currentMenuIndex.ToString(), menuItemIndex.ToString()); BuildSubMenu(attachTo, menuContainer, node.ChildNodes, 1); } menuItemIndex++; } } #endregion }