Use the TreeView display file in ASP. NET, asp. nettreeview
In ASP. NET, TreeView is widely used.
Add the TreeView control first.
Html code
- <Asp: TreeView ID = "driverInfoView" runat = "server" ImageSet = "XPFileExplorer" OnTreeNodePopulate = "driverInfoView_TreeNodePopulate">
- </Asp: TreeView>
When loading a page, write the following code
C # code
- 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 + "(not ready yet )";
- }
- This. driverInfoView. Nodes. Add (newNode );
- }
- }
Set events of the TreeView Node
C # code
- 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 );
- }
- }
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.