CodeVerge.Net Beta


   Explore    Item Entry   Register  Login  
Microsoft News
Asp.Net Forums
IBM Software
Borland Forums
Adobe Forums
Novell Forums

ASP.NET Web Hosting – 3 Months Free!



Zone: > NEWSGROUP > Asp.Net Forum > general_asp.net.master_pages_themes_and_navigation_controls Tags:
Item Type: NewsGroup Date Entered: 2/18/2008 12:38:53 PM Date Modified: Subscribers: 0 Subscribe Alert
Rate It:
(NR, 0)
XPoints: N/A Replies: 15 Views: 73 Favorited: 0 Favorite
Can Reply:  No Members Can Edit: No Online: Yes
16 Items, 1 Pages 1 |< << Go >> >|
OwainGDWilliams
Asp.Net User
Treeview - Listing folder content.2/18/2008 12:38:53 PM

0/0

Hi!

I had a quick look around the forum but couldnt find anything that answered my question.

I have a folder full of word documents and PDFs, what I want to do is use the Treeview to list the contents of this folder.

The site I want to implement this on is in C#. Could someone point me in the correct direction for doing this. All I want is a list of the documents and when you click on the document it opens.

 Thanks,

Owain.

Curt_C
Asp.Net User
Re: Treeview - Listing folder content.2/18/2008 1:32:37 PM

0/0

you can use the System.IO namespace to get a list of folder contents. From there you just need to iterate the collection and manually add each item to the treeview, or bind the treeview to your collection.


Curt Christianson
Chief Architect, df-Software.com
df-Software.com | StarterKits.org | Darkfalz.com | WebHost4Life | Microsoft MVP
OwainGDWilliams
Asp.Net User
Re: Treeview - Listing folder content.2/18/2008 1:41:18 PM

0/0

Sorry I should have said, Ive only just come off a .net2 course so still very new to all of this.

Any tutorials or walk throughs I could follow?

O.

Curt_C
Asp.Net User
Re: Treeview - Listing folder content.2/18/2008 4:42:59 PM

0/0

I dont know of any specific samples but just give it a whirl, post the specifics of where you get stuck and we'll walk you through.

Google the System.IO.File/Folder stuff, you'll get samples on that. Iterate the collection (for each loop) and just write the results to the page. Then work on getting the results into a treeview.


Curt Christianson
Chief Architect, df-Software.com
df-Software.com | StarterKits.org | Darkfalz.com | WebHost4Life | Microsoft MVP
OwainGDWilliams
Asp.Net User
Re: Treeview - Listing folder content.2/18/2008 8:27:40 PM

0/0

 Thanks, I will give it a go in the morning and see where I get :)


O.

Amanda Wang - M
Asp.Net User
Re: Treeview - Listing folder content.2/20/2008 2:58:26 AM

0/0

Hi,

Try to refer the below sample code, you should add the namespace using System.IO;:

 private const string  VirtualImageRoot = @"D:\public";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PopulateTree();
        }
    }

    private void PopulateTree()
    {
        //Populate the tree based on the subfolders of the specified VirtualImageRoot
        //Dim rootFolder As New DirectoryInfo(Server.MapPath(VirtualImageRoot))

        DirectoryInfo rootFolder = new DirectoryInfo(VirtualImageRoot);
        TreeNode root = AddNodeAndDescendents(rootFolder, null);

        //Add the root to the TreeView
        DirectoryTree.Nodes.Add(root);
        if (Page.IsClientScriptBlockRegistered("initMenu('DirectoryTree')"))
        {}
    }


    private TreeNode AddNodeAndDescendents(DirectoryInfo folder, TreeNode parentNode)
    {
        //Add the TreeNode, displaying the folder's name and storing the full path to the folder as the value...
        string virtualFolderPath;

        if (parentNode == null)
        {
            virtualFolderPath = VirtualImageRoot;
        }
        else
        {
            virtualFolderPath = parentNode.Value + folder.Name + "/";
        }

        TreeNode node = new TreeNode(folder.Name, virtualFolderPath);

        //Recurse through this folder's subfolders
        DirectoryInfo[] subFolders = folder.GetDirectories();

        foreach (DirectoryInfo subFolder in subFolders)
        {
            TreeNode child = AddNodeAndDescendents(subFolder, node);
            node.ChildNodes.Add(child);
        }
        //Return the new TreeNode
        return node;
    }

 

Hope it helps.


Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Yours sincerely,
Amanda Wang
Microsoft Online Community Support
OwainGDWilliams
Asp.Net User
Re: Treeview - Listing folder content.2/20/2008 9:00:36 AM

0/0

That is great, thanks for that. I will give it a go today and if I need any further help I will post back.

 I won't mark it as answered if that OK just now, but hopefully I will have it doing what I need by the end of the day.

 O.

OwainGDWilliams
Asp.Net User
Re: Treeview - Listing folder content.2/20/2008 11:01:41 AM

0/0

 Ive been reading about namespaces on a couple of sites and I just can't seem to get my head around it.

 

