⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 breadcrumbs.cs

📁 基于.Net环境下c#实现的国人自己的Blog平台,基于.Text的核心技术,但做了汉化以及改写了一部分的核心代码,值得研究学习
💻 CS
字号:
#region Disclaimer/Info

///////////////////////////////////////////////////////////////////////////////////////////////////
// .Text WebLog
// 
// .Text is an open source weblog system started by Scott Watermasysk. 
// Blog: http://ScottWater.com/blog 
// RSS: http://scottwater.com/blog/rss.aspx
// Email: Dottext@ScottWater.com
//
// For updated news and information please visit http://scottwater.com/dottext and subscribe to 
// the Rss feed @ http://scottwater.com/dottext/rss.aspx
//
// On its release (on or about August 1, 2003) this application is licensed under the BSD. However, I reserve the 
// right to change or modify this at any time. The most recent and up to date license can always be fount at:
// http://ScottWater.com/License.txt
// 
// Please direct all code related questions to:
// GotDotNet Workspace: http://www.gotdotnet.com/Community/Workspaces/workspace.aspx?id=e99fccb3-1a8c-42b5-90ee-348f6b77c407
// Yahoo Group http://groups.yahoo.com/group/DotText/
// 
///////////////////////////////////////////////////////////////////////////////////////////////////

#endregion

using System;
using System.Collections.Specialized;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dottext.Framework.Configuration;
// TODO: Expose Ancestors for editing locally? N2H
namespace Dottext.Web.Admin.WebUI
{
	[ToolboxData("<{0}:BreadCrumbs runat=\"server\" />")]
	public class BreadCrumbs : Control
	{
		protected const string PREFIXTEXT_DEFAULT = "位置:";
		protected const string SEPARATOR_DEFAULT = "&nbsp;&#187;&nbsp;";
		protected const string SPACER_DEFAULT = "&nbsp;";
		protected Control container;
		protected PageLocation _lastItem;
		protected string _lastItemText;
		protected bool _lastItemOverride = false;

		protected string _pageID = String.Empty;
		protected bool _isPanel = true;
		protected bool _includeRoot = true;
		protected bool _includeSelf = true;
		protected bool _lastItemStatic;

		protected bool _usePrefixText = false;
		protected string _prefixText = PREFIXTEXT_DEFAULT;
		protected bool _useSpacers = true;
		protected string _spacer = SPACER_DEFAULT;
		protected string _separator = SEPARATOR_DEFAULT;

		protected string _cssClass = String.Empty;
		protected string _linkCssClass = String.Empty;
		protected string _lastCssClass = String.Empty;
		protected NameValueCollection _linkStyle = new NameValueCollection();
		protected NameValueCollection _lastStyle = new NameValueCollection();

		public BreadCrumbs()
		{
			SetContainer();
		}

		#region Accessors

		public string PageID
		{
			get
			{
				if (_pageID.Length > 0)
					return _pageID;
				else
					return this.Page.GetType().BaseType.FullName;
			}
			set { _pageID = value; }
		}

		public bool IsPanel
		{
			get { return _isPanel; }
			set
			{
				if (_isPanel != value)
				{
					_isPanel = value;
					SetContainer();
				}
			}
		}

		public bool IncludeRoot
		{
			get { return _includeRoot; }
			set { _includeRoot = value; }
		}

		public bool IncludeSelf
		{
			get { return _includeSelf; }
			set { _includeSelf = value; }
		}

		public bool UsePrefixText
		{
			get { return _usePrefixText; }
			set { _usePrefixText = value; }
		}

		public string PrefixText
		{
			get { return _prefixText; }
			set { _prefixText = value; }
		}

		public bool UseSpacers
		{
			get { return _useSpacers; }
			set { _useSpacers = value; }
		}

		public string Spacer
		{
			get { return _spacer; }
			set { _spacer = value; }
		}

		public string Separator
		{
			get { return _separator; }
			set { _separator = value; }
		}

		public bool LastItemStatic
		{
			get { return _lastItemStatic; }
			set { _lastItemStatic = value; }
		}

		public string CssClass
		{
			get { return _cssClass; }
			set { _cssClass = value; }
		}

		public string LinkCssClass
		{
			get { return _linkCssClass; }
			set { _linkCssClass = value; }
		}

