Section Index

Topics: Controls
Feb 28, 2011 at 8:32 PM

This is for anybody who requires a self updating section index for subpages. I looked at the nuePageList extension, which is good, but only lists immediate page children. I needed something that would go deeper, so here's a modified pageMenu control that can reside in the User controls folder that when popped in a parent page will list all descendant pages (that are published) along with their description. Output is box standard HTML lists that can be styled with CSS.

I'm sure it could be improved, so if you have a mind to, please do and let us know.

Make the following two files and drop them in the "User controls" folder, and on your page where you want the list appearing it's standard control injector syntax [usercontrol: ~/User controls/SectionList.ascx]

The ascx file - SectionList.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="SectionList.ascx.cs" EnableViewState="false" Inherits="UserControls.SectionList" %>

 

The ascx.cs file - SectionList.ascx.cs

namespace UserControls
{
    using System;
    using System.Text.RegularExpressions;
    using System.Collections.Generic;
    using System.Web.UI.HtmlControls;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using BlogEngine.Core;   

    /// <summary>
    /// Summary description for SectionList
    /// </summary>
    ///


    public partial class SectionList : UserControl
    {
        public SectionList()
        {
            HtmlGenericControl ul = BindPages();
            if (ul != null)
            {
                System.IO.StringWriter sw = new System.IO.StringWriter();
                ul.RenderControl(new HtmlTextWriter(sw));
                _Html = sw.ToString();
            }
        }

        #region Properties

        private static object _SyncRoot = new object();
        private static string _Html = null;
        private bool _ulIdSet = false;
        private Guid _curPage;       

        #endregion

        private HtmlGenericControl BindPages()
        {
            string pageId = HttpContext.Current.Request.QueryString["id"];
            if (String.IsNullOrEmpty(pageId))
            {
                //In the unlikely event there is no ID then just abandon ship
                return null;
            }
            // recursivly get all children of the current page
            _curPage = new Guid(pageId);
            HtmlGenericControl ul = GetChildren(_curPage);
            return ul;
        }

        bool HasChildren(Guid pageId)
        {
            bool returnValue = false;

            foreach (BlogEngine.Core.Page page in BlogEngine.Core.Page.Pages)
            {
                if (page.IsPublished)
                {
                    if (page.Parent == pageId)
                    {
                        returnValue = true;
                        break;
                    }
                }
            }

            return returnValue;
        }

        HtmlGenericControl GetChildren(Guid parentId)
        {
            HtmlGenericControl ul = new HtmlGenericControl("ul");

            if (!_ulIdSet)
            {
                ul.Attributes.Add("id", "sectionList");
                _ulIdSet = true;
            }

            foreach (BlogEngine.Core.Page page in BlogEngine.Core.Page.Pages)
            {
                if (page.IsPublished)
                {
                    if (page.Parent == parentId)
                    {
                        HtmlGenericControl li = new HtmlGenericControl("li");
                        HtmlAnchor anc = new HtmlAnchor();
                        anc.HRef = page.RelativeLink.ToString();
                        anc.InnerHtml = page.Title;
                        HtmlGenericControl spn = new HtmlGenericControl("span");
                        spn.InnerText = " - " + page.Description;
                        li.Controls.Add(anc);
                        li.Controls.Add(spn);

                        if (HasChildren(page.Id))
                        {
                            HtmlGenericControl subUl = GetChildren(page.Id);
                            li.Controls.Add(subUl);
                        }
                        ul.Controls.Add(li);
                    }
                }
            }

            return ul;
        }

        public override void RenderControl(HtmlTextWriter writer)
        {
            writer.Write(_Html);
            writer.Write(Environment.NewLine);
        }
    }
}