May 5 2006

PopulateOnDemand & Node Icon in TreeView - ASP.NET 2.0

Category: ASP.NET 2.0 - GeneralBil@l @ 08:02

I discovered something interesting today in the world of TreeViews!

I have been working on TreeViews and dynamic populating for those TreeViews. One thing took my attention which is that:

When I first load the root node in a dynamically populated TreeView, having:

  • PopulateOnDemand true for the root node
  • ShowExpandCollapse true
  • ExpandDepth = "0"
  • Having XPFileExplorer ImageSet for instance (can be any other ImageSet)

The icon near the only node displaying now is a folder icon, in a time I should have a PC icon representing the root node.

But looking into Reflector, an advise given to me by my colleague Teemu Keiski, you will notice the following in the TreeNode - Render method:

      TreeNodeTypes types1 = this.GetTreeNodeType();
      string text5 = string.Empty;
      if (this.ImageUrl.Length > 0)
            text5 = this._owner.ResolveClientUrl(this.ImageUrl);
      else if (((num1 < this._owner.LevelStyles.Count) && (this._owner.LevelStyles[num1] != null)) && (style1.ImageUrl.Length > 0))
            text5 = this._owner.GetLevelImageUrl(num1);
            switch (types1)
                  case TreeNodeTypes.Root:
                        text5 = this._owner.GetImageUrl(0);
                        goto Label_06FB;

                  case TreeNodeTypes.Parent:
                        text5 = this._owner.GetImageUrl(1);
                        goto Label_06FB;

                  case (TreeNodeTypes.Parent | TreeNodeTypes.Root):
                        goto Label_06FB;

                  case TreeNodeTypes.Leaf:
                        text5 = this._owner.GetImageUrl(2);
                        goto Label_06FB;

Notice the bold items:

private TreeNodeTypes GetTreeNodeType()
      TreeNodeTypes types1 = TreeNodeTypes.Leaf;
      if ((this.Depth == 0) && (this.ChildNodes.Count > 0))
            return TreeNodeTypes.Root;
      if ((this.ChildNodes.Count <= 0) && !this.PopulateOnDemand)
            return types1;
      return TreeNodeTypes.Parent;

As you can see, the first if doesn't match, although the only root is at depth 0, but the children count now is 0.

The second if is false, we do have children count 0 but we have PopulateOnDemand true

Finally, the node type returned is Parent.

Going back to the Render method bold items, we can see that if the type is Parent we are rendering the Parent icon which is in our case (XPFileExplorer) a yellow folder.

This explains why the PC icon was not displayed initially. However, once you populate the children, it will be rendered again (the root node) as a PC icon.

If you feel bothered from having a folder icon at the beginng you can do the following in the Page_Load/Page_Init for instance:

this.TreeView1.Nodes[0].ImageUrl = this.Page.ClientScript.GetWebResourceUrl(typeof(TreeView), "TreeView_XP_Explorer_RootNode.gif");

Assuming you have placed a single node in the TreeView!

Hope this tip helps you all.




Comments are closed