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" />
EditCode 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
{
///
/// Builds a page list.
///
public class FilteredPageList : Control
{
///
/// Initializes the class.
///
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;
///
/// Caches the rendered HTML in the private field and first
/// updates it when a post has been saved (new or updated).
///
private NameValueCollection Html
{
get
{
if (_Html == null ||
(!String.IsNullOrEmpty(Filter) && _HtmlFilter == null))
{
lock (_SyncRoot)
{
if (_Html == null ||
(!String.IsNullOrEmpty(Filter) && _HtmlFilter == 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
///
/// Loops through all pages and builds the HTML
/// presentation.
///
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;
}
///
/// Renders the control.
///
public override void RenderControl(HtmlTextWriter writer)
{
writer.Write(HtmlFilter);
writer.Write(Environment.NewLine);
}
}
}