On the standard TreeView of ASP.NET 2.0 it is not possible to give individual TreeNodes different styles. With the style attributes on the TreeView, one can only set the styling for a certain group of nodes, like the styling for all the leafnodes with the LeafNodeStyle attribute. However, it is possible to add styling on individual nodes with just a few lines of code. Just create a custom treenode class by inheriting from TreeNode, add some styling attributes and apply these attributes to the node by overriding the RenderPreText method.

You can see some code on how to do this on the ASP.NET forums and in the comments on a post of Danny Chen . Just check it out. Done ? Ok ! As you can see, while trying out this solution I made the mistake of writing ‘Gray’ as ‘Grey’ which lead me to some funny conclusions about why it wasn’t working. Guess it is better to think twice before posting or to have a spellchecker in Visual Studio. :)

Update : Kris van der Mast, probably by far Belgium’s most active poster on the ASP.NET forums, pointed me to a blogentry of Fredrik Normén, dated even before Beta2 was released, that already talked about this extensibility of TreeNodes.

Leave a Reply