Home >

Custom design for Parent-Child style of defining pages in blogengine.net

3. March 2011

When we create child pages in blogengine.net page title now showing in intended form i check it in PageList.cs control file located in App_Code\Controls and used inner controls from admin area.

Create a copy of your file (PageList.cs) as backup and copy below code in PageList.cs file.

#region Using

using System;
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 PageList : Control
  {

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

    #region Properties

    private static object _SyncRoot = new object();
    private static string _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 string Html
    {
      get
      {
        if (_Html == null)
        {
          lock (_SyncRoot)
          {
              if (_Html == null || BlogEngine.Core.Page.Pages == null)
            {
              HtmlGenericControl ul = BindPages();
              System.IO.StringWriter sw = new System.IO.StringWriter();
              ul.RenderControl(new HtmlTextWriter(sw));
              _Html = sw.ToString();
            }
          }
        }

        return _Html;
      }
    }

    #endregion

    /// <summary>
    /// Loops through all pages and builds the HTML
    /// presentation.
    /// </summary>
    private HtmlGenericControl BindPages()
    {
      HtmlGenericControl ul = new HtmlGenericControl("ul");
            ul.Attributes.Add("class", "pagelist");
            ul.ID = "pagelist";

      foreach (BlogEngine.Core.Page page in BlogEngine.Core.Page.Pages)
      {
        if (page.ShowInList && page.IsPublished && !page.HasParentPage)
        {
          //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);

          HtmlGenericControl li = new HtmlGenericControl("li");
          HtmlAnchor a = new HtmlAnchor();
          a.HRef = page.RelativeLink.ToString();
          a.InnerHtml = page.Title;

          li.Controls.Add(a);

          if (page.HasChildPages)
          {
              li.Controls.Add(BuildChildPageList(page));
          }

          li.Attributes.CssStyle.Remove("font-weight");
          li.Attributes.CssStyle.Add("font-weight", "bold");

          ul.Controls.Add(li);
        }
      }

      return ul;
    }

    private HtmlGenericControl BuildChildPageList(BlogEngine.Core.Page page)
    {
        HtmlGenericControl ul = new HtmlGenericControl("ul");
        foreach (BlogEngine.Core.Page cPage in BlogEngine.Core.Page.Pages.FindAll(delegate(BlogEngine.Core.Page p)
        {
            //p => (p.Parent == page.Id)))
            return p.Parent == page.Id;
        }))
        {
            HtmlGenericControl cLi = new HtmlGenericControl("li");
            cLi.Attributes.CssStyle.Add("font-weight", "normal");
            HtmlAnchor cA = new HtmlAnchor();
            cA.HRef = cPage.RelativeLink.ToString();
            cA.InnerHtml = "&nbsp;" + cPage.Title;

            cLi.Controls.Add(cA);

            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;
    }
    /// <summary>
    /// Renders the control.
    /// </summary>
    public override void RenderControl(HtmlTextWriter writer)
    {
      writer.Write(Html);
      writer.Write(Environment.NewLine);
    }
  }
}

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading