This is a migrated thread and some comments may be shown as answers.

Populating RadMenu from both sitemap and database

1 Answer 135 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Aaron
Top achievements
Rank 1
Aaron asked on 03 Apr 2018, 07:21 PM

     I'd like to populate my menu from the sitemap, utilizing the SecurityTrimmingEnabled feature, and also add some additional menu nodes from a database.

 

Before I added the sitemap and SecurityTrimming I was doing this to dynamically add records to my Help menu.  Now it's not adding the child menus.

 

private void LoadHelpMenu()
        {
            List<HelpDocumentation> menuItems = new List<HelpDocumentation>();
            menuItems = BaseUserProcess.GetHelpMenuItems();

            if (menuItems != null)
            {
                RadMenuItem HelpMenu = radMenuNavigation.Items.Where(f => f.Text == "Help").FirstOrDefault();
                HelpMenu = LoadMenuNode(menuItems, HelpMenu);

                string version = typeof(WorkBench).Assembly.GetName().Version.ToString();
                RadMenuItem Appversion = new RadMenuItem("Version: " + version);
                HelpMenu.Items.Add(Appversion);
            }
            else
            {
                radMenuNavigation.Items.Where(f => f.Text == "Help").FirstOrDefault().Visible = false;
            }

        }

        private RadMenuItem LoadMenuNode(List<HelpDocumentation> menuItems, RadMenuItem node, int? ParentID = null)
        {
            List<HelpDocumentation> GroupingItems = new List<HelpDocumentation>();
            if (ParentID == null)
            {
                GroupingItems = menuItems.Where(F => F.ParentID.HasValue == false).ToList();
            }
            else
            {
                GroupingItems = menuItems.Where(F => F.ParentID == ParentID).ToList();
            }

            foreach (var item in GroupingItems.OrderBy(F => F.DisplayDescription).ToList())
            {
                RadMenuItem i = new RadMenuItem(item.DisplayDescription);
                if (!string.IsNullOrEmpty(item.Link))
                    i.NavigateUrl = item.Link;
                if (!string.IsNullOrEmpty(item.Target))
                    i.Target = item.Target;
                if (menuItems.Any(F => F.ParentID == item.ID))
                    i = LoadMenuNode(menuItems, i, item.ID);
                node.Items.Add(i);
            }
            return node;
        }

1 Answer, 1 is accepted

Sort by
0
Marin Bratanov
Telerik team
answered on 06 Apr 2018, 02:15 PM
Hello Aaron,

I do not see anything obviously wrong with this code. Can you step through it and see what the collections hold and how looping through them goes in both cases? Perhaps something goes amiss with the data source and the loops do not get executed.

I can also suggest you look into reading the sitemap programmatically and create all menu items programmatically instead of using data binding.

Another test you can run is:

  1. take the sample from this post: https://www.telerik.com/forums/multi-column-amp-sitemap-datasource#DIcRKiCLGE2vpaOrgg-KOg
  2. make the menu add data bound items:
    <telerik:RadMenu ID="RadMenu1" runat="server" DataSourceID="SiteMapDataSource1" Skin="Telerik" AppendDataBoundItems="true">
  3. add items:
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            RadMenu1.Items.Add(new Telerik.Web.UI.RadMenuItem("test item"));
        }
    }

 


Regards,
Marin Bratanov
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Menu
Asked by
Aaron
Top achievements
Rank 1
Answers by
Marin Bratanov
Telerik team
Share this question
or