Adding menu control on page programmatically

You can define the menu control programmatically by adding MenuItem objects into ItemCollection.
The following example showes how to create a new Menu control and add it to page:

// Menu item with server-side handler
var postBackItem = new MenuPostBack
                        {
                            Title = "PostBack",
                            CommandArgument = SPContext.Current.Web.Title
                        };
postBackItem.Click += (o, args) =>
                    {
                        ((MenuPostBack) o).Title = DateTime.Now.ToShortDateString();
                    };
// Create new menu
var simpleMenu = new Menu
{
    Title = "Simple menu",
    IconSize = MenuIconSize.Small,
    Width = new Unit("250px"),
    ItemCollection = new MenuItemCollection
    {
        // Link to page
        new MenuLink
            {
                Title = "My blog",
                Url = "http://blog.vitalyzhukov.ru",
                IconSrc = "/_layouts/images/ichtm.gif"
            },
        // Separator
        new MenuSeparator(),
        // Checkbox
        new MenuCheck
            {
                Title = "Checkbox",
                Checked = true
            },
        // Item raising PostBack
        postBackItem,
        // Submenu
        new MenuNested
            {
                Title = "SubMenu",
                ItemCollection = new MenuItemCollection
                                    {
                                        new MenuCheck
                                            { Title = "SubMenuCheck" },
                                        new MenuSeparator(),
                                        new MenuCheck
                                            { Title = "SubMenuCheck 2" },
                                    }
            }
    }
};
Controls.Add(simpleMenu);


The result on a page is this:




Last edited May 30, 2012 at 6:36 AM by VitalyZhukov, version 4

Comments

No comments yet.