。net控件学习之使用TreeView显示文件夹
--------------------------------------------
1.使用TV显示文件夹
-------------------------------------
前台:
//ImageSet:可使用系统提供视图;OnTreeNodePopulate:展开时触发事件;PopulateNodesFromClient:为true时表示异步从服务器获得子节点
//Value:地址;Expanded:第一节点不可扩展;PopulateOnDemand:异步
-------------------------------------
<div>
<asp:TreeView runat="server" ID="TreeView1"
ImageSet="XPFileExplorer"
OnTreeNodePopulate="TreeView1_TreeNodePopulate"
PopulateNodesFromClient="true">
<Nodes>
<asp:TreeNode
Text="自学"
Value="C:\Documents and Settings\Administrator\桌面\自学"
Expanded="false"
PopulateOnDemand="true" />
</Nodes>
</asp:TreeView>
</div>
-------------------------------------
后台:
-------------------------------------
//TreeNodePopulate()指填充TreeNode时激发的事件,带有TreeNodeEventArgs属性
protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
//需要引入命名空间System.IO,用于提取目录
//其中DirectoryInfo设置提取文件夹,而FileInfo是提取出文件
DirectoryInfo info = new DirectoryInfo(e.Node.Value);
foreach (DirectoryInfo directory in info.GetDirectories())
{
TreeNode newNode = new TreeNode();
newNode.PopulateOnDemand = true;
newNode.Text = directory.Name;
newNode.Value = directory.FullName;
e.Node.ChildNodes.Add(newNode);
}
foreach (FileInfo file in info.GetFiles())
{
TreeNode newNode = new TreeNode();
newNode.Text = file.Name;
newNode.Value = file.FullName;
e.Node.ChildNodes.Add(newNode);
}
}
--------------------------------------------
----使用示例
--------------------------------------------