should I do

 using System.IO;

 namespace myNameSpace

{

Your code

}

 

Or am I missing the point of namespaces here?

O.

 

Curt_C
Asp.Net User
Re: Treeview - Listing folder content.2/20/2008 1:30:14 PM

0/0

namespaces are just a way of grouping functionality.

you dont need the extra myNamespace in the sample he provided


Curt Christianson
Chief Architect, df-Software.com
df-Software.com | StarterKits.org | Darkfalz.com | WebHost4Life | Microsoft MVP
OwainGDWilliams
Asp.Net User
Re: Treeview - Listing folder content.2/27/2008 11:34:51 AM

0/0



Ok, I have put the code in and I have got a Treeview Component on my page but I am now getting the following error and I can't work this one out.

 

Compiler Error Message: CS0117: 'ASP.manualword_aspx' does not contain a definition for 'TreeView1_SelectedNodeChanged'

Source Error:

Line 23: 	
Line 24:      
Line 25:         <asp:TreeView ID="DirectoryTree" runat="server" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged">
Line 26:     </asp:TreeView>
Line 27: <p>

 

Can anyone help please.
O.

 
Amanda Wang - M
Asp.Net User
Re: Treeview - Listing folder content.2/28/2008 2:00:52 AM

0/0

hi,

Two ways to solve it:

1. delete OnSelectedNodeChanged="TreeView1_SelectedNodeChanged" from the aspx code.

2. add the TreeView1_SelectedNodeChanged even in the codebehind.

Hope it helps.


Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Yours sincerely,
Amanda Wang
Microsoft Online Community Support
OwainGDWilliams
Asp.Net User
Re: Treeview - Listing folder content.2/28/2008 9:42:42 AM

0/0

Thanks!

 One more question, when the files are displayed is there a way to open them? Just now when I click on the file it doesnt do anything, ideally I would want to open the files and read them.....thanks for your continued help!

 

Ive just found another problem and it may be a setting I am missing somewhere. If I point the tree to the E:\MyFolder\ then it lists all of the files but only allows you to expand some folders, not all of them.

If I point the tree to E:\MyFolder\AFolder\ the files within this folder are not displayed at all.....Im very confused why this happenes.


O.

OwainGDWilliams
Asp.Net User
Re: Treeview - Listing folder content.2/28/2008 3:01:45 PM

0/0

What I have found out is it only shows folders, it doesnt show a .txt file or a .doc file if its in the folder. Is there a way of doing this?

I think I need to use the folder.GetFiles but Im not sure how to use this yet, I will dig around and see what I can find.

Sorry for all the questions.


O.

Amanda Wang - M
Asp.Net User
Re: Treeview - Listing folder content.2/29/2008 2:54:37 AM

0/0

Hi,

If you want to get the folder's files, you need to add some code in the AddNodeAndDescendents function, the border characters is the added code:

private TreeNode AddNodeAndDescendents(DirectoryInfo folder, TreeNode parentNode)
    {
        //Add the TreeNode, displaying the folder's name and storing the full path to the folder as the value...
        string virtualFolderPath;

        if (parentNode == null)
        {
            virtualFolderPath = VirtualImageRoot;
        }
        else
        {
            virtualFolderPath = parentNode.Value + folder.Name + "/";
        }

        TreeNode node = new TreeNode(folder.Name, virtualFolderPath);

        //Recurse through this folder's subfolders
        DirectoryInfo[] subFolders = folder.GetDirectories();

        foreach (DirectoryInfo subFolder in subFolders)
        {
            TreeNode child = AddNodeAndDescendents(subFolder, node);

            foreach (FileInfo file in subFolder.GetFiles())
            {
                int index = file.FullName.LastIndexOf("\\");
                string strname = file.FullName.Substring(index + 1);
                string[] name = strname.Split('.');

                TreeNode tn = new TreeNode(name[0], file.FullName);
                child.ChildNodes.Add(tn);
            }
            node.ChildNodes.Add(child);
          
        }
        //Return the new TreeNode
        return node;
    }

Then you can open the file in the treeview's SelectedNodeChanged event:

    protected void DirectoryTree_SelectedNodeChanged(object sender, EventArgs e)
    {
        System.Diagnostics.Process.Start(this.DirectoryTree.SelectedNode.Value);
    }

 

Hope it helps.

 


Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Yours sincerely,
Amanda Wang
Microsoft Online Community Support
OwainGDWilliams
Asp.Net User
Re: Treeview - Listing folder content.3/3/2008 9:45:35 AM

0/0

Thanks, all the files are now displayed but when I click on the link to try and open it the screen refreshes but nothing is opened :( I don't really know what the code is doing so trying to troubleshoot it is a bit hard.

O.

OwainGDWilliams
Asp.Net User
Re: Treeview - Listing folder content.3/4/2008 3:20:19 PM

0/0

Ignore this post, I am going to try and display the data in a grid view, hopefully with some help from the forum users :)

 

O.

