treenode.cs

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

CS
1,240
字号
        {
            RenderDownLevelPath(output);
        }
            
        /// <summary>
        /// Render the downlevel version of the TreeNode.
        /// </summary>
        /// <param name="output">The HtmlTextWriter that will receive the markup.</param>
        protected override void RenderDownLevelPath(HtmlTextWriter output)
        {
            string href;
            String imageSrc = "";
            int i;
            CssCollection cssStyle;
            char cJunction = CalcJunction();
            object obj; // scratch object

            output.Write("<TR><TD valign='middle'>");
            output.Write("&nbsp;");

            //
            // Walk up tree to draw lines/whitespace
            //
            Object obWalk = Parent;
            if (ParentTreeView.ShowLines == true)
            {
                int iCount = 0;
                string strLines = String.Empty;
                while (obWalk is TreeNode)
                {
                    TreeNode elWalk = (TreeNode)obWalk;
                    TreeNodeCollection kids = GetNodeCollection(elWalk.Parent);
                    i = kids.IndexOf(elWalk) + 1;
                    if (i < kids.Count)
                    {
                        if (iCount > 0)
                        {
                            strLines = "<IMG align='top' border='0' width='" + iCount * ParentTreeView.Indent + "px' height='1px' SRC='" + ParentTreeView.SystemImagesPath + "white.gif'>" + strLines;
                            iCount = 0;
                        }
                        strLines = "<IMG align='top' border='0' SRC='" + ParentTreeView.SystemImagesPath + "I.gif'>" + strLines;
                    }
                    else
                        iCount++;
                    obWalk = elWalk.Parent;
                }
                if (iCount > 0)
                    strLines = "<IMG align='top' border='0' width='" + iCount * ParentTreeView.Indent + "px' height='1px' SRC='" + ParentTreeView.SystemImagesPath + "white.gif'>" + strLines;
                output.Write(strLines);
            }
            else
            {
                if (cJunction != 'R' && cJunction != 'F')
                {
                    int iIndent = 0;
                    while (obWalk is TreeNode && ((TreeNode)obWalk).Parent != null)
                    {
                        iIndent += ParentTreeView.Indent;
                        obWalk = ((TreeNode)obWalk).Parent;
                    }
                    output.Write("<IMG align='top' border='0' SRC='" + ParentTreeView.SystemImagesPath + "white.gif' WIDTH=" + iIndent + " HEIGHT=1>");
                }
            }

            // is this a branch node?
            bool bParent = false;
            obj = FindNodeAttribute("Expandable");
            if (obj == null)
            {
                obj = Expandable; // get default value if none is explicitly set
            }
            if (Nodes.Count > 0 || (ExpandableValue)obj == ExpandableValue.Always || ((ExpandableValue)obj == ExpandableValue.CheckOnce && !CheckedExpandable))
            {
                bParent = true;
            }

            if (ParentTreeView.ShowLines == true || ParentTreeView.ShowPlus == true)
            {
                if (ParentTreeView.ShowLines == true)
                {
                    switch (cJunction)
                    {
                        case 'L':
                            imageSrc = "L";
                            break;
                        case 'T':
                            imageSrc = "T";
                            break;
                        case 'R':
                            imageSrc = "R";
                            break;
                        case 'F':
                            imageSrc = "F";
                            break;
                    }
                }
                else
                    imageSrc = "";
                imageSrc = "'" + ParentTreeView.SystemImagesPath + imageSrc;

                if (bParent && ParentTreeView.ShowPlus == true)
                {
                    if (Expanded == true)
                    {
                        imageSrc += "minus.gif'>";
                    }
                    else
                    {
                        imageSrc += "plus.gif'>";
                    }
                    if (ParentTreeView.Page != null)
                        href = "javascript:" + ParentTreeView.Page.GetPostBackEventReference(ParentTreeView, (Expanded ? "oncollapse," : "onexpand,") + GetNodeIndex());
                    else
                        href = String.Empty;
                    output.AddAttribute(HtmlTextWriterAttribute.Href, href);
                    output.RenderBeginTag(HtmlTextWriterTag.A);
                }
                else if (ParentTreeView.ShowLines == true)
                    imageSrc += ".gif'>";
                else
                    imageSrc = "'" + ParentTreeView.SystemImagesPath + "white.gif'>";

                output.Write("<IMG align='top' border='0' class='icon' SRC=" + imageSrc);

                if (bParent && ParentTreeView.ShowPlus == true)
                    output.RenderEndTag();
            }

            // Render a checkbox
            obj = FindNodeAttribute("CheckBox");
            if (obj != null && (bool)obj == true)
            {
                string strOnclick = ParentTreeView.Page.GetPostBackEventReference(ParentTreeView, "oncheck," + GetNodeIndex());

                output.Write("<INPUT style='display:inline' align=middle type=checkbox " + (Checked ? " checked " : " ") + "onclick=\"" + strOnclick + "\" />");
            }

            // if this node is a navigating leaf, use its nav information.
            
            obj = FindNodeAttribute("NavigateUrl");
            if (obj != null && (!(ParentTreeView.SelectExpands && bParent && !Expanded)))
            {
                href = (string)obj;
                obj = FindNodeAttribute("Target");
                if (obj != null)
                {
                    output.AddAttribute(HtmlTextWriterAttribute.Target, (string)obj);
                    obj = null;
                }
            }
            else if (ParentTreeView.Page != null)
            {
                if (!Selected)
                {
                    href = "javascript:" + ParentTreeView.Page.GetPostBackEventReference(ParentTreeView, "onselectedindexchange," + ParentTreeView.SelectedNodeIndex + "," + GetNodeIndex());
                    if (bParent && ParentTreeView.SelectExpands == true)
                        href = href.Substring(0, href.Length - 2) + (";" + (Expanded ? "oncollapse," : "onexpand,") + GetNodeIndex()) + "')";
                }
                else
                    if (bParent && ParentTreeView.SelectExpands == true)
                    href = "javascript:" + ParentTreeView.Page.GetPostBackEventReference(ParentTreeView, (Expanded ? "oncollapse," : "onexpand,") + GetNodeIndex());
                else
                    href = String.Empty;
            }
            else
                href = String.Empty;

            if (href != String.Empty)
            {
                output.AddAttribute(HtmlTextWriterAttribute.Href, href);
                output.RenderBeginTag(HtmlTextWriterTag.A);
            }

            obj = null;
            if (Selected == true)
                obj = FindNodeAttribute("SelectedImageUrl");
            if (obj == null)
            {
                if (bParent == true && Expanded == true)
                    obj = FindNodeAttribute("ExpandedImageUrl");
                if (obj == null)
                    obj = FindNodeAttribute("ImageUrl");
            }
            if (obj != null)
                output.Write("<IMG align='top' border='0' class='icon' SRC='" + (string)obj + "'>");

            cssStyle = new CssCollection("display:inline; font-size:10pt; font-face:Times; color: black; text-decoration:none; cursor: hand; overflow:hidden;");
            object o = FindNodeAttribute("DefaultStyle");
            if (o != null)
            {
                cssStyle.Merge((CssCollection)o, true);
            }
            if (Selected)
            {
                CssCollection temp = new CssCollection("color: #00FFFF; background-color: #08246B;");
                o = FindNodeAttribute("SelectedStyle");
                if (o != null)
                {
                    temp.Merge((CssCollection)o,true);
                }
                cssStyle.Merge(temp, true);
            }

            /*
                       // Links will be blue by default.  We want them to be the color specified in our style.
                       // So we have to override the link color by adding a font tag inside the A tag.
                       string curColor = cssStyle.GetColor();
                       if (!Selected && (curColor != null) && (curColor != String.Empty))
                       {
                           output.AddStyleAttribute(HtmlTextWriterStyle.Color, curColor);
                       }
           */            
            cssStyle.AddAttributesToRender(output);
            cssStyle.RenderBeginFontTag(output);
            cssStyle.RenderBeginModalTags(output);

            if (Text != null && Text != String.Empty)
                output.Write("&nbsp;" + Text);

            cssStyle.RenderEndModalTags(output);
            cssStyle.RenderEndFontTag(output);

            if (href != String.Empty)
                output.RenderEndTag();

            output.Write("</TD></TR>\n");

            // Render child treenodes
            if (Expanded)
            {
                foreach (TreeNode item in Nodes)
                {
                    item.Render(output, RenderPathID.DownLevelPath);
                }
            }
        }

        /// <summary>
        /// Returns the value of att from the node's NodeType.
        /// </summary>
        /// <param name="att">The name of the attribute to lookup.</param>
        /// <returns>The value of the attribute or null.</returns>
        protected object GetNodeTypeAttribute(string att)
        {
            if (NodeTypeObject != null)
                return NodeTypeObject.GetStateVar(att);
            return null;
        }

        /// <summary>
        /// Return the attribute from the first place it's defined among:
        /// * the node itself
        /// * the node's type (directly declared or inherited via ChildType)
        /// * the treeview
        /// </summary>
        /// <param name="strAtt">The name of the attribute to lookup.</param>
        /// <returns>The value of the attribute.</returns>
        public object FindNodeAttribute(String strAtt)
        {
            object obj = GetStateVar(strAtt);
            if (obj != null && !(obj is CssCollection && ((CssCollection)obj).CssText == String.Empty))
                return obj;

            obj = GetNodeTypeAttribute(strAtt);
            if (obj != null && !(obj is CssCollection && ((CssCollection)obj).CssText == String.Empty))
                return obj;

            return ParentTreeView.GetStateVar(strAtt);
        }

        /// <summary>
        /// [TODO: to be supplied]
        /// </summary>
        /// <returns>[TODO: to be supplied]</returns>
        private bool CanExpand()
        {
            Object exp = FindNodeAttribute("Expandable");
            if (exp == null)
                exp = Expandable;
            if ((ExpandableValue)exp == ExpandableValue.Always || ((ExpandableValue)exp == ExpandableValue.CheckOnce && !CheckedExpandable))
                return true;
            if (Nodes.Count > 0)
                return true;
            return false;
        }

        /// <summary>
        /// [TODO: to be supplied]
        /// </summary>
        internal void ReadXmlSrc()
        {
            if (TreeNodeSrc != String.Empty)
            {
                TreeView tv = ParentTreeView.ReadXmlSrc(TreeNodeSrc, TreeNodeXsltSrc, "TREENODES");
                if (tv != null)
                {
                    TreeNodeCollection col = tv.Nodes;
                    ParentTreeView.CopyXmlNodesIntoTree(col, this);
                }
                _bBound = true;
            }
        }

        /// <summary>
        /// Gets the TreeNodeCollection from the given object.
        /// The object must be a TreeView or TreeNode.
        /// </summary>
        /// <param name="obj">The object on which to retrieve its Nodes collection.</param>
        /// <returns>The TreeNodeCollection.</returns>
        private TreeNodeCollection GetNodeCollection(Object obj)
        {
            if (obj is TreeView)
                return ((TreeView)obj).Nodes;
            else if (obj is TreeNode)
                return ((TreeNode)obj).Nodes;
            else
                throw new Exception(Util.GetStringResource("TreeInvalidObject"));
        }   

        /// <summary>
        /// Instructs the item to track changes to its view state.
        /// </summary>
        protected override void TrackViewState()
        {
            base.TrackViewState();

            ((IStateManager)Nodes).TrackViewState();
        }

        /// <summary>
        /// Restores the state of this object from a saved object in ViewState.
        /// </summary>
        /// <param name="savedState">The saved object.</param>
        protected override void LoadViewState(object savedState)
        {
            if (savedState != null)
            {
                object[] state = (object[])savedState;

                base.LoadViewState(state[0]);
                ((IStateManager)Nodes).LoadViewState(state[1]);
            }
        }

        /// <summary>
        /// Saves the state of the TreeNode into the ViewState.
        /// </summary>
        /// <returns>A ViewState object representing changes to this object.</returns>
        protected override object SaveViewState()
        {
            object[] state = new object[]
            {
                base.SaveViewState(),
                ((IStateManager)Nodes).SaveViewState(),
            };

            // Check to see if we're really saving anything
            foreach (object obj in state)
            {
                if (obj != null)
                {
                    return state;
                }
            }

            return null;
        }

        /// <summary>
        /// Remove the TreeNode and any children from its parent's Nodes collection.
        /// </summary>
        public void Remove()
        {
            TreeNodeCollection colParent = GetSiblingNodeCollection();
            if (colParent != null)
                colParent.Remove(this);
        }

        /// <summary>
        /// Retrieves the TreeNodeCollection this TreeNode is in.
        /// </summary>
        /// <returns>The TreeNodeCollection</returns>
        public TreeNodeCollection GetSiblingNodeCollection()
        {
            if (Parent is TreeView || Parent is TreeNode)
            {
                return GetNodeCollection(Parent);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// Forces the node to redatabind immediately, even if it isn't expanded.
        /// Child nodes are only bound if they're expanded.
        /// </summary>
        public void Databind()
        {
            if (TreeNodeSrc != String.Empty)
            {
                ReadXmlSrc();
                foreach (TreeNode node in Nodes)
                {
                    node.OnInit();
                }
            }        
        }
    }
}

⌨️ 快捷键说明

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