ASP.NET中使用TreeView显示文件
作者:佚名    ASP.NET网站开发编辑:admin   更新时间:2022-07-23
asp.net中使用TreeView显示文件
在ASP.NET中,TreeView的使用很普遍,把它利用上来
首先加入TreeView控件
Html代码
- <asp:TreeViewID="driverInfoView"runat="server"ImageSet="XPFileExplorer"OnTreeNodePopulate="driverInfoView_TreeNodePopulate">
- </asp:TreeView>
当页面加载的时候,写下以下代码
C#代码
- PRotectedvoidPage_Load(objectsender,EventArgse)
- {
- foreach(DriveInfodriverInfoinDriveInfo.GetDrives())
- {
- TreeNodenewNode=newTreeNode();
- 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节点的事件
C#代码
- protectedvoiddriverInfoView_TreeNodePopulate(objectsender,TreeNodeEventArgse)
- {
- DirectoryInfodictInfo=newDirectoryInfo(e.Node.Value);
- foreach(DirectoryInfodirectoryindictInfo.GetDirectories())
- {
- TreeNodenewNode=newTreeNode();
- newNode.Expanded=false;
- newNode.PopulateOnDemand=true;
- newNode.Text=directory.Name;
- newNode.Value=directory.FullName;
- e.Node.ChildNodes.Add(newNode);
- }
- foreach(FileInfofileInfoindictInfo.GetFiles())
- {
- TreeNodenewNode=newTreeNode();
- newNode.Text=fileInfo.Name;
- newNode.Value=fileInfo.FullName;
- e.Node.ChildNodes.Add(newNode);
- }
- }