Fly TreeView 教程
时间:2010-09-09 来源:情缘鸟
1 <%@ Register Assembly="NineRays.WebControls.FlyTreeView" Namespace="NineRays.WebControls"
2 TagPrefix="NineRays" %>
3
4 <NineRays:FlyTreeView ID="flyTreeView" runat="server" BorderColor="Silver" BorderWidth="1px"
5 Height="320px" Width="280px" Padding="3px" OnPopulateNodes="flyTreeView_PopulateNodes"
6 FadeEffect="True" Style="display: block" BackColor="White" ImageSet="Vista" DrawLines="False">
7 <HoverStyle Font-Underline="True" />
8 <SelectedStyle BackColor="#D6F0FD" BorderColor="#9ADFFE" BorderStyle="Solid" BorderWidth="1px"
9 Padding="2px;6px;6px;2px" />
10 <DefaultStyle Font-Names="Tahoma" Font-Size="11px" ForeColor="Black" ImageUrl="$vista_folder"
11 Padding="3px;7px;7px;3px" RowHeight="18px" />
12 </NineRays:FlyTreeView>
13
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
AddNodes(flyTreeView.Nodes, string.Empty);
}
}
protected void flyTreeView_PopulateNodes(object sender, NineRays.WebControls.FlyTreeNodeEventArgs e)
{
AddNodes(e.Node.ChildNodes, e.Node.Value.ConvertType(0));
}
private const string ROOT_FOLDER = @"C:\";
protected void AddNodes(FlyTreeNodeCollection nodes, string path)
{
string folder = Path.Combine(ROOT_FOLDER, path);
DirectoryInfo directory = new DirectoryInfo(Path.Combine(ROOT_FOLDER, path));
if (!directory.Exists) return;
DirectoryInfo[] subDirectories = directory.GetDirectories();
foreach (DirectoryInfo subDir in subDirectories)
{
FlyTreeNode ftn = new FlyTreeNode();
ftn.Text = HttpUtility.HtmlEncode(subDir.Name);
ftn.Value = subDir.Name;
nodes.Add(ftn);
try
{
// do not set populate on demand for nodes that have no nested directories
ftn.PopulateNodesOnDemand = subDir.GetDirectories().Length > 0;
}
catch (Exception ex)
{
// add a child node that shows that there was an exception trying to get its child nodes
AddExceptionNode(ftn.ChildNodes, ex);
}
}
}
protected void AddExceptionNode(FlyTreeNodeCollection nodes, Exception ex)
{
FlyTreeNode ftn = new FlyTreeNode();
ftn.Text = HttpUtility.HtmlEncode(ex.Message);
ftn.NodeTypeID = "errorType";
nodes.Add(ftn);
}
相关阅读 更多 +