Code 1<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ServerAppFilePath.ascx.cs" Inherits="UserControl_ServerFilePath"%> 2<div style="width:400px; background-color:#ffffff; border-right: black thin solid; border-top: black thin solid; border-left: black thin solid; border-bottom: black thin solid;"> 3<asp:Panel ID="pnlTreeview" runat="server" Height="400px" Width="400px" ScrollBars="Auto" BackColor="White"> 4 <asp:TreeView ID="treeFileView" runat="server" ImageSet="XPFileExplorer" NodeIndent="15" ShowLines="True" OnSelectedNodeChanged="treeFileView_SelectedNodeChanged"> 5 <ParentNodeStyle Font-Bold="False" /> 6 <HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" /> 7 <SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" HorizontalPadding="0px" 8 VerticalPadding="0px" /> 9 <NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black" HorizontalPadding="2px"10 NodeSpacing="0px" VerticalPadding="2px" />11 </asp:TreeView>12</asp:Panel>13</div>
后台代码: Code 1//---------------------------------------------------------------------- 2// Ryan Wei 3// Date:2008.05.08 4//---------------------------------------------------------------------- 5// Description: 6//---------------------------------------------------------------------- 7using System; 8using System.Data; 9using System.Configuration; 10using System.Collections; 11using System.Web; 12using System.Web.Security; 13using System.Web.UI; 14using System.Web.UI.WebControls; 15using System.Web.UI.WebControls.WebParts; 16using System.Web.UI.HtmlControls; 17using System.IO; 18 19//委托 20public delegate void SelectedNodeChangedHandler(object sender, EventArgs e); 21 22public partial class UserControl_ServerFilePath : System.Web.UI.UserControl 23{ 24 Property#region Property 25 private string selectedFilePath; 26 public string SelectedFilePath 27 { 28 set { selectedFilePath = value; } 29 get { return selectedFilePath; } 30 } 31 #endregion 32 33 protected void Page_Load(object sender, EventArgs e) 34 { 35 if (!Page.IsPostBack) 36 { 37 TreeNode tn = new TreeNode(); 38 //Root Name 39 tn.Text = "Root"; 40 getDirectories(Server.MapPath("../Publish"), tn); 41 treeFileView.Nodes.Add(tn); 42 } 43 } 44 45 Control Event#region Control Event 46 /**//// <summary> 47 /// 选择节点变化时 48 /// </summary> 49 /// <param name="sender"></param> 50 /// <param name="e"></param> 51 protected void treeFileView_SelectedNodeChanged(object sender, EventArgs e) 52 { 53 SelectedFilePath = treeFileView.SelectedValue; 54 this.OnSelected(e); 55 } 56 #endregion 57 58 Static Functions#region Static Functions 59 60 /**//// <summary> 61 /// 循环遍历获得某一目录下的所有文件信息 62 /// </summary> 63 /// <param name="path">目录名</param> 64 /// <param name="tn">树节点</param> 65 private static void getDirectories(string path, TreeNode tn) 66 { 67 string[] fileNames = Directory.GetFiles(path); 68 string[] directories = Directory.GetDirectories(path); 69 70 //先遍历这个目录下的文件夹 71 foreach (string dir in directories) 72 { 73 TreeNode subtn = new TreeNode(); 74 subtn.Value = dir; 75 subtn.Text = GetShorterFileName(dir); 76 subtn.ImageUrl = "../Images/doc.png"; 77 subtn.Expanded = false; 78 getDirectories(dir, subtn); 79 tn.ChildNodes.Add(subtn); 80 } 81 82 //再遍历这个目录下的文件 83 foreach (string file in fileNames) 84 { 85 TreeNode subtn = new TreeNode(); 86 if (".application".IndexOf(file.Substring(file.LastIndexOf(".") + 1)) > -1) 87 { 88 subtn.ImageUrl = "../Images/file.png"; 89 subtn.Value = file; 90 subtn.Text = GetShorterFileName(file); 91 //subtn.ShowCheckBox = true; 92 tn.ChildNodes.Add(subtn); 93 } 94 } 95 } 96 97 /**//// <summary> 98 /// 滤去文件名前面的路径 99 /// </summary>100 /// <param name="filename"></param>101 /// <returns></returns>102 private static string GetShorterFileName(string filename)103 { 104 return Path.GetFileName(filename); 105 }106 #endregion107108 委托事件#region 委托事件109 public event SelectedNodeChangedHandler Selected;110111 protected void OnSelected(EventArgs e)112 { 113 if (Selected != null)114 { 115 Selected(this, e);116 }117 }118 #endregion 119}120121