treenode.cs

来自「浏览器端看到树型目录结构,用户可以完整地看到像windows资源管理器一样的效果」· CS 代码 · 共 1,240 行 · 第 1/3 页

CS
1,240
字号
//------------------------------------------------------------------------------
// Copyright (c) 2000-2003 Microsoft Corporation. All Rights Reserved.
//------------------------------------------------------------------------------

namespace Microsoft.Web.UI.WebControls
{
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Data;
    using System.Drawing.Design;
    using System.Web;
    using System.Web.UI;
    using System.Xml;

    /// <summary>
    /// Indicates how a TreeNode should handle expanding and the plus sign.
    /// </summary>
    public enum ExpandableValue 
    {
        /// <summary>
        /// Always shows a plus sign and attempts to expand.
        /// </summary>
        Always,

        /// <summary>
        /// Shows a plus sign and allows expanding only when there are children.
        /// </summary>
        Auto,

        /// <summary>
        /// Allows expanding to be attempted once, such as in a databinding case, when
        /// the existence of children is unknown.
        /// </summary>
        CheckOnce
    };

    /// <summary>
    /// Class to eliminate runat="server" requirement and restrict content to approved tags
    /// </summary>
    internal class TreeNodeControlBuilder : FilterControlBuilder
    {
        protected override void FillTagTypeTable()
        {
            Add("treenode", typeof(TreeNode));
        }
    }


    /// <summary>
    /// TreeNode class: represents a tree node.
    /// Renders the necessary tags to display a treenode and handle its events.
    /// </summary>
    [
    ParseChildren(false),
    ControlBuilderAttribute(typeof(TreeNodeControlBuilder)),
    ToolboxItem(false),
    ]
    public class TreeNode : TreeBase, IParserAccessor
    {
        private int _level;
        private TreeView _treeview;     // parent
        private string _strInheritedType;
        private int _NodeTypeIndex;
        internal TreeNodeCollection _Nodes;
        private bool _bBound;

        /// <summary>
        /// Adds a parsed child object to the Nodes collection.
        /// Only accepts TreeNodes.
        /// </summary>
        /// <param name="obj">The object to add (must be a TreeNode).</param>
        void IParserAccessor.AddParsedSubObject(Object obj)
        {
            if (obj is TreeNode)
            {
                _Nodes.Add((TreeNode)obj);
            }
        }

        /// <summary>
        /// A node's inheritedType is the first of:
        /// * parent's childType
        /// * parent's type's childType
        /// * parent's inheritedType's childType
        /// * parent's inheritedType
        /// </summary>
        internal String InheritedType
        {
            get 
            {
                if ((_strInheritedType == null) || ((ParentTreeView != null) && ParentTreeView.AlwaysCalcValues))
                {
                    if (Parent is TreeNode)
                    {
                        TreeNode tnParent = (TreeNode)Parent;
                        // parent's ChildType
                        if (tnParent.ChildType != String.Empty)
                            _strInheritedType = tnParent.ChildType;

                        else 
                        {
                            // parent's type's or inheritedType's childType
                            TreeNodeType _parenttype = tnParent.NodeTypeObject;
                            if (_parenttype != null && _parenttype.ChildType != String.Empty)
                                _strInheritedType = _parenttype.ChildType;
                            else
                                _strInheritedType = tnParent.InheritedType;
                        }
                    }
                    else
                        _strInheritedType = ((TreeView)Parent).ChildType;
                }
                return _strInheritedType;
            }
        }

        /// <summary>
        /// The TreeNodeType that this TreeNode inherits properties from.
        /// </summary>
        private TreeNodeType NodeTypeObject
        {
            get
            {
                if (_NodeTypeIndex == -1)
                {
                    string strType;
                    if (Type != String.Empty)
                        strType = Type;
                    else if (InheritedType != String.Empty)
                        strType = InheritedType;
                    else return null;

                    // find the nodetype in the tree's nodetype collection
                    strType = strType.ToLower();
                    int i = 0;
                    int iCount = ParentTreeView.TreeNodeTypes.Count;
                    while (i < iCount && _NodeTypeIndex == -1)
                    {
                        if (((TreeNodeType)ParentTreeView.TreeNodeTypes[i]).Type.ToLower() == strType)
                            _NodeTypeIndex = i;
                        i++;
                    }
                    if (_NodeTypeIndex == -1)
                        return null;
                }
                return (TreeNodeType)ParentTreeView.TreeNodeTypes[_NodeTypeIndex];
            }
        }

        /// <summary>
        /// Controls how often the node determines whather or not it can be expanded
        /// </summary>
        [
        Category("Behavior"),
        DefaultValue(ExpandableValue.Auto),
        PersistenceMode(PersistenceMode.Attribute),
        ResDescription("TreeExpandable"),
        ]
        public ExpandableValue Expandable
        {
            get 
            {
                object b = ViewState["Expandable"];
                return ((b == null) ? (TreeNodeSrc == String.Empty ? ExpandableValue.Auto : ExpandableValue.CheckOnce) : (ExpandableValue)b);
            }
            set 
            {
                ViewState["Expandable"] = value;
            }
        }

