Use the TreeView display file in ASP. NET, asp. nettreeview

Source: Internet
Author: User

Use the TreeView display file in ASP. NET, asp. nettreeview

In ASP. NET, TreeView is widely used.

Add the TreeView control first.

Html code
  1. <Asp: TreeView ID = "driverInfoView" runat = "server" ImageSet = "XPFileExplorer" OnTreeNodePopulate = "driverInfoView_TreeNodePopulate">
  2. </Asp: TreeView>

When loading a page, write the following code

C # code
  1. Protected void Page_Load (object sender, EventArgs e)
  2. {
  3. Foreach (DriveInfo driverInfo in DriveInfo. GetDrives ())
  4. {
  5. TreeNode newNode = new TreeNode ();
  6. NewNode. Expanded = false;
  7. NewNode. PopulateOnDemand = true;
  8. NewNode. Value = driverInfo. Name;
  9. If (driverInfo. IsReady)
  10. {
  11. NewNode. Text = driverInfo. Name + "(" + driverInfo. VolumeLabel + ")";
  12. }
  13. Else
  14. {
  15. NewNode. Text = driverInfo. Name + "(not ready yet )";
  16. }
  17. This. driverInfoView. Nodes. Add (newNode );
  18. }
  19. }

Set events of the TreeView Node

C # code
  1. Protected void driverInfoView_TreeNodePopulate (object sender, TreeNodeEventArgs e)
  2. {
  3. DirectoryInfo dictInfo = new DirectoryInfo (e. Node. Value );
  4. Foreach (DirectoryInfo directory in dictInfo. GetDirectories ())
  5. {
  6. TreeNode newNode = new TreeNode ();
  7. NewNode. Expanded = false;
  8. NewNode. PopulateOnDemand = true;
  9. NewNode. Text = directory. Name;
  10. NewNode. Value = directory. FullName;
  11. E. Node. ChildNodes. Add (newNode );
  12. }
  13. Foreach (FileInfo fileInfo in dictInfo. GetFiles ())
  14. {
  15. TreeNode newNode = new TreeNode ();
  16. NewNode. Text = fileInfo. Name;
  17. NewNode. Value = fileInfo. FullName;
  18. E. Node. ChildNodes. Add (newNode );
  19. }
  20. }

 


How to display the file directory in treeview in aspnet and the file content

Treeview is a navigation control. It is used to display website maps. It's not a file directory ~

Use treeview in aspnet to display the website directory structure

Give you a thought, you store the website structure into the database

Id nodeName parentID

1 parent node 0

2 subnode 1

Then recursively bind something.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.