<%@ Import namespace="System.IO" %>
<html>
<head>
<title>Populating TreeView Nodes On Demand Using Page Titles</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<script language="C#" runat=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 ) {
if ( file.IndexOf ( ".aspx." ) == -1 ) {
// open web page
System.Net.WebRequest request = System.Net.WebRequest.Create ( file );
System.Net.WebResponse response = request.GetResponse ( );
StreamReader reader = new StreamReader ( response.GetResponseStream ( ) );
string page = reader.ReadToEnd ( );
reader.Close ( );
response.Close ( );
// get page title
string title = Regex.Match ( page, "<title>(?<title>[^<]+)</title>",
RegexOptions.IgnoreCase ).Groups["title"].Value;
if ( title != "" ) {
TreeNode newNode = new TreeNode ( title, 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 Using <span class="hilite">Page Titles</span></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 that display page titles instead of file names. </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>