Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to expand treeview onload and setup links

Status
Not open for further replies.

itrickski

Programmer
May 30, 2009
1
US
i have setup a treeview control and am reading a directory to populate the nodes (I want to read all files/folders under c:\humanres\jobdesc\). it works well, except for 2 things.

1) when the page first loads, i'd like the treeview to expand and show the root folder plus one level of child files/folders. Right now, it only shows the root folder. setting ExpandDepth property to "1" causes the program to stop functioning.

2) i'd like for each file to be a clickable link, allowing the user to click and open the file in the appropriate program (MS Word, Excel, etc).
----CODE STARTS HERE----
****************************** CODE BEHIND FILE ****************************************
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class job_descriptions : System.Web.UI.Page
{


protected void Page_Load(object sender, EventArgs e)
{


}

protected void TreeView_jobdesc_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
if (IsCallback)
{
if (e.Node.ChildNodes.Count == 0)
{
LoadChildNode(e.Node);
}

}
}
private void LoadChildNode(TreeNode node)
{
DirectoryInfo directory;

directory = new DirectoryInfo(node.Value);
foreach (DirectoryInfo sub in directory.GetDirectories())
{
if (sub.Name != "_vti_cnf")
{
TreeNode subNode = new TreeNode(sub.Name); // create a new tree subdir node
if (sub.Name == "jobdesc")
subNode.Expanded = true;
subNode.Value = sub.FullName; // grab the name of the sub directory and store it in new sub node
try
{
if (sub.GetDirectories().Length > 0 || sub.GetFiles().Length > 0)
{
subNode.SelectAction = TreeNodeSelectAction.SelectExpand; // add + because there are contents in this folder
subNode.PopulateOnDemand = true;
subNode.NavigateUrl="#"; // set URL value of sub folder
}
}
catch
{
subNode.ImageUrl = "WebResource.axd?a=s&r=TreeView_XP_Explorer_ParentNode.gif&t=632242003305625000";
}
node.ChildNodes.Add(subNode);
} // end if not vti_cnf
} // end foreach sub directory

foreach (FileInfo fi in directory.GetFiles())
{
TreeNode subNode = new TreeNode(fi.Name);
node.ChildNodes.Add(subNode);
subNode.NavigateUrl = fi.FullName;
}

}

}
******************************* ASPX FILE **************************************************

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="job_descriptions.aspx.cs" Inherits="job_descriptions" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
<html xmlns=" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:TreeView ID="TreeView_jobdesc"
runat="server" ImageSet="xPFileExplorer"
AutoGenerateDataBindings="false"
ExpandDepth="0"
OnTreeNodePopulate="TreeView_jobdesc_TreeNodePopulate">
<SelectedNodeStyle BackColor="#B5B5B5"></SelectedNodeStyle>
<Nodes>
<asp:TreeNode Value="c:\humanres\jobdesc\" Text="Job Descriptions"
PopulateOnDemand="true" SelectAction="Select"
NavigateUrl="#" >
</asp:TreeNode>
</Nodes>
<NodeStyle VerticalPadding="2" Font-Names="Tahoma" Font-Size="8pt" HorizontalPadding="2" ForeColor="Black"></NodeStyle>
<HoverNodeStyle Font-Underline="True" ForeColor="#6666AA"></HoverNodeStyle>
</asp:TreeView>

</form>
</body>
</html>
----------------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top