TreeView - Dynamic Node and Leaf styles possible?

LetsGoPens

New member
Joined
Apr 18, 2008
Messages
4
Programming Experience
1-3
I have a TreeView that is being populated from a query. The last(leaf) and second to last (node) levels of tree can be URL links depending on the result of a separate query.

I would like the nodes and leaves that have URL links to appear differently than the ones that do not have a link. I tried to manipute the TreeView.LeafNodeStyle for each Leaf, but it is setting it for the entire class. Specifically I want to change the color and underlining for links vs. nonlinks.
VB.NET:
Expand Collapse Copy
If dt2.Rows.Count < 1 Then
    tn.PopulateOnDemand = False
    If dt3.Rows.Count < 1 Then
        tn.SelectAction = TreeNodeSelectAction.None
        tn.ToolTip = "No data available for this leaf"
        'TreeView1.LeafNodeStyle.Font.Underline = False
    Else
        tn.SelectAction = TreeNodeSelectAction.Select
        tn.NavigateUrl = "some URL"
    End If
Else
    tn.PopulateOnDemand = True
    If dt3.Rows.Count < 1 Then
        tn.SelectAction = TreeNodeSelectAction.Expand
        tn.ToolTip = "No data available for this Node"
        'TreeView1.LeafNodeStyle.Font.Underline = False
    Else
        tn.SelectAction = TreeNodeSelectAction.SelectExpand
        tn.NavigateUrl = "some URL "
        tn.ToolTip = "Click to view the Report for this Node"
    End If
End If

Any thoughts on this?

Thanks,
 
Last edited by a moderator:
You can change the Font and ForeColor of each TreeNode.
 
John,

I should have mentioned that this TreeView is being used on an aspx page. It is a member of System.Web.UI.WebControls. ForeColor and Font are not members of this TreeNode class.
 
Thread moved to ASP.Net section. This can't easily be done in ASP.Net, from what I gather you have to inherit TreeNode and customize the rendered output, for example:
VB.NET:
Expand Collapse Copy
Public Class LinkTreeNode
    Inherits TreeNode
    Protected Overrides Sub RenderPreText(ByVal writer As System.Web.UI.HtmlTextWriter)
        writer.AddStyleAttribute("text-decoration", "underline")
        MyBase.RenderPreText(writer)
    End Sub
End Class
VB.NET:
Expand Collapse Copy
Dim tn As New LinkTreeNode
tn.Text = "hello"
Me.TreeView1.Nodes(0).ChildNodes.Add(tn)
This article might give you some more pointers: Customizing TreeNodes with RenderPreText and RenderPostText
 
Back
Top