⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multilevelsectionmenu.cs

📁 ASP.NET精品全站程序SQL版.rar
💻 CS
字号:
namespace ASPNET.StarterKit.Communities {

    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.Caching;
    using System.Configuration;
    using System.Collections;
    using System.Data;



    //*********************************************************************
    //
    // MultiLevelSectionMenu Class
    //
	// SMR - Enh - New class for multilevel section menu
	//
    // WebControl that displays top-level sections. This control
    // takes into account the roles of the current user and displays
    // private sections only to users who have the right roles. It also
	// loads the the SubSection menu to handle rendering of the other tiers
	// of the menu structire.
    //
    //*********************************************************************
    public class MultiLevelSectionMenu : SkinnedCommunityControl {
    
        string _skinFileName = "Sections_MultiLevelSectionMenu.ascx";
        Repeater rptSections;

		private bool _expandAll = false;
		bool _showSelection = true;
		int _expandLevels = 0;
		int _currentLevel = 1;
        
		// SMR - Enh - indicates if a special style should be used for the section currently being viewed
		public bool ShowSelection
		{
			get { return _showSelection; }
			set { _showSelection = value; }
		}

		// SMR - Enh - indicates how many levels deep the tree can be exapanded
		public int ExpandLevels
		{
			get { return _expandLevels; }
			set { _expandLevels = value; }
		}

		// SMR - Enh - indicates if the section tree should always be expanded for all nodes
		public bool ExpandAll
		{
			get { return _expandAll; }
			set { _expandAll = value; }
		}

        //*********************************************************************
        //
        // MultiLevelSectionMenu Constructor
        //
        // Calls the base SkinnedCommunityControl constructor
        // and assigns the default page skin.
        //
        //*********************************************************************
        public MultiLevelSectionMenu() : base() 
		{
            // Assign a default template name
            if (SkinFileName == null)
                SkinFileName = _skinFileName;
        }


        //*********************************************************************
        //
        // SkinType Property
        //
        // Specifies the skins directory where this page's skin file is located.
        //
        //*********************************************************************
        override protected string SkinType {
            get { return "ControlSkins"; }
        }

   
        //*********************************************************************
        //
        // InitializeSkin Method
        //
        // Retrieves all the controls from the page skin.
        //
        //*********************************************************************
        override protected void InitializeSkin(Control skin) {

            // Get the Repeater from the Skin
            rptSections = (Repeater)GetControl( skin, "Sections" );
            rptSections.EnableViewState = false;
            rptSections.ItemDataBound += new RepeaterItemEventHandler(Repeater_ItemDataBound);
        }



        //*********************************************************************
        //
        // Repeater_ItemDataBound Method
        //
        // Assigns values to the controls contained in the item and 
        // alternating item templates of the Repeater used to display
        // the list of sections.
        //
        //*********************************************************************
        private void Repeater_ItemDataBound(Object s, RepeaterItemEventArgs e) {
            HyperLink lnkSection;
            SectionMenuLink _sectionMenuLink;
            SubSectionMenu _subSectionCtrl;
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
                lnkSection = (HyperLink)GetControl(e.Item, "lnkSection");
                _sectionMenuLink = (SectionMenuLink)e.Item.DataItem;
                _subSectionCtrl = (SubSectionMenu)GetControl(e.Item, "SubSectionMenu");

                lnkSection.Text = _sectionMenuLink.SectionMenuTitle;
                lnkSection.NavigateUrl = _sectionMenuLink.SectionPath;  
  
				if(_showSelection && _sectionMenuLink.SectionID == objSectionInfo.ID)
				{
					lnkSection.CssClass = this.CssClass + "_SelectedMenuItem";
				}

				if(SubSectionUtility.ShowSubSectionsForThisSection(_sectionMenuLink,_expandAll,objSectionInfo) && _expandLevels != _currentLevel)
				{
					_subSectionCtrl.ShowSelection = _showSelection;
					_subSectionCtrl.ExpandAll = _expandAll;
					_subSectionCtrl.CurrentLevel = _currentLevel + 1;
					_subSectionCtrl.ExpandLevels = _expandLevels;
					_subSectionCtrl.SectionId = _sectionMenuLink.SectionID;
				}
				else
				{
					_subSectionCtrl.Visible = false;
				}
            }
        }

