[ASP.NET]Treeview 控件显示服务端目录文件夹及文件

使用UserControl实现。

同时在用户控件上定义了委托,以实现与基页面的事件相结合。

VS2005 (Asp.net Ajax)

前端代码:
ContractedBlock.gif ExpandedBlockStart.gif Code
 1None.gif<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ServerAppFilePath.ascx.cs" Inherits="UserControl_ServerFilePath"%>
 2None.gif<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;">
 3None.gif<asp:Panel ID="pnlTreeview" runat="server" Height="400px" Width="400px" ScrollBars="Auto" BackColor="White">
 4None.gif    <asp:TreeView ID="treeFileView" runat="server" ImageSet="XPFileExplorer" NodeIndent="15" ShowLines="True" OnSelectedNodeChanged="treeFileView_SelectedNodeChanged">
 5None.gif        <ParentNodeStyle Font-Bold="False" />
 6None.gif        <HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
 7None.gif        <SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" HorizontalPadding="0px"
 8None.gif            VerticalPadding="0px" />
 9None.gif        <NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black" HorizontalPadding="2px"
10None.gif            NodeSpacing="0px" VerticalPadding="2px" />
11None.gif    </asp:TreeView>
12None.gif</asp:Panel>
13None.gif</div>

后台代码:
ContractedBlock.gif ExpandedBlockStart.gif Code
  1None.gif//----------------------------------------------------------------------
  2None.gif// Ryan Wei
  3None.gif// Date:2008.05.08
  4None.gif//----------------------------------------------------------------------
  5None.gif// Description: 
  6None.gif//----------------------------------------------------------------------
  7None.gifusing System;
  8None.gifusing System.Data;
  9None.gifusing System.Configuration;
 10None.gifusing System.Collections;
 11None.gifusing System.Web;
 12None.gifusing System.Web.Security;
 13None.gifusing System.Web.UI;
 14None.gifusing System.Web.UI.WebControls;
 15None.gifusing System.Web.UI.WebControls.WebParts;
 16None.gifusing System.Web.UI.HtmlControls;
 17None.gifusing System.IO;
 18None.gif
 19None.gif//委托
 20None.gifpublic delegate void SelectedNodeChangedHandler(object sender, EventArgs e);
 21None.gif
 22None.gifpublic partial class UserControl_ServerFilePath : System.Web.UI.UserControl
 23ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 24ContractedSubBlock.gifExpandedSubBlockStart.gif    Property#region Property
 25InBlock.gif    private string selectedFilePath;
 26InBlock.gif    public string SelectedFilePath
 27ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 28ExpandedSubBlockStart.gifContractedSubBlock.gif        set dot.gif{ selectedFilePath = value; }
 29ExpandedSubBlockStart.gifContractedSubBlock.gif        get dot.gifreturn selectedFilePath; }
 30ExpandedSubBlockEnd.gif    }

 31ExpandedSubBlockEnd.gif    #endregion

 32InBlock.gif
 33InBlock.gif    protected void Page_Load(object sender, EventArgs e)
 34ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 35InBlock.gif        if (!Page.IsPostBack)
 36ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 37InBlock.gif            TreeNode tn = new TreeNode();
 38InBlock.gif            //Root Name
 39InBlock.gif            tn.Text = "Root";
 40InBlock.gif            getDirectories(Server.MapPath("../Publish"), tn);
 41InBlock.gif            treeFileView.Nodes.Add(tn);
 42ExpandedSubBlockEnd.gif        }

 43ExpandedSubBlockEnd.gif    }

 44InBlock.gif
 45ContractedSubBlock.gifExpandedSubBlockStart.gif    Control Event#region Control Event
 46ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 47InBlock.gif    /// 选择节点变化时
 48InBlock.gif    /// </summary>
 49InBlock.gif    /// <param name="sender"></param>
 50ExpandedSubBlockEnd.gif    /// <param name="e"></param>

 51InBlock.gif    protected void treeFileView_SelectedNodeChanged(object sender, EventArgs e)
 52ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 53InBlock.gif        SelectedFilePath = treeFileView.SelectedValue;
 54InBlock.gif        this.OnSelected(e);
 55ExpandedSubBlockEnd.gif    }

 56ExpandedSubBlockEnd.gif    #endregion

 57InBlock.gif
 58ContractedSubBlock.gifExpandedSubBlockStart.gif    Static Functions#region Static Functions
 59InBlock.gif
 60ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 61InBlock.gif    /// 循环遍历获得某一目录下的所有文件信息
 62InBlock.gif    /// </summary>
 63InBlock.gif    /// <param name="path">目录名</param>
 64ExpandedSubBlockEnd.gif    /// <param name="tn">树节点</param>

 65InBlock.gif    private static void getDirectories(string path, TreeNode tn)
 66ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 67InBlock.gif        string[] fileNames = Directory.GetFiles(path);
 68InBlock.gif        string[] directories = Directory.GetDirectories(path);
 69InBlock.gif
 70InBlock.gif        //先遍历这个目录下的文件夹
 71InBlock.gif        foreach (string dir in directories)
 72ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 73InBlock.gif            TreeNode subtn = new TreeNode();
 74InBlock.gif            subtn.Value = dir;
 75InBlock.gif            subtn.Text = GetShorterFileName(dir);
 76InBlock.gif            subtn.ImageUrl = "../Images/doc.png";
 77InBlock.gif            subtn.Expanded = false;
 78InBlock.gif            getDirectories(dir, subtn);
 79InBlock.gif            tn.ChildNodes.Add(subtn);
 80ExpandedSubBlockEnd.gif        }

 81InBlock.gif
 82InBlock.gif        //再遍历这个目录下的文件
 83InBlock.gif        foreach (string file in fileNames)
 84ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 85InBlock.gif            TreeNode subtn = new TreeNode();
 86InBlock.gif            if (".application".IndexOf(file.Substring(file.LastIndexOf("."+ 1)) > -1)
 87ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 88InBlock.gif                subtn.ImageUrl = "../Images/file.png";
 89InBlock.gif                subtn.Value = file;
 90InBlock.gif                subtn.Text = GetShorterFileName(file);
 91InBlock.gif                //subtn.ShowCheckBox = true;
 92InBlock.gif                tn.ChildNodes.Add(subtn);
 93ExpandedSubBlockEnd.gif            }

 94ExpandedSubBlockEnd.gif        }

 95ExpandedSubBlockEnd.gif    }

 96InBlock.gif
 97ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 98InBlock.gif    /// 滤去文件名前面的路径
 99InBlock.gif    /// </summary>
100InBlock.gif    /// <param name="filename"></param>
101ExpandedSubBlockEnd.gif    /// <returns></returns>

102InBlock.gif    private static string GetShorterFileName(string filename)
103ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
104InBlock.gif        return Path.GetFileName(filename); 
105ExpandedSubBlockEnd.gif    }

106ExpandedSubBlockEnd.gif    #endregion

107InBlock.gif
108ContractedSubBlock.gifExpandedSubBlockStart.gif    委托事件#region 委托事件
109InBlock.gif    public event SelectedNodeChangedHandler Selected;
110InBlock.gif
111InBlock.gif    protected void OnSelected(EventArgs e)
112ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
113InBlock.gif        if (Selected != null)
114ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
115InBlock.gif            Selected(this, e);
116ExpandedSubBlockEnd.gif        }

117ExpandedSubBlockEnd.gif    }

118ExpandedSubBlockEnd.gif    #endregion
     
119ExpandedBlockEnd.gif}

120None.gif
121None.gif

转载于:https://www.cnblogs.com/wyforumid/archive/2008/05/13/1195561.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值