pagelist.cs

来自「个人博客系统」· CS 代码 · 共 97 行

CS
97
字号
#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");
      foreach (BlogEngine.Core.Page page in BlogEngine.Core.Page.Pages)
      {
        if (page.ShowInList && page.IsPublished)
        {
          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);
      writer.Write(Environment.NewLine);
    }
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?