Click here to Skip to main content
Licence CPOL
First Posted 5 Dec 2007
Views 44,548
Bookmarked 43 times

Directory Browsing in ASP.Net 2.0

By | 15 Apr 2008 | Article
An article on browsing via directories or HTTP, with ASP.NET using Tree View Control
Download Browsing.zip - 18.72 KB
Screenshot - main.JPG

Introduction

The file uploder is the control which help to select the file but not the path of the file or directory and also we can't make a Active X Control for this so with the help of pop-up we provide the user to select file or directory path in the intranet. This pop-up look same like the directory browing in desktop application. 

Background

I read this article http://www.codeproject.com/KB/aspnet/Browsing.aspx which is in ASP.net 1.1. I have changed the folder structure with tree view.

Using the code

Create a C# web application in asp.net 2.0 and add BrowseDirectory.aspx and add following files into your app_code folder

  • WebForm1_aspx_cs_FileList.cs
  • WebForm1_aspx_cs_PathHelper.cs

and create a textbox and button on which webform you want to use it. Add onClientClick of the button and set OnClientClick="showDirectory();" . Add this script into yout web form for calling the browsing page.

function showDirectory()
             {
                document.all.TextBox1.value= window.showModalDialog("browseDirectory.aspx",
                            'jain',
                            "dialogHeight: 560px; dialogWidth:360px; edge: Raised; center: Yes; help: Yes; resizable: Yes; status: No;"); 
                return false;
              }
           

 

 

Explanation of code blocks

Add Node method in the BrowseDirectory.aspx which add the tree node of the directory and files. FileList Constructor takes path and the filters for the files

 
            FileList objList = new FileList(path, "*.*");
            TreeNode node = new TreeNode(path,path);
            for(int index = 0 ; index < objList.Directories.Length ; index++)
            {
                string directory = objList.Directories[index];            
                TreeNode objChildNode = new TreeNode(directory, path + "\\" + directory + "\\"); 
                objChildNode.PopulateOnDemand = true;
                objChildNode.Target = "_blank";
                
                parentNode.ChildNodes.Add(objChildNode);
            }
            foreach (string file in objList.Files)
            {
                TreeNode objChildNode = new TreeNode(file, path + "\\" + file);
                parentNode.ChildNodes.Add(objChildNode);
            }
            return node;
            

For selecting the path of the file or directory use the selection event of the tree view

            void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
            {
                _browseTextBox.Text = TreeView1.SelectedValue;
            }
            

Expanding is base on the directory selection.

             void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
            {
                if (e.Node.Value.EndsWith("\\"))
                {
                    AddNodes(e.Node.Value, e.Node); 
                }
               

            }
            

Finally when you select the path then go back to the parent page with this path

            <script language="javascript">
                <!--
                function SelectAndClose()
		            {
		                txtValue = document.getElementById('_browseTextBox');
            		    
		                window.returnValue = txtValue.value;
		                window.close();
		                return false;
		            }
            		
            		
                -->
            </script>
            

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Rupesh Burad

Team Leader
MPS Technologies Limited
India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Layout  Per page   
  Refresh
QuestionDirectory Browsing Pinmemberanandkumar01880:55 22 May '12  
QuestionVB source Pinmemberhighglow6:26 23 Apr '12  
QuestionNot able to browse network drive Pinmemberajitsingh4172:09 11 Jan '12  
QuestionUn Know in Fire Fox PinmemberPhan Van Thao17:06 25 Sep '11  
GeneralQuestion [modified] PinmemberAtul Naik2:03 21 Sep '11  
GeneralDirectory browser Pinmembersrinivas prabhu1:37 20 Aug '10  
GeneralRe: Directory browser PinmemberRupesh Burad18:48 22 Aug '10  
GeneralRe: Directory browser Pinmemberselllllllllllllll1:50 8 Sep '11  
GeneralMy vote of 1 Pinmemberzdlik12:36 5 Feb '10  
QuestionThanks....? PinmemberMember 412614921:37 13 Nov '09  
GeneralDirectory Browsing in ASP.Net 2.0 Pinmemberab_sdfdsfsdf7:31 20 Oct '09  
GeneralHi! Pinmemberweiiis3:42 11 Aug '09  
GeneralThanks PinmemberIntellect19:32 22 Mar '09  
GeneralMy vote of 1 Pinmemberjoshuaemory5:21 31 Dec '08  
QuestionSource code Pinmemberclbmribas9:59 12 Jul '08  
QuestionNeed Source PinmemberJeff Bowman18:10 27 Dec '07  
GeneralRe: Need Source PinmemberRupesh Burad23:27 15 Apr '08  
Uploaded, Let me know if any issue
GeneralRe: Need Source PinmemberJeff Bowman15:53 19 Apr '08  
QuestionSourcecode Pinmemberknoami6:50 12 Dec '07  
GeneralRe: Sourcecode PinmemberRupesh Burad23:27 15 Apr '08  
GeneralRe: Sourcecode Pinmemberknoami3:50 16 Apr '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 16 Apr 2008
Article Copyright 2007 by Rupesh Burad
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid