FilteredPageList

This simple modification of the PageList control allows you to filter the pages in the list based on the beginning of the page's description. This way, you can build sub-menus for your pages. For example to list only the pages having "Photo" at the beginning of their description property, you would use the following markup:

<blog:FilteredPageList ID="FilteredPageList1" runat="Server" Filter="Photo" />

Code for the control

#region Using

using System;
using System.Collections.Specialized;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.IO;
using BlogEngine.Core;

#endregion
 
namespace Controls
{
    /// <summary>
    /// Builds a page list.
    /// </summary>
    public class FilteredPageList : Control
    {

        /// <summary>
        /// Initializes the <see cref="PageList"/> class.
        /// </summary>
        static FilteredPageList()
        {
            BlogEngine.Core.Page.Saved += delegate { _Html = null; };
        }

        #region Properties
        
        private string _Filter;

        public string Filter
        {
            get { return _Filter; }
            set { _Filter = value; }
        }

        private static object _SyncRoot = new object();
        private static NameValueCollection _Html;
        /// <summary>
        /// Caches the rendered HTML in the private field and first
        /// updates it when a post has been saved (new or updated).
        /// </summary>
        private NameValueCollection Html
        {
            get
            {
                if (_Html == null ||
                    (!String.IsNullOrEmpty(Filter) && _Html[Filter] == null))
                {
                    lock (_SyncRoot)
                    {
                        if (_Html == null ||
                            (!String.IsNullOrEmpty(Filter) && _Html[Filter] == null))
                        {
                            if (_Html == null)
                                _Html = new NameValueCollection();
                            HtmlGenericControl ul = BindPages();
                            System.IO.StringWriter sw = new System.IO.StringWriter();
                            ul.RenderControl(new HtmlTextWriter(sw));
                            _Html.Add(Filter, sw.ToString());
                        }
                    }
                }

                return _Html;
            }
        }

        #endregion

        /// <summary>
        /// Loops through all pages and builds the HTML
        /// presentation.
        /// </summary>
        private HtmlGenericControl BindPages()
        {
            HtmlGenericControl ul = new HtmlGenericControl("ul");
            foreach (BlogEngine.Core.Page page in BlogEngine.Core.Page.Pages)
                if (String.IsNullOrEmpty(Filter) || page.Description.ToLower().StartsWith(_Filter.ToLower()))
                {
                    HtmlGenericControl li = new HtmlGenericControl("li");

                    HtmlAnchor anc = new HtmlAnchor();
                    anc.HRef = page.RelativeLink.ToString();
                    anc.InnerHtml = page.Title;
                    anc.Title = page.Description;

                    li.Controls.Add(anc);
                    ul.Controls.Add(li);
                }


            return ul;
        }

        /// <summary>
        /// Renders the control.
        /// </summary>
        public override void RenderControl(HtmlTextWriter writer)
        {
            writer.Write(Html[Filter]);
            writer.Write(Environment.NewLine);
        }
    }
}

Last edited Mar 7, 2009 at 10:57 PM by BenAmada, version 1

Comments

No comments yet.