16 Items, 1 Pages 1 |< << Go >> >|


Free Download:

Books:
Harnessing AutoCAD 2002 Authors: Thomas A. Stellman, G. V. Krishnan, Pages: 1189, Published: 2001
Pro .NET 2.0 Windows Forms and Custom Controls in C#: From Professional to Expert Authors: Matthew MacDonald, Pages: 1037, Published: 2005
Pro .NET 2.0 Windows Forms and Custom Controls in VB 2005 Authors: Matthew MacDonald, Pages: 1036, Published: 2006
Professional ASP.NET 2.0 Authors: Bill Evjen, Scott Hanselman, Farhan Muhammad, Srinivasa Sivakumar, Devin Rader, Pages: 1253, Published: 2005
Red Hat Enterprise Linux 4 For Dummies Authors: Terry Collings, Pages: 408, Published: 2005
Professional C# Authors: Simon Robinson, Christian Nagel, Karli Watson, Jay Glynn, Morgan Skinner, Bill Evjen, Pages: 1224, Published: 2004
Harnessing AutoCAD 2006 Authors: Thomas A. Stellman, G. V. Krishnan, Pages: 1187, Published: 2005
Professional C# 2005 with .NET 3.0 Authors: Christian Nagel, Bill Evjen, Jay Glynn, Karli Watson, Morgan Skinner, Pages: 1748, Published: 2007
Adobe Photoshop CS3 for Photographers: A Professional Image Editor's Guide to the Creative Use of Photoshop for the Macintosh and PC Authors: Martin Evening, Pages: 704, Published: 2007
Professional C# 2005 Authors: Christian Nagel, Bill Evjen, Jay Glynn, Karli Watson, Morgan Skinner, Allen Jones, Pages: 1540, Published: 2006

Web:
ASP.NET.4GuysFromRolla.com: Using the TreeView Control and a ... Aug 30, 2006 ... A TreeView control that lists the folders rooted at some specified folder, and; A data Web control to list the files in the selected folder. ...
Treeview - Listing folder content. - ASP.NET Forums Re: Treeview - Listing folder content. 03-04-2008, 10:20 AM. Contact ... Re: Treeview - Listing folder content. 04-28-2008, 3:39 PM. Contact ...
Treeview Files/Folder Lists Recursively - developerFusion - the ... Can you help me how if I want to group the folders and then only list the files? ... A perfect example of how to fill a treeview control with a directory ...
Folder/File Tree View Sep 15, 2007 ... FolderObject: The Folder object whose contents we ' are going to list. The TreeView element for ' FolderObject has already been added to the ...
CodeProject: Tree View Control Files & Folders List - Pocket PC ... Jun 28, 2007 ... List all the files & folder present in your PocketPc in a Tree View Control.; Author: Prasannavig; Section: Miscellaneous; Chapter: Desktop ...
How to Save or Print a Directory Listing in Windows Treeview list, Treeview list with file size. List folder names only. ... Perfect to print out CD or DVD contents, supports files over Gbs. ...
File list tree view with folders and tasks as folders with ... File list tree view with folders and tasks as folders with uploaded files / # 2935 ... What about change Files list view to shows tree structure? ...
CodeGuru: Treeview Control Discover an implementation of a tree view, combined with a list view, ... Very popular file/folder treeview that can be used on either a dialog or a view ...
Tree View of Files, Not the control! [Archive] - Xtreme Visual ... i really don't mind using tree view, but i need to take the list and send it via winsock to ... if i want to list all folders/files in: ...
SysExporter: Grab data from list-view, tree-view, combo box ... The files list inside a folder. The event log of Windows. ... SysExporter can export data from most combo boxes, list boxes, tree-view, and list-view ...

Videos:
Structuring Personal Information When Everything Can Be Saved and Searched... Google TechTalks May 17, 2006 William Jones William Jones is an Research Associate Professor in The Information School at the University of Washingt...




Search This Site:










enter key postback in asp.net 2.0

autopostback doesn't work inside <asp:content>?

web page looks great in ie 7 (no supprise) but is meesed up in firefox.

treeview - remove tooltip

copying a treeview probem

menu control : cross page posting and posted values in nested controls?

skins not showing on my page - project similar to personal website.

has anyone solved the font color bug with the menu control?

treeview data binding problem

how to turn off starting node in site map path?

using master page controls within the code of the content page

problem with menu control and sitemap

no global themes directory

custom control's events in master page

masterpages to chrome user controls?

jscript compatablity in firefox

formating menu items

recommendations for navbar dynamic menus

masterpage and menuitem navigateurl

problem with a content page's page_load subroutine

how to dynamically update and expand/focus a node in treeview asp.net 2.0 ?

content showing up in wrong place

master page and default button problem

to create a dynamic menu or treeview

basic web.sitemap output

help with sitemapdatasource1.sitemapprovider

themes

help needed with a menu

(html) meta tag format puzzle

menuitem and imageurl problem

  Privacy | Contact Us
All Times Are GMT