<%@ Import namespace="System.IO" %>
<html>
<head>
<title>Populating TreeView Nodes On Demand</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<script language="C#" runat="server">
void populateNode ( Object source, TreeNodeEventArgs e ) {
TreeNode node = e.Node;
string fullPath = Request.MapPath ( node.Value, Request.ApplicationPath, false );
// enumerate directories
string [ ] dirs = Directory.GetDirectories ( fullPath );
foreach ( string dir in dirs ) {
string virtualDir = node.Value.TrimEnd ( '/' ) + "/" + Path.GetFileName ( dir );
if ( virtualDir.IndexOf ( "admin" ) == -1 || virtualDir.IndexOf ( "tmp" ) == -1 ) {
TreeNode newNode = new TreeNode ( Path.GetFileName ( dir ), virtualDir );
if ( Directory.GetFiles ( dir, "*.asp?" ).Length > 0 || Directory.GetDirectories ( dir ).Length > 0 ) {
newNode.PopulateOnDemand = true;
node.ChildNodes.Add ( newNode );
}
}
}
// enumerate files
string [ ] files = Directory.GetFiles ( fullPath, "*.asp?" );
foreach ( string file in files ) {
TreeNode newNode = new TreeNode ( Path.GetFileName ( file ), Path.GetFileName ( file ) );
newNode.NavigateUrl = "~/" + file.Substring ( Request.PhysicalApplicationPath.Length );
node.ChildNodes.Add ( newNode );
}
}
</script>
</head>
<body>
<!-- #include virtual="~/shared/top.inc" -->
<div class="header"><h2>Populating <span class="hilite">TreeView Nodes</span> On Demand</h2></div>
<!-- #include virtual="~/shared/viewsrc_top.inc" -->
<hr size=1 width=92%>
<form runat="server">
<p align="center">This example demonstrates using the TreeView <i>PopulateOnDemand</i> feature<br>
to programmatically create <span class="hilite">TreeNodes</span> from the file system of the application that contains this sample. </p>
<div style="margin-left:50">
<asp:treeview id="myTreeView" runat="server"
imageset="XPFileExplorer"
nodestyle-horizontalpadding=3
showlines expanddepth=1 target="_ext"
onTreeNodePopulate="populateNode">
<nodes>
<asp:treenode text="asp.net.ph" value="~/" populateondemand />
</nodes>
</asp:treeview>
</div>
</form>
<hr size=1 width=92%>
<!-- #include virtual="~/shared/viewsrc.inc" -->
</body>
</html>