欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET中使用TreeView显示文件的方法

程序员文章站 2024-02-24 12:37:10
在asp.net中,treeview的使用很普遍,把它利用上来 首先加入treeview控件

在asp.net中,treeview的使用很普遍,把它利用上来

首先加入treeview控件

<asp:treeview id="driverinfoview" runat="server" imageset="xpfileexplorer" ontreenodepopulate="driverinfoview_treenodepopulate">
</asp:treeview>

当页面加载的时候,写下以下代码

protected void page_load(object sender, eventargs e) 
{ 
foreach (driveinfo driverinfo in driveinfo.getdrives()) 
{ 
treenode newnode = new treenode(); 
newnode.expanded = false; 
newnode.populateondemand = true; 
newnode.value = driverinfo.name; 

if (driverinfo.isready) 
{ 
newnode.text = driverinfo.name + "(" + driverinfo.volumelabel + ")"; 
} 
else 
{ 
newnode.text = driverinfo.name + "(还没准备好)"; 
} 
this.driverinfoview.nodes.add(newnode); 
} 
}

设置treeview节点的事件

protected void driverinfoview_treenodepopulate(object sender, treenodeeventargs e) 
{ 
directoryinfo dictinfo = new directoryinfo(e.node.value); 

foreach (directoryinfo directory in dictinfo.getdirectories()) 
{ 
treenode newnode = new treenode(); 
newnode.expanded = false; 
newnode.populateondemand = true; 
newnode.text = directory.name; 
newnode.value = directory.fullname; 
e.node.childnodes.add(newnode); 
} 

foreach (fileinfo fileinfo in dictinfo.getfiles()) 
{ 
treenode newnode = new treenode(); 
newnode.text = fileinfo.name; 
newnode.value = fileinfo.fullname; 
e.node.childnodes.add(newnode); 
}
}