Il codice di una Web Part per generare un menu con lo stile della quick launch di SharePoint 2007 (WSS3 - MOSS).

C#

#region Using
using System;
using System.Data;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
#endregion

namespace Sgart
{
    [Guid("9EF9849C-C102-41a3-BA16-466C878E2149")]
    public class SgartAspMenu: System.Web.UI.WebControls.WebParts.WebPart
    {
        private AspMenu mnu;

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            LiteralControl lc1 = new LiteralControl("<div class=\"ms-quickLaunch\" style=\"width:100%\">");
            this.Controls.Add(lc1);

            //add menu
            mnu = new AspMenu();
            mnu.ID = "MyMenu";
            mnu.StaticDisplayLevels = 2;
            mnu.ItemWrap = true;
            mnu.MaximumDynamicDisplayLevels = 0;
            mnu.StaticSubMenuIndent = 0;
            mnu.SkipLinkText = "";

            MenuItemStyle mis;
            SubMenuStyle sms;

            mis = new MenuItemStyle();
            mis.CssClass = "ms-navheader";
            mis.Font.Underline = false;
            mnu.LevelMenuItemStyles.Add(mis);
            mis = new MenuItemStyle();
            mis.CssClass = "ms-navitem";
            mis.Font.Underline = false;
            mnu.LevelMenuItemStyles.Add(mis);

            sms = new SubMenuStyle();
            sms.CssClass = "ms-navSubMenu1";
            sms.Font.Underline = false;
            mnu.LevelSubMenuStyles.Add(sms);
            sms = new SubMenuStyle();
            sms.CssClass = "ms-navSubMenu2";
            sms.Font.Underline = false;
            mnu.LevelSubMenuStyles.Add(sms);

            mis = new MenuItemStyle();
            mis.CssClass = "ms-selectednavheader";
            mis.Font.Underline = false;
            mnu.LevelSelectedStyles.Add(mis);
            mis = new MenuItemStyle();
            mis.CssClass = "ms-selectednav";
            mis.Font.Underline = false;
            mnu.LevelSelectedStyles.Add(mis);

            this.Controls.Add(mnu);
         

            LiteralControl lc2 = new LiteralControl("</div>");
            this.Controls.Add(lc2);
        }

        //load example data        
        private void LoadAspMenu(AspMenu mnu)
        {
            MenuItem mi;
            MenuItem mi2;

            //1
            mi = new MenuItem("Menu 1");

            mi2 = new MenuItem("Item 1.a");
            string relativeUrl = SPContext.Current.Web.ServerRelativeUrl;
            mi2.ImageUrl = relativeUrl + "/_layouts/images/ADDTOFAVORITES.GIF";
            mi2.NavigateUrl = "http://www.sgart.it";
            mi.ChildItems.Add(mi2);
            mi2 = new MenuItem("Item 1.b");
            mi.ChildItems.Add(mi2);

            mnu.Items.Add(mi);

            //2
            mi = new MenuItem("Menu 2");

            mi2 = new MenuItem("Item 2.1");
            mi.ChildItems.Add(mi2);
            mi2 = new MenuItem("Item 2.2");
            mi.ChildItems.Add(mi2);
            mi2 = new MenuItem("Item 2.3");
            mi.ChildItems.Add(mi2);

            mnu.Items.Add(mi);
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                EnsureChildControls();
                LoadAspMenu(mnu);
            }
        }
    }
}
Tags:
SharePoint498 SharePoint 2007218
Potrebbe interessarti anche: