0

I have an menu control with a definition that starts out so:

<asp:Menu ID="mnuDocs" runat="server" DataSourceID="SiteMapDataSource1"...

I want to be able to add items to the menu based on various circumstances, and have found some examples online. By way of testing the concept, I have tried code in the Page_Load like this:

        MenuItem newItem = new MenuItem("Second Item");
        newItem.NavigateUrl = "#SecondItem";
        mnuDocs.Items.Add(newItem);
        mnuDocs.Items.Add(new MenuItem("New Menu Item", "#NewMenuItem"));

This has no effect on the displayed menu, though inspecting the menu in debug shows the items are added. What I don't understand is that the items from the sitemap do not show up in the menu when I inspect it in debug, but only items from the sitemap are rendered on the page. What is it I am not understanding about how to go about this?

1 Answer 1

0

What I did not realize (because the menu items in the sitemap were rendering) is that the control had to be explicitly databound in order to add items. So, the problem was resolved by preceding the above code with:

    mnuDocs.DataSource = SiteMapDataSource1;
    mnuDocs.Databind();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.