treeelement.cs

来自「《精通ASP.NET2.0网络应用系统开发》书中的源码」· CS 代码 · 共 725 行 · 第 1/2 页

CS
725
字号
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
//using System.Diagnostics;

namespace iiuga.Web.UI
{
	// TODO: Implement the TreeElement as an template control, to be able to include other controls in its body.
	// TODO: Add "Attributes" collection to the node. (these attributes can be displayed on the same line with the node)

	/// <summary>
	/// Element types. 
	/// The elements can be added at design time or dinamically at run time.
	/// 
	/// Copyright ?Iulian Iuga, 2003. All Rights Reserved.
	/// </summary>
	/// <author>
	/// Created by Iulian Iuga; 19 January, 2003
	/// </author>
	internal enum _ElementType : byte
	{
		_designTimeElement,
		_runTimeElement
	}

	/// <summary>
	/// Represents a tree element. It is displayed as part of TreeWeb control.
	/// 
	/// Copyright ?Iulian Iuga, 2003. All Rights Reserved.
	/// </summary>
	/// <author>
	/// Created by Iulian Iuga; 27 December, 2002
	/// </author>
	public class TreeElement : IStateManager
	{
		internal static readonly char	_separator = ':';
		internal static readonly string	_checkboxIDSufix = "checkbox";
		static readonly string			_idPrefix = "element";

		string					_id;
		int						_level;
		ElementsCollection		_elements;
		TreeElement				_parent;
		TreeWeb					_treeWeb;
		StateBag				_state;
		_ElementType			_elementType;
		bool					_marked = false;

		#region TreeElement public properties

		/// <summary>
		/// Gets the ID of the TreeElement.
		/// </summary>
		/// <author>
		/// Created by Iulian Iuga; 27 December, 2002
		/// </author>
		public string ID 
		{ 
			get { 
				if( _id != null && _id.Length > 0)
					return _id;

				_id = Parent.ID + _separator + _idPrefix + Parent.Elements.IndexOf( this );
				return _id; 
			} 
		}

		/// <summary>
		/// Gets or sets the text to be displayed for the TreeElement.
		/// </summary>
		/// <author>
		/// Created by Iulian Iuga; 28 December, 2002
		/// </author>
		[
		Bindable( true ),
		DefaultValue( "" ),
		PersistenceMode( PersistenceMode.Attribute )
		]
		public string Text 
		{ 
			get {
				string _text = (string) ViewState["Text"];
				return( ( _text == null ) ? String.Empty : _text );
			}
			set {
				ViewState["Text"] = value;
			}
		}

		/// <summary>
		/// Gets the ElementsCollection for the TreeElement.
		/// </summary>
		/// <author>
		/// Created by Iulian Iuga; 27 December, 2002
		/// </author>
		[
		Browsable( true ),
		PersistenceMode( PersistenceMode.InnerProperty )
		]
		public ElementsCollection Elements
		{
			get {
				if( _elements == null )
					_elements = new ElementsCollection( this );

				return _elements;
			}
		}

		/// <summary>
		/// Gets the TreeElement parent object for the current element.
		/// </summary>
		/// <author>
		/// Created by Iulian Iuga; 30 December, 2002
		/// </author>
		[
		Browsable( false )
		]
		public TreeElement Parent
		{
			get {
				return _parent;
			}
		}

		/// <summary>
		/// Gets the TreeWeb object.
		/// </summary>
		/// <author>
		/// Created by Iulian Iuga; 30 December, 2002
		/// </author>
		[
		Browsable( false )
		]
		public TreeWeb TreeWeb
		{
			get {
				return _treeWeb;
			}
		}

		/// <summary>
		/// Gets whether the element is expanded or not.
		/// </summary>
		/// <author>
		/// Created by Iulian Iuga; 30 December, 2002
		/// </author>
		[
		DefaultValue( false ),
		Browsable( false )
		]
		public bool IsExpanded 
		{
			get {
				object _expanded = ViewState["IsExpanded"];
				return ( ( _expanded == null ) ? false : (bool)_expanded );
			}
		}

		/// <summary>
		/// Gets whether the element has or not children elements.
		/// </summary>
		/// <author>
		/// Created by Iulian Iuga; 30 December, 2002
		/// </author>
		[
		DefaultValue( false ),
		Browsable( false )
		]
		public bool HasElements
		{
			get {
				return ( _elements != null && _elements.Count > 0 );
			}
		}

