O'Reilly logo

ASP.NET 2.0: A Developer's Notebook by Wei-Meng Lee

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Populate a TreeView Control Programmatically

Note

Instead of statically binding the TreeView control to a site map or XML file, gain flexibility by creating it dynamically during runtime.

In other labs in this chapter, you have seen how the TreeView control can be bound to site map files as well as XML documents at design time. However, you can also build your TreeView control dynamically, one node at a time. For example, you might want to display a particular disk directory using the TreeView control, or you may have a site that is changing frequently and hence it is much more efficient to build the tree dynamically.

In this lab, you will use the TreeView control to display the current application path and its subdirectories.

How do I do that?

In this lab, you will learn how to customize the TreeView control and dynamically populate it with items. Specifically, you will display the current application path and all its subdirectories in the TreeView control.

  1. Launch Visual Studio 2005 and create a new ASP.NET 2.0 web application. Name the project C:\ASPNET20\chap02-DynamicTreeView.

  2. Drag and drop a TreeView control onto the default Web Form.

  3. In Solution Explorer, right-click the project name and select Add New Folder Regular Folder. Name the folder Images, since you'll use this folder to store your image files.

  4. Add the images shown in Figure 2-36 to the C:\ASPNET20\chap02-DynamicTreeView\Images folder.

    Tip

    The images can be downloaded from this book's support site at http://www.oreilly.com/catalog/aspnetadn/ ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required