        /// <summary>
        /// [TODO: to be supplied]
        /// </summary>
        private bool CheckedExpandable
        {
            get
            {
                object b = ViewState["CheckedExpandable"];
                return ((b == null) ? false : (bool)b);
            }
            set
            {
                ViewState["CheckedExpandable"] = value;
            }
        }

        /// <summary>
        /// Name of the TreeNodeType to apply to this node
        /// </summary>
        [
        Category("Data"),
        DefaultValue(""),
        PersistenceMode(PersistenceMode.Attribute),
        Editor(typeof(Microsoft.Web.UI.WebControls.Design.NodeTypeEditor), typeof(UITypeEditor)),
        ResDescription("TreeType"),
        ]
        public String Type
        {
            get 
            {
                object str = ViewState["Type"];
                return ((str == null) ? String.Empty : (String)str);
            }
            set
            {
                ViewState["Type"] = value;
            }
        }

        /// <summary>
        /// Text to display
        /// </summary>
        [
        Category("Appearance"),
        DefaultValue(""),
        PersistenceMode(PersistenceMode.Attribute),
        ResDescription("ItemText"),
        ]
        public String Text
        {
            get
            {
                object str = ViewState["Text"];
                return ((str == null) ? String.Empty : (String)str);
            }
            set
            {
                ViewState["Text"] = value;
            }
        }

        /// <summary>
        /// Url to navigate to when node is selected
        /// </summary>
        [
        Category("Behavior"),
        DefaultValue(""),
        PersistenceMode(PersistenceMode.Attribute),
        ResDescription("TreeNavigateUrl"),
        ]
        public String NavigateUrl
        {
            get
            {
                object str = ViewState["NavigateUrl"];
                return ((str == null) ? String.Empty : (String)str);
            }
            set
            {
                ViewState["NavigateUrl"] = value;
            }
        }

        /// <summary>
        /// Whether or not the node is expanded
        /// </summary>
        [
        Category("Appearance"),
        DefaultValue(false),
        PersistenceMode(PersistenceMode.Attribute),
        ResDescription("TreeExpanded"),
        ]
        public bool Expanded
        {
            get
            {
                object b = ViewState["Expanded"];
                if (b == null)
                {
                    if (ParentTreeView.ExpandLevel > Level)
                        return true;
                    else
                        return false;
                }
                else
                    return (bool)b;
            }
            set
            {
                ViewState["Expanded"] = value;
            }
        }

        /// <summary>
        /// Whether or not the node is currently visible (all parents expanded)
        /// </summary>
        internal bool IsVisible
        {
            get
            {
                TreeNode prev = this;
                while (prev.Parent is TreeNode)
                {
                    prev = (TreeNode)prev.Parent;
                    if (!prev.Expanded)
                        return false;
                }
                return true;
            }
        }

        /// <summary>
        /// Whether or not the node is checked
        /// </summary>
        [
        Category("Appearance"),
        DefaultValue(false),
        PersistenceMode(PersistenceMode.Attribute),
        ResDescription("TreeChecked"),
        ]
        public bool Checked
        {
            get
            {
                object b = ViewState["Checked"];
                if (b == null)
                    return false;
                else
                    return (bool)b;
            }
            set
            {
                ViewState["Checked"] = value;
            }
        }

        /// <summary>
        /// Custom data
        /// </summary>
        [
        Category("Data"),
        DefaultValue(""),
        PersistenceMode(PersistenceMode.Attribute),
        ResDescription("NodeData"),
        ]
        public string NodeData
        {
            get
            {
                object str = ViewState["NodeData"];
                if (str == null)
                    return String.Empty;
                else
                    return (string)str;
            }
            set
            {
                ViewState["NodeData"] = value;
            }
        }

        /// <summary>
        /// Whether or not the node is selected
        /// </summary>
        internal bool Selected
        {
            get
            {
                object b = ViewState["Selected"];
                return ((b == null) ? false : (bool)b);
            }
            set
            {
                ViewState["Selected"] = value;
            }
        }

        /// <summary>
        /// Url of the xml file to import as the content of this node
        /// </summary>
        [
        Category("Data"),
        DefaultValue(""),
        PersistenceMode(PersistenceMode.Attribute),
        ResDescription("TreeNodeSrc"),
        ]
        public string TreeNodeSrc
        {
            get
            {
                object str = ViewState["TreeNodeSrc"];
                return ((str == null) ? String.Empty : (string)str);
            }
            set
            {
                ViewState["TreeNodeSrc"] = value;
                _bBound = false;
                CheckedExpandable = false;
            }
        }

        /// <summary>
        /// Url of the xsl transform file to apply to the TreeNodeSrc
        /// </summary>
        [
        Category("Data"),
        DefaultValue(""),
        PersistenceMode(PersistenceMode.Attribute),
        ResDescription("TreeNodeXsltSrc"),
        ]
        public string TreeNodeXsltSrc
        {
            get
            {
                object str = ViewState["TreeNodeXsltSrc"];
                return ((str == null) ? String.Empty : (string)str);
            }
            set
            {
                ViewState["TreeNodeXsltSrc"] = value;
                _bBound = false;

⌨️ 快捷键说明

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