		/// <summary>
		/// Gets the level where the element is in the tree.
		/// </summary>
		/// <author>
		/// Created by Iulian Iuga; 04 January, 2003
		/// </author>
		[
		Browsable( false )
		]
		public int Level
		{
			get {
				return _level;
			}
		}

		/// <summary>
		/// Gets whether the element is checked or not.
		/// </summary>
		/// <author>
		/// Created by Iulian Iuga; 05 January, 2003
		/// </author>
		[
		DefaultValue( false ),
		Browsable( false )
		]
		public bool IsChecked 
		{
			get {
				object _checked = ViewState["IsChecked"];
				return ( ( _checked == null ) ? false : (bool)_checked );
			}
		}

		/// <summary>
		/// Gets or sets specific data for the tree element.
		/// </summary>
		/// <author>
		/// Created by Iulian Iuga; 18 January, 2003
		/// </author>
		[
		Bindable( true ),
		Browsable( true ),
		DefaultValue( "" ),
		PersistenceMode( PersistenceMode.Attribute )
		]
		public string Key 
		{ 
			get {
				string _key = (string) ViewState["Key"];
				return( ( _key == null ) ? String.Empty : _key );
			}
			set {
				ViewState["Key"] = value;
			}
		}

		/// <summary>
		/// Gets or sets the action for the tree element.
		/// </summary>
		/// <author>
		/// Created by Iulian Iuga; 18 January, 2003
		/// </author>
		[
		Bindable( true ),
		DefaultValue( "" ),
		PersistenceMode( PersistenceMode.Attribute )
		]
		public string NavigateUrl
		{
			get {
				string _navigateUrl = (string) ViewState["NavigateUrl"];
				return( ( _navigateUrl == null ) ? String.Empty : _navigateUrl );
			}
			set {
				ViewState["NavigateUrl"] = value;
			}
		}

		/// <summary>
		/// Gets or sets the target of the tree element action.
		/// </summary>
		/// <author>
		/// Created by Iulian Iuga; 25 January, 2003
		/// </author>
		[
		Bindable( true ),
		DefaultValue( "" ),
		PersistenceMode( PersistenceMode.Attribute )
		]
		public string Target
		{
			get {
				string _target = (string) ViewState["Target"];
				return( ( _target == null ) ? String.Empty : _target );
			}
			set {
				ViewState["Target"] = value;
			}
		}

		/// <summary>
		/// Gets or sets whether a check box is displayed next to the current element.
		/// </summary>
		/// <author>
		/// Created by Iulian Iuga; 20 January, 2003
		/// </author>
		[
		Bindable( true ),
		DefaultValue( false ),
		PersistenceMode( PersistenceMode.Attribute )
		]
		public bool CheckBox
		{
			get {
				object _checkBox = ViewState["CheckBox"];
				return ( ( _checkBox == null ) ? false : (bool)_checkBox );
			}
			set {
				ViewState["CheckBox"] = value;
			}
		}

		/// <summary>
		/// Gets or sets the ToolTip display text for the current element.
		/// </summary>
		/// <author>
		/// Created by Iulian Iuga; 26 January, 2003
		/// </author>
		[
		Bindable( true ),
		DefaultValue( "" ),
		PersistenceMode( PersistenceMode.Attribute )
		]
		public string ToolTip 
		{
			get {
				string _toolTip = (string) ViewState["ToolTip"];
				return( ( _toolTip == null ) ? String.Empty : _toolTip );
			}
			set {
				ViewState["ToolTip"] = value;
			}
		}

		/// <summary>
		/// Gets or sets whether the current element is enabled or not.
		/// </summary>
		/// <author>
		/// Created by Iulian Iuga; 26 January, 2003
		/// </author>
		[
		Bindable( true ),
		DefaultValue( true ),
		PersistenceMode( PersistenceMode.Attribute )
		]
		public bool Enabled
		{
			get {
				object _enabled = ViewState["Enabled"];
				return ( ( _enabled == null ) ? true : (bool)_enabled );
			}
			set {
				ViewState["Enabled"] = value;
			}
		}

		/// <summary>
		/// Gets or sets the default CssClass for an element.
		/// </summary>
		/// <author>
		/// Created by Iulian Iuga; 26 January, 2003
		/// </author>
		[
		Bindable( true ),
		DefaultValue( "" ),
		PersistenceMode( PersistenceMode.Attribute )
		]
		public string CssClass
		{
			get {
				string _cssClass = (string) ViewState["CssClass"];
				return ( ( _cssClass == null ) ? String.Empty : _cssClass );
			}
			set {
				ViewState["CssClass"] = value;

⌨️ 快捷键说明

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