ASP.NET开发中如何使用TreeView显示文件
来源:爱站网时间:2020-10-23编辑:网友分享
ASP.NET开发中Treeview被诸多程序员广泛使用,其实在页面加载时添加treeview控件,今天就让爱站技术频道小编带大家了解ASP.NET开发中如何使用TreeView显示文件吧!
ASP.NET开发中Treeview被诸多程序员广泛使用,其实在页面加载时添加treeview控件,今天就让爱站技术频道小编带大家了解ASP.NET开发中如何使用TreeView显示文件吧!
1.首先加入TreeView控件:
2.当页面加载的时候,写下以下代码:
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); } }
3.设置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); } }
以上就是爱站技术频道小编带来的ASP.NET开发中如何使用TreeView显示文件,这里有很多成功的实例,希望能为你牌友解困。