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);
text5 = this._owner.GetImageUrl(0);
text5 = this._owner.GetImageUrl(1);
case (TreeNodeTypes.Parent | TreeNodeTypes.Root):
text5 = this._owner.GetImageUrl(2);
Notice the bold items:
private TreeNodeTypes GetTreeNodeType()
TreeNodeTypes types1 = TreeNodeTypes.Leaf;
if ((this.Depth == 0) && (this.ChildNodes.Count > 0))
if ((this.ChildNodes.Count <= 0) && !this.PopulateOnDemand)
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.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.