		public string LastCssClass
		{
			get { return _lastCssClass; }
			set { _lastCssClass = value; }
		}

		public NameValueCollection LinkStyle
		{
			get { return _linkStyle; }
		}

		public NameValueCollection LastStyle
		{
			get { return _lastStyle; }
		}

		#endregion

		protected void SetContainer()
		{
			if (_isPanel)
			{
				container = new Panel();
				this.Controls.Clear();
				this.Controls.Add(container);
			}
			else
			{
				this.Controls.Clear();
				container = this;
			}
		}

		public void AddLastItem(string title)
		{
			_lastItemStatic = true;
			_lastItemOverride = true;
			_lastItemText = title;
		}

		public void AddLastItem(string title, string url)
		{
			AddLastItem(title, url, String.Empty);
		}

		public void AddLastItem(string title, string url, string description)
		{
			_lastItem = new PageLocation(String.Empty, title, url, description);
			_lastItemOverride = true;
		}

		private string _fullyQualifiedBaseUrl;

		public string FullyQualifiedBaseUrl
		{
			get
			{
				if (this._fullyQualifiedBaseUrl == null)
					this._fullyQualifiedBaseUrl = Config.CurrentBlog().FullyQualifiedUrl + "admin/";
				return this._fullyQualifiedBaseUrl;
			}
			set { this._fullyQualifiedBaseUrl = value; }
		}

		protected HyperLink CreateHyperLinkFromLocation(PageLocation value, bool isLastItem)
		{
			HyperLink result = new HyperLink();
			result.Text = value.Title;
			result.NavigateUrl = FullyQualifiedBaseUrl + value.Url;
			result.CssClass = isLastItem ? _lastCssClass : _linkCssClass;
			result = (HyperLink) ApplyStyles(result, isLastItem);
			result.Target = value.Target;

			return result;
		}

		protected WebControl ApplyStyles(WebControl control, bool isLastItem)
		{
			if (isLastItem && _lastStyle.Count > 0)
				return Utilities.CopyStyles(control, _lastStyle);
			else if (_linkStyle.Count > 0)
				return Utilities.CopyStyles(control, _linkStyle);
			else
				return control;
		}

		protected PageLocation[] GetSampleAncestors()
		{
			PageLocation[] results = new PageLocation[3];
			results[0] = new PageLocation("", "Level 3", "#");
			results[1] = new PageLocation("", "Level 2", "#");
			results[2] = new PageLocation("", "Level 1", "#");
			return results;
		}

		protected override void Render(HtmlTextWriter writer)
		{
			container.Controls.Clear();

			PageLocation[] lineage;
			if (null != HttpContext.Current)
				lineage = SiteMap.Instance.GetAncestors(this.PageID, _includeSelf);
			else
				lineage = GetSampleAncestors();

			if (null != lineage && lineage.Length > 0)
			{
				if (_usePrefixText && _prefixText.Length > 0)
				{
					container.Controls.Add(new LiteralControl(_prefixText));
					if (_useSpacers)
						container.Controls.Add(new LiteralControl(_spacer));
				}

				int startAt = _includeRoot ? lineage.Length - 1 : lineage.Length - 2;
				for (int i = startAt; i >= 0; i--)
				{
					if (i > 0 || _lastItemOverride || !_lastItemStatic)
						container.Controls.Add(CreateHyperLinkFromLocation(lineage[i], i > 0 || _lastItemOverride));
					else //if (!_lastItemOverride && _lastItemStatic) (redundant)
						container.Controls.Add(new LiteralControl(lineage[i].Title));

					if (_useSpacers && (i > 0 || _lastItemOverride))
						container.Controls.Add(new LiteralControl(_separator));
				}

				if (_lastItemOverride)
				{
					if (null != _lastItem)
					{
						if (!_lastItemStatic)
							container.Controls.Add(CreateHyperLinkFromLocation(_lastItem, true));
						else
							container.Controls.Add(new LiteralControl(_lastItem.Title));
					}
					else if (_lastItemText.Length > 0)
						container.Controls.Add(new LiteralControl(_lastItemText));
				}
			}

			if (_isPanel && _cssClass.Length > 0)
				(container as Panel).CssClass = _cssClass;

			base.Render(writer);
		}
	}
}

⌨️ 快捷键说明

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