- Posted by matt on April 8, 2008
I've been using the excellent .NET blogging platform BlogEngine.NET for a while now and really enjoy coding against it even though code highliting doesn't seem to work. The extensibility of the code is awesome and allows you to morph the program into whatever you like. I was recently converting the program into a CMS system for a project that required a simple management tool and realized that the Page listing was lacking a bit in its display. Granted, this tool was not meant to be a CMS system but if your blog has many pages this might be useful to you as well. The system has built in Parent -> Child paging but displays all of the pages straight out in a list. I wanted to see the list with the Parent pages in bold and the child pages indented beneith its parent. To accomplish this I added 2 properties to the Page.cs object in the Blogengin.core, then I added a function to the Pages.cs code behind in /Admin/Pages/Pages.aspx.cs and modified the BindPageList() function to call it.
Note: I recompiled the program as a .NET 3.5 project to make use of Lambda expressions in the BindChildPageList() function. Change it to use delegates in the for loop if you're using .NET 2.0
First I added two properties to the Page object to make things easier later on: [BlogEngine.Core/Page.cs]
///
/// Does this post have a parent page?
///
public bool HasParentPage
{
get { return this.Parent != Guid.Empty; }
}
///
/// Does this post have child pages
///
public bool HasChildPages
{
get
{
foreach (Page p in Page._Pages)
{
if (p.Parent == this.Id)
return true;
}
return false;
}
}
Then I modified the BindPageList() function [BlogEngine.Web/Admin/Pages/Pages.aspx.cs]
private void BindPageList() {
foreach (Page page in BlogEngine.Core.Page.Pages) {
if (!page.HasParentPage)//MOD: Only add the page to the list if it's a root page (children are added below)
{//MOD
HtmlGenericControl li = new HtmlGenericControl("li");
HtmlAnchor a = new HtmlAnchor();
a.HRef = "?id=" + page.Id.ToString();
a.InnerHtml = page.Title;
System.Web.UI.LiteralControl text = new System.Web.UI.LiteralControl
(" (" + page.DateCreated.ToString("yyyy-dd-MM HH:mm") + ")");
li.Controls.Add(a);
li.Controls.Add(text);
if (page.HasChildPages)//MOD: If the page has children, add them recursively
{//MOD
//find children
li.Controls.Add(BuildChildPageList(page));//MOD
}//MOD
ulPages.Controls.Add(li);
}//MOD
}
divPages.Visible = true;
aPages.InnerHtml = BlogEngine.Core.Page.Pages.Count + " " + Resources.labels.pages;
}
And finally I added the BuildChildPageList() function [BlogEngine.Web/Admin/Pages/Pages.aspx.cs]
private HtmlGenericControl BuildChildPageList(BlogEngine.Core.Page page)
{
HtmlGenericControl ul = new HtmlGenericControl("ul");
foreach (Page cPage in BlogEngine.Core.Page.Pages.FindAll(p => (p.Parent == page.Id)))
{
HtmlGenericControl cLi = new HtmlGenericControl("li");
cLi.Attributes.CssStyle.Add("font-weight", "normal");
HtmlAnchor cA = new HtmlAnchor();
cA.HRef = "?id=" + cPage.Id.ToString();
cA.InnerHtml = cPage.Title;
System.Web.UI.LiteralControl cText = new System.Web.UI.LiteralControl
(" (" + cPage.DateCreated.ToString("yyyy-dd-MM HH:mm") + ")");
cLi.Controls.Add(cA);
cLi.Controls.Add(cText);
if (cPage.HasChildPages)
{
cLi.Attributes.CssStyle.Remove("font-weight");
cLi.Attributes.CssStyle.Add("font-weight", "bold");
cLi.Controls.Add(BuildChildPageList(cPage));
}
ul.Controls.Add(cLi);
}
return ul;
}
The result is the following:

