This example illustrates TreeView how to populate a node on the client using ASP.NET 2.0 and C#.NET. First, you will need to import the using System.IO namespace.

We use the DirectoryInfo class for typical operations such as copying, moving, renaming, creating, and deleting directories. We then call the GetDirectories to returns the subdirectories of the current directory. Then we use the Treeview1_TreeNodePopulate event to do the work. Sometimes, it is not practical to statically predefine the tree structure due to data size or custom content that depends on user input. Because of this, the TreeView control supports dynamic node population. When a node’s PopulateOnDemand property is set to true, that node is populated at run time through a postback event when the node is expanded. To populate a node dynamically, an event-handling method that populates the node must be defined for the TreeNodePopulate event. Supported browsers can also take advantage of client-side node population. When enabled, this allows the TreeView control to dynamically populate a node on the client when that node is expanded, which prevents the need to post back to the server.

The front end Default.aspx page looks something like this:

The flow for the code behind page is as follows:

Download Source Files