|
在ASP.NET中,TreeView的使用很普遍,把它利用上來
首先加入TreeView控件
<ASP:TreeView ID="driverInfoView" runat="server" ImageSet="XPFileExplorer" OnTreeNodePopulate="driverInfoView_TreeNodePopulate"></ASP:TreeView>
當(dāng)頁(yè)面加載的時(shí)候,寫下以下代碼
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 + "(還沒準(zhǔn)備好)"; } this.driverInfoView.Nodes.Add(newNode); } }
設(shè)置TreeView節(jié)點(diǎn)的事件
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); }}
AspNet技術(shù):ASP.NET中使用TreeView顯示文件的方法,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。