Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Depth Property

TreeNode.Depth Property

Gets the depth of the node.

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

public int Depth { get; }
/** @property */
public int get_Depth ()

public function get Depth () : int

Not applicable.

Property Value

The depth of the node.

Use the Depth property to determine the depth of the node. The depth represents the number of levels of hierarchy between a node and the root node. For example, a root node has a depth of zero. A child of the root node has a depth of one, and so on.

The following code example demonstrates how use the Depth property to determine the depth of a node. It initializes all nodes with a depth of one to a selected state. For this example to work correctly, you must copy the sample XML data below to a file named Newsgroup.xml.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">

  void Data_Bound(Object sender, TreeNodeEventArgs e)

    // Check the depth of a node as it is being bound to data.
    // Initialize the Checked property to true if the depth is 1.
    if(e.Node.Depth == 1)

      e.Node.Checked = true;


      e.Node.Checked = false;




<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeNode Checked Example</title>
    <form id="form1" runat="server">
      <h3>TreeNode Checked Example</h3>
      <asp:TreeView id="NewsgroupTreeView" 
          <asp:TreeNodeBinding DataMember="category" TextField="Name"/>
          <asp:TreeNodeBinding DataMember="group" TextField="Name"/>
      <asp:XmlDataSource id="NewsgroupXmlDataSource"  

The following code is sample XML data for the previous example.

<category name="news.microsoft.com">
    <group name="microsoft.public.dotnet.framework.aspnet"/>
    <group name="microsoft.public.dotnet.framework.aspnet.mobile"/>
    <group name="microsoft.public.dotnet.framework.aspnet.webservices"/>

Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

© 2015 Microsoft