

Navigator : Home > Tutorials > Controls Tutorials > ...
TreeView without page refresh in ASP.NET 2.0 and C#
This tutorial will show you how to work with TreeView control to display folders and file names without refreshing web page by C# and ASP.NET 2.0.
This tutorial will show you how to work with TreeView control to display folders and file names without refreshing web page by C# and ASP.NET 2.0.
First, you need to import the namespace from System.IO.DirectoryInfo in the namespace System.IO.DirectoryInfo class for typical operations such as copying, moving, renaming, creating, and deleting directories.
Try Server Intellect for Windows Server Hosting. Quality and Quantity!
We will use the Treeview control to display the files names and folders without refreshing web page. The TreeView is an ASP.NET server control for authoring user interfaces representing hierarchical data. In the sample, we will use the events of Treeview1_TreeNodePopulate and LoadChildNode to show you how it works. Treeview1_TreeNodePopulate is used for displaying the folders and files in drive C. LoadChildNode is used for loading the name of folder and file. The code as below:
protected void Treeview1_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()) {
TreeNode subNode = new TreeNode(sub.Name); subNode.Value = sub.FullName; try {
if (sub.GetDirectories().Length > 0 || sub.GetFiles().Length > 0) {
subNode.SelectAction = TreeNodeSelectAction.SelectExpand; subNode.PopulateOnDemand = true; subNode.NavigateUrl = "#"; } } catch { } node.ChildNodes.Add(subNode); } foreach (FileInfo fi in directory.GetFiles()) {
TreeNode subNode = new TreeNode(fi.Name); node.ChildNodes.Add(subNode); } } |
The front end TreeViewCharp2005.aspx page looks something like this:
<asp:treeview ID="Treeview1" runat="server" ImageSet="XPFileExplorer" AutoGenerateDataBindings="false" ExpandDepth=0 OnTreeNodePopulate="Treeview1_TreeNodePopulate" > <SelectedNodeStyle BackColor="#B5B5B5"></SelectedNodeStyle> <Nodes><asp:TreeNode Value="C:" Text="C:" 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> |
We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!
The flow for the code behind page is as follows
using System; using System.Data; using System.Configuration; 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 _Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) { } protected void Treeview1_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()) {
TreeNode subNode = new TreeNode(sub.Name); subNode.Value = sub.FullName; try {
if (sub.GetDirectories().Length > 0 || sub.GetFiles().Length > 0) {
subNode.SelectAction = TreeNodeSelectAction.SelectExpand; subNode.PopulateOnDemand = true; subNode.NavigateUrl = "#"; } } catch { } node.ChildNodes.Add(subNode); } foreach (FileInfo fi in directory.GetFiles()) {
TreeNode subNode = new TreeNode(fi.Name); node.ChildNodes.Add(subNode); } } } |
Looking for the VB.NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!