Wednesday, August 20, 2008

How to get current node's depth from a Sitemap in ASP.NET2.0?

If you are using treecontrol or menucontrol in ASP.NET 2.0, you can achieve this easily using the built in property.

While i was working on a custom navigation tree control, i wanted to get the depth of the current node from the root. By default ASP.NET 2.0 does not have a property to get the depth. So i wrote a neat little recursion which will calculate the depth for you.



Private Function GetDepth(ByVal current As SiteMapNode) As Integer
  If(Not current.ParentNode Is Nothing) Then
    Return GetDepth(current.ParentNode) + 1
  Else
    Return 1
  End If
End Function

No comments:

Post a Comment