http://www.dotnetjunkies.com/QuickStartv20/aspnet/doc/ctrlref/navigation/treeview.aspx
<%@ Page Language="C#" %>
<head runat="server"/>
<h2>Customizing TreeView Styles</h2>
<form runat="server">
<asp:TreeView
RootNodeStyle-ImageUrl="~/images/xp/computer.gif"
ParentNodeStyle-ImageUrl="~/images/xp/folder.gif"
LeafNodeStyle-ImageUrl="~/images/xp/ie.gif"
NodeIndent="20"
runat="server"
>
<NodeStyle Font-Name="Arial" Font-Size="8pt" ForeColor="DarkBlue" HorizontalPadding="5"/>
<RootNodeStyle Font-Bold Font-Size="9pt"/>
<HoverNodeStyle Font-UnderLine/>
<Nodes>
<asp:TreeNode Text="My Computer" Expanded="true">
<asp:TreeNode Text="Favorites" ImageUrl="~/images/xp/star.gif">
<asp:TreeNode Text="News">
<asp:TreeNode Text="MSN" NavigateUrl="http://www.msn.com"/>
<asp:TreeNode Text="MSNBC News" NavigateUrl="http://www.msnbc.msn.com"/>
</asp:TreeNode>
<asp:TreeNode Text="Technology">
<asp:TreeNode Text="Microsoft" NavigateUrl="http://www.microsoft.com"/>
<asp:TreeNode Text="ASP.NET" NavigateUrl="http://www.asp.net"/>
<asp:TreeNode Text="GotDotNet" NavigateUrl="http://www.gotdotnet.com"/>
<asp:TreeNode Text="MSDN" NavigateUrl="http://msdn.microsoft.com"/>
</asp:TreeNode>
<asp:TreeNode Text="Shopping">
<asp:TreeNode Text="MSN Shopping" NavigateUrl="http://shopping.msn.com"/>
<asp:TreeNode Text="MSN Autos" NavigateUrl="http://autos.msn.com"/>
</asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode Text="City Links">
<asp:TreeNode Text="MapPoint" NavigateUrl="http://www.mappoint.com"/>
<asp:TreeNode Text="MSN City Guides" NavigateUrl="http://local.msn.com"/>
</asp:TreeNode>
<asp:TreeNode Text="Music Links">
<asp:TreeNode Text="MSN Music" NavigateUrl="http://music.msn.com"/>
</asp:TreeNode>
</asp:TreeNode>
</Nodes>
</asp:TreeView>
</form>
<!--/////////////////////////////////////////-->
<%@ Page Language="C#" Theme="Default" %>
<head runat="server"/>
<script runat="server">
void MyTree_Select (Object source, EventArgs e) {
String value = MyTree.SelectedNode.Value;
String path = MyTree.SelectedNode.ValuePath;
Response.Write ("The value selected was " + value + "<br>");
Response.Write ("The value path is " + path + "<br>");
}
</script>
<body bgcolor="#eeeeee">
<h2>TreeView Selection</h2>
<form runat="server">
<asp:TreeView Id="MyTree" SkinId="MSDN"
OnSelectedNodeChanged="MyTree_Select"
ExpandDepth="0"
runat="server"
>
<SelectedNodeStyle BackColor="White" BorderWidth="1"/>
<Nodes>
<asp:TreeNode Text="Welcome to Code Center" Value="1.0" />
<asp:TreeNode Text="Data Binding" Value="2.0" SelectAction="Expand">
<asp:TreeNode Text="ANCHOR Element" Value="2.1"/>
<asp:TreeNode Text="APPLET Element" Value="2.2"/>
<asp:TreeNode Text="BUTTON Element" Value="2.3" SelectAction="Expand">
<asp:TreeNode Text="Text Attribute" Value="2.3.1"/>
<asp:TreeNode Text="Value Attribute" Value="2.3.2"/>
</asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode Text="Data Source Objects" Value="3.0" SelectAction="Expand">
<asp:TreeNode Text="Array (VB)" Value="3.1"/>
<asp:TreeNode Text="Calendar Applet (Java)" Value="3.2"/>
<asp:TreeNode Text="Dual DSO (HTML)" Value="3.3"/>
<asp:TreeNode Text="Loan Calculator (VB)" Value="3.4"/>
</asp:TreeNode>
<asp:TreeNode Text="DirectX Transformations" Value="4.0" SelectAction="Expand">
<asp:TreeNode Text="Settings Reference" Value="4.1"/>
</asp:TreeNode>
<asp:TreeNode Text="Content & Component Delivery" Value="5.0" SelectAction="Expand">
<asp:TreeNode Text="Channel" Value="5.1"/>
<asp:TreeNode Text="IISLog" Value="5.2"/>
</asp:TreeNode>
<asp:TreeNode Text="Common Controls" Value="6.0" SelectAction="Expand">
<asp:TreeNode Text="Rebar Control" Value="6.1"/>
<asp:TreeNode Text="Virtual List View Control" Value="6.2"/>
</asp:TreeNode>
<asp:TreeNode Text="Component Development" Value="7.0" SelectAction="Expand">
<asp:TreeNode Text="Band Object Implementation" Value="7.1"/>
</asp:TreeNode>
<asp:TreeNode Text="Internet Explorer 5 Demos" Value="8.0" SelectAction="Expand">
<asp:TreeNode Text="Ad Banner" Value="8.1"/>
</asp:TreeNode>
<asp:TreeNode Text="Internet Explorer 5.5 Demos" Value="9.0" SelectAction="Expand">
<asp:TreeNode Text="Binary Behavior" Value="9.1"/>
</asp:TreeNode>
<asp:TreeNode Text="Internet Explorer 6 Demos" Value="10.0" SelectAction="Expand">
<asp:TreeNode Text="HTML+TIME Transitions" Value="10.1"/>
<asp:TreeNode Text="HTMLEditor" Value="10.2"/>
</asp:TreeNode>
<asp:TreeNode Text="Messaging & Collaboration" Value="11.0" SelectAction="Expand">
<asp:TreeNode Text="WAB Property Inspector Tool" Value="11.1"/>
</asp:TreeNode>
<asp:TreeNode Text="Windows Media" Value="12.0" SelectAction="Expand">
<asp:TreeNode Text="Simple ASX" Value="12.1"/>
</asp:TreeNode>
</Nodes>
</asp:TreeView>
</form>
</body>
<!--/////////////////////// 遍历目录下所有文件 /////////////////////////////-->
<%@ Page Language="C#" Theme="Default" %>
<%@ Import Namespace="System.IO" %>
<html>
<head runat="server" />
<script language="C#" runat="server">
static readonly char[] _slashArray = new char[] {'/'};
void PopulateNode(Object source, TreeNodeEventArgs e)
{
TreeNode node = e.Node;
if (node.Value == "Demos")
node.Value = "~/";
String rootDirectory = Request.MapPath("~/", Request.ApplicationPath, false);
String fullPath = Request.MapPath(node.Value, Request.ApplicationPath, false);
if (fullPath.StartsWith(rootDirectory) == false)
{
// Mitigate against spoofed callback arguments
// Requested directory is not under root directory
return;
}
String[] dirs = Directory.GetDirectories(fullPath);
// Enumerate directories
foreach (String dir in dirs)
{
String virtualDir = node.Value.TrimEnd(_slashArray) + "/" + Path.GetFileName(dir);
TreeNode newNode = new TreeNode(Path.GetFileName(dir), virtualDir);
if (Directory.GetFiles(dir).Length > 0
|| Directory.GetDirectories(dir).Length > 0)
{
newNode.PopulateOnDemand = true;
}
node.ChildNodes.Add(newNode);
}
// Enumerate files
String[] files = Directory.GetFiles(fullPath);
foreach (String file in files)
{
TreeNode newNode = new TreeNode(Path.GetFileName(file), Path.GetFileName(file));
node.ChildNodes.Add(newNode);
}
}
</script>
<body>
<form runat="server">
<h2>
Populating TreeView Nodes On-Demand</h2>
<asp:TreeView ID="MyTree" SkinID="Explorer" PathSeparator="|" OnTreeNodePopulate="PopulateNode"
ExpandDepth="1" runat="server">
<Nodes>
<asp:TreeNode Text="Demos" PopulateOnDemand="true" />
</Nodes>
</asp:TreeView>
</form>
</body>
</html>
<!--****************************** 数据库应用 *****************************-->
<%@ Page Theme="Default" %>
<html>
<head runat="server"/>
<script language="C#" runat="server">
void GetProductCategories (TreeNode node) {
CategoryList categories = WarehouseDB.GetProductCategories();
foreach (Category c in categories) {
TreeNode newNode = new TreeNode(c.Name, c.Id);
newNode.SelectAction = TreeNodeSelectAction.Expand;
newNode.PopulateOnDemand = true;
node.ChildNodes.Add(newNode);
}
}
void GetProductsForCategory (TreeNode node) {
String categoryId = node.Value;
ProductList products = WarehouseDB.GetProductsForCategory(categoryId);
foreach (Product p in products) {
TreeNode newNode = new TreeNode(p.Name, p.Id);
node.ChildNodes.Add(newNode);
}
}
void PopulateNode (Object source, TreeNodeEventArgs e) {
switch (e.Node.Depth) {
case 0 :
GetProductCategories(e.Node);
break;
case 1 :
GetProductsForCategory(e.Node);
break;
}
}
</script>
<body>
<h2>Populating TreeView Nodes from a SQL Database</h2>
<form runat="server">
<asp:TreeView OnTreeNodePopulate="PopulateNode" SkinId="Simple" Width="250" ExpandDepth="0"
runat="server">
<Nodes>
<asp:TreeNode Text="Inventory" SelectAction="Expand" PopulateOnDemand="true"/>
</Nodes>
</asp:TreeView>
</form>
</body>
</html>
<!--**************************** 绑定 XML 文件 ****************************-->
<%@ Page Language="C#" Theme="Default" %>
<html>
<body>
<form runat="server">
<asp:XmlDataSource id="MySource" DataFile="~/App_Data/Bookstore.xml" runat="server"/>
<asp:TreeView
SkinId="Bookstore"
DataSourceId="MySource"
ExpandDepth="3"
MaxDataBindDepth="3"
runat="server"
/>
</form>
</body>
</html>
<!--//XML文件-->
<Bookstore>
<genre name="Business">
<book ISBN="BU1032" Title="The Busy Executive's Database Guide" Price="19.99">
<chapter num="1" name="Introduction">
Abstract...
</chapter>
<chapter num="2" name="Body">
Abstract...
</chapter>
<chapter num="3" name="Conclusion">
Abstract...
</chapter>
</book>
<book ISBN="BU2075" Title="You Can Combat Computer Stress!" Price="2.99">
<chapter num="1" name="Introduction">
Abstract...
</chapter>
<chapter num="2" name="Body">
Abstract...
</chapter>
<chapter num="3" name="Conclusion">
Abstract...
</chapter>
</book>
<book ISBN="BU7832" Title="Straight Talk About Computers" Price="19.99">
<chapter num="1" name="Introduction">
Abstract...
</chapter>
<chapter num="2" name="Body">
Abstract...
</chapter>
<chapter num="3" name="Conclusion">
Abstract...
</chapter>
</book>
</genre>
</Bookstore>
<!--**********************绑定 SiteMap ******************************-->
<?xml version="1.0" encoding="utf-8" ?>
<siteMap>
<siteMapNode title="ASP.NET Tutorials" url="~/default.aspx" >
<siteMapNode title="Page Development" url="~/pages.aspx">
<siteMapNode title="Master Pages" url="~/masterpages.aspx"/>
<siteMapNode title="Themes and Styles" url="~/themes.aspx"/>
<siteMapNode title="Site Navigation" url="~/sitenav.aspx"/>
<siteMapNode title="Client Side Script" url="~/client.aspx"/>
</siteMapNode>
<siteMapNode title="Developer Infrastructure" url="~/developer.aspx">
<siteMapNode title="Membership and Roles" url="~/membership.aspx"/>
<siteMapNode title="Personalization" url="~/personalization.aspx"/>
<siteMapNode title="Profile" url="~/profile.aspx"/>
<siteMapNode title="State Management" url="~/state.aspx"/>
<siteMapNode title="Post-Cache Substitution " url="~/postcache.aspx"/>
</siteMapNode>
<siteMapNode title="Server Controls" url="~/controls.aspx">
<siteMapNode title="GridView Control" url="~/gridview.aspx"/>
<siteMapNode title="Menu Control" url="~/menu.aspx"/>
<siteMapNode title="Wizard Control" url="~/wizard.aspx"/>
<siteMapNode title="Login Control" url="~/login.aspx"/>
</siteMapNode>
</siteMapNode>
</siteMap>
<%@ Page Language="C#" Theme="Default" %>
<html>
<head runat="server"/>
<body>
<form runat="server">
<h2>DataBinding TreeView to SiteMapDataSource</h2>
<asp:SiteMapDataSource id="SiteMapSource" runat="server"/>
<asp:TreeView id="MyTreeView"
SkinId="BulletedList3"
DataSourceId="SiteMapSource"
runat="server"
>
<LevelStyles>
<asp:TreeNodeStyle Font-Bold Font-Size="10pt" ChildNodesPadding="5" />
<asp:TreeNodeStyle Font-Underline ChildNodesPadding="5" />
</LevelStyles>
<Databindings>
<asp:TreeNodeBinding TextField="Title" NavigateUrlField="Url" />
</Databindings>
</asp:TreeView>
</form>
</body>
</html>