Asp. Method of using TreeView to display files in net _ practical tips

Source: Internet
Author: User

In ASP.net, the use of the TreeView is common, and it is used to

First join the TreeView control

<asp:treeview id= "Driverinfoview" runat= "imageset=" Xpfileexplorer "ontreenodepopulate=" DriverInfoView_ Treenodepopulate ">
</asp:TreeView>

When the page loads, write down the following code

protected void Page_Load (object sender, EventArgs e) 
{ 
foreach (DriveInfo driverinfo in Driveinfo.getdrives ()) c5/>{ 
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); 
} 

Setting events for the TreeView node

 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); }
}
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.