categorylist.cs
来自「个人博客系统」· CS 代码 · 共 138 行
CS
138 行
#region Using
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Collections.Generic;
using BlogEngine.Core;
#endregion
namespace Controls
{
/// <summary>
/// Builds a category list.
/// </summary>
public class CategoryList : Control
{
static CategoryList()
{
Post.Saved += delegate { _Html = null; };
Category.Saved += delegate { _Html = null; };
}
#region Properties
private bool _ShowRssIcon = true;
/// <summary>
/// Gets or sets whether or not to show feed icons next to the category links.
/// </summary>
public bool ShowRssIcon
{
get { return _ShowRssIcon; }
set { _ShowRssIcon = value; }
}
private static object _SyncRoot = new object();
private static string _Html;
private string Html
{
get
{
if (_Html == null)
{
lock (_SyncRoot)
{
if (_Html == null)
{
HtmlGenericControl ul = BindCategories();
System.IO.StringWriter sw = new System.IO.StringWriter();
ul.RenderControl(new HtmlTextWriter(sw));
_Html = sw.ToString();
}
}
}
return _Html;
}
}
#endregion
private HtmlGenericControl BindCategories()
{
HtmlGenericControl ul = new HtmlGenericControl("ul");
SortedDictionary<string, Guid> dic = SortGategories();
foreach (string key in dic.Keys)
{
HtmlGenericControl li = new HtmlGenericControl("li");
if (ShowRssIcon)
{
HtmlImage img = new HtmlImage();
img.Src = Utils.RelativeWebRoot + "pics/rssbutton.gif";
img.Alt = "RSS feed for " + key;
img.Attributes["class"] = "rssButton";
HtmlAnchor feedAnchor = new HtmlAnchor();
feedAnchor.HRef = Utils.RelativeWebRoot + "syndication.axd?category=" + dic[key].ToString();
feedAnchor.Attributes["rel"] = "nofollow";
feedAnchor.Controls.Add(img);
li.Controls.Add(feedAnchor);
}
HtmlAnchor anc = new HtmlAnchor();
anc.HRef = Utils.RelativeWebRoot + "category/" + Utils.RemoveIllegalCharacters(key) + BlogSettings.Instance.FileExtension;
anc.InnerHtml = key + " (" + Post.GetPostsByCategory(dic[key]).Count + ")";
anc.Title = "Category: " + key;
li.Controls.Add(anc);
ul.Controls.Add(li);
}
return ul;
}
private SortedDictionary<string, Guid> SortGategories()
{
SortedDictionary<string, Guid> dic = new SortedDictionary<string, Guid>();
foreach (Category cat in Category.Categories)
{
if (HasPosts(cat))
dic.Add(cat.Title, cat.Id);
}
return dic;
}
private bool HasPosts(Category cat)
{
foreach (Post post in Post.Posts)
{
if (post.IsVisible)
{
foreach (Category category in post.Categories)
{
if (category == cat)
return true;
}
}
}
return false;
}
/// <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 + -
显示快捷键?