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

📄 sectionmenu.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;



    //*********************************************************************
    //
    // SectionMenu Class
    //
    // 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. 
    //
    //*********************************************************************
    
    public class SectionMenu : SkinnedCommunityControl {
    
        string _skinFileName = "Sections_SectionMenu.ascx";

        Repeater rptSections;
        
 
        //*********************************************************************
        //
        // SectionMenu Constructor
        //
        // Calls the base SkinnedCommunityControl constructor
        // and assigns the default page skin.
        //
        //*********************************************************************

        public SectionMenu() : 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;
            
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
                lnkSection = (HyperLink)GetControl(e.Item, "lnkSection");
                _sectionMenuLink = (SectionMenuLink)e.Item.DataItem;
                
                lnkSection.Text = _sectionMenuLink.SectionMenuTitle;
                lnkSection.NavigateUrl = _sectionMenuLink.SectionPath;    
            }
        }
        

        //*********************************************************************
        //
        // 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)
                    publicTopLevelSections.Add(new SectionMenuLink(section.MenuTitle, section.Path));        
            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) 
				{
					// KAB (StarPilot) - changed order of if statement.  Since it is ORed
					// once it hits a true statement it stops evaluating.  It is less steps
					// since 80% or better it is "Community-Everyone" or "Community-Authenticated"
					if (role=="Community-Everyone" || role=="Community-Authenticated" || objUserInfo.IsInRole(role)) 
					{
						mayView = true;
						break;	// KAB (StarPilot) - no reason to keep going if they can view...
					}
				}
                if (mayView)        
                    colSections.Add( new SectionMenuLink(section.MenuTitle, section.Path));
            }
            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 + -