        //*********************************************************************
        //
        // OnPreRender Method
        //
        // Binds the sections to the Repeater control.
        //
        //*********************************************************************
        override protected void OnPreRender(EventArgs e) {
            // Bind the results to the DataList
            rptSections.DataSource = GetTopLevelSections(objUserInfo);
            rptSections.DataBind();  
        }


        //*********************************************************************
        //
        // GetTopLevelSections Method
        //
        // Gets list of top-level sections. Displays different sections
        // depending on whether the current user is authenticated.
        //
        //*********************************************************************
        private ArrayList GetTopLevelSections(UserInfo user) {
            if (!user.IsAuthenticated)
                return GetPublicTopLevelSections();
            return GetTopLevelSectionsForUser(user);
        }
    

        //*********************************************************************
        //
        // GetPublicTopLevelSections Method
        //
        // Returns top-level sections from the cache, or if not available,
        // calculates top-level sections.
        //
        //*********************************************************************
        private ArrayList GetPublicTopLevelSections() {   
            ArrayList sections = (ArrayList)Context.Cache[CommunityGlobals.CacheKey("PublicTopLevelSections")];
            if (sections == null) {
                sections = CalculatePublicTopLevelSections();
                Context.Cache.Insert
                (
                    CommunityGlobals.CacheKey("PublicTopLevelSections"),
                    sections,
                    new CacheDependency( null, new string[] {CommunityGlobals.CacheKey("Sections")})
                );
            }
            return sections;
        }
        

        //*********************************************************************
        //
        // CalculatePublicTopLevelSections Method
        //
        // Determines public top-level sections by iterating through
        // the list of sections and finding sections that Everyone or
        // Authenticated can access.
        //
        //*********************************************************************  
        private ArrayList CalculatePublicTopLevelSections() {
            ArrayList publicTopLevelSections = new ArrayList();
            
            ArrayList topLevelSections = GetTopLevelSections();
            foreach (SectionInfo section in topLevelSections)
				if (Array.IndexOf(section.ViewRoles, "Community-Everyone") != -1 || Array.IndexOf(section.ViewRoles, "Community-Authenticated") != -1)
				{
					//SMR - Enh - use new overload constructor for SectionMenuLink
					publicTopLevelSections.Add(new SectionMenuLink(section.MenuTitle, section.Path, section.ID, section.ParentSectionID));   
				}
            return publicTopLevelSections;
        }
        


        //*********************************************************************
        //
        // GetTopLevelSections Method
        //
        // Retrieves all top-level sections.
        //
        //*********************************************************************
        private ArrayList GetTopLevelSections() {
            ArrayList topLevelSections = (ArrayList)Context.Cache[CommunityGlobals.CacheKey("TopLevelSections")];
            
            if (topLevelSections == null) {
                ArrayList sections = SectionUtility.GetAllEnabledSections().GetOrderedSections();
                topLevelSections = new ArrayList();
                foreach (SectionInfo section in sections)
                    if (section.ParentSectionID == -1 || section.ParentSectionID == SectionUtility.DefaultSectionID)
                        topLevelSections.Add(section);
            }
            return topLevelSections;
        }


        //*********************************************************************
        //
        // GetTopLevelSectionsForUser Method
        //
        // Determines top-level sections for an authenticated user.
        // This might include private sections.
        //
        //*********************************************************************
        private ArrayList GetTopLevelSectionsForUser(UserInfo user) {
            ArrayList colSections = new ArrayList();
            bool mayView = false;
            
            ArrayList topLevelSections = GetTopLevelSections();
            foreach (SectionInfo section in topLevelSections) {
                mayView = false;
                foreach (string role in section.ViewRoles) {
                    if (objUserInfo.IsInRole(role) || role=="Community-Everyone" || role=="Community-Authenticated")
                        mayView = true;
                }
                if (mayView)        
                    colSections.Add( new SectionMenuLink(section.MenuTitle, section.Path, section.ID, section.ParentSectionID));
            }
            if (objUserInfo.IsAdministrator)
                colSections.Add( new SectionMenuLink( "Admin", CommunityGlobals.ResolveBase( "Admin/Default.aspx" ) ));
            return colSections;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -