.net Treeview无限级无刷新示例

本文介绍如何使用ASP.NET中的TreeView控件结合C#编程实现文件系统的树状展示功能,通过递归加载目录及文件信息,实现动态生成树形结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<script runat="server">

void Treeview1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
if (IsCallback)
if (e.Node.ChildNodes.Count == 0)
{
LoadChildNode(e.Node);
}

}

private void LoadChildNode(TreeNode node)
{

DirectoryInfo directory;
directory = new DirectoryInfo(node.Value);

foreach (DirectoryInfo sub in directory.GetDirectories())
{

TreeNode subNode = new TreeNode(sub.Name);
subNode.Value = sub.FullName;

try
{
if (sub.GetDirectories().Length > 0    sub.GetFiles().Length > 0)
{
subNode.SelectAction = TreeNodeSelectAction.SelectExpand;
subNode.PopulateOnDemand = true;
subNode.NavigateUrl = "#";
}
}
catch { subNode.ImageUrl = "WebResource.axd?a=sr=TreeView_XP_Explorer_ParentNode.gif&t=632242003305625000"; }
node.ChildNodes.Add(subNode); 
 }

 foreach (FileInfo fi in directory.GetFiles())
{
TreeNode subNode = new TreeNode(fi.Name);
node.ChildNodes.Add(subNode);
}
}


</script>

name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-1572879403720716&dt=1188263129234&hl=zh-CN&lmt=1187838976&alternate_ad_url=http%3A%2F%2Fwww.qqread.com%2F0000js%2Fgoogle300.htm&format=300x250_as&output=html&correlator=1188263129218&channel=0168087865&url=http%3A%2F%2Fwww.qqread.com%2Faspdotnet%2Fu294824_2.html&color_bg=F9FCFE&color_text=000000&color_link=1F3A87&color_url=FF6666&color_border=F9FCFE&ad_type=text_image&ref=http%3A%2F%2Fwww.qqread.com%2Faspdotnet%2Fu294824.html&cc=100&ga_vid=1254641071.1188262273&ga_sid=1188262273&ga_hid=1418957026&ga_fc=true&flash=9&u_h=1024&u_w=1280&u_ah=996&u_aw=1280&u_cd=32&u_tz=480&u_his=1&u_java=true" frameborder="0" width="300" scrolling="no" height="250" allowtransparency="allowtransparency">

html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>

<body bgcolor="white">
<form id="form1" runat="server">
<div>
<asp:treeview ID="Treeview1" runat="server" ImageSet="XPFileExplorer" AutoGenerateDataBindings="false" ExpandDepth=0
OnTreeNodePopulate="Treeview1_TreeNodePopulate"

<SelectedNodeStyle BackColor="#B5B5B5"></SelectedNodeStyle>
<Nodes>
<asp:TreeNode Value="C:" Text="C:" PopulateOnDemand="true" SelectAction="Select" NavigateUrl="#" >
</asp:TreeNode>
</Nodes>
<NodeStyle VerticalPadding="2" Font-Names="Tahoma" Font-Size="8pt" HorizontalPadding="2"
ForeColor="Black"></NodeStyle>
<HoverNodeStyle Font-Underline="True" ForeColor="#6666AA"></HoverNodeStyle>
</asp:treeview>
</div>
</form>
</body>
</html>

更多文章 更多内容请看.NET开发手册  .NET移动与嵌入式技术专题讨论。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值