Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 758
  • Last Modified:

Populate HMTL select list entries dynamically from folder lists

I need to populate an HTML Select list to show folder names.
I have found and would like to use this C# sample code on: http://msdn.microsoft.com/en-us/library/s7xk2b58.aspx.

public class GetDirectoriesTest
{
    public static void Main()
    {

        // Make a reference to a directory.
        DirectoryInfo di = new DirectoryInfo("c:\\");

        // Get a reference to each directory in that directory.
        DirectoryInfo[] diArr = di.GetDirectories();

        // Display the names of the directories.
        foreach (DirectoryInfo dri in diArr)
            Console.WriteLine(dri.Name);
    }
}



I need a way to call this function via script and pass it a folder path and have it return the list of desired folders.

Then I need a way to populate the list based on the directories returned.

as an example, the end result should look something like this:

<select id="Folders">
<option value="2011-2012" >2011-2012</option>
<option value="2010-2011" >2010-2011</option>
<option value="2009-2010" >2009-2010</option>
</select>
0
sleiman
Asked:
sleiman
  • 9
  • 8
1 Solution
 
RobOwner (Aidellio)Commented:
is this a web based app or just a console app creating an html file?

The following will spit out the directories in the c drive but changing that to a function is easy but I'd need to know how you are going to apply this eg web etc

public class GetDirectoriesTest 
{
    public static void Main() 
    {

        // Make a reference to a directory.
        DirectoryInfo di = new DirectoryInfo("c:\\");

        // Get a reference to each directory in that directory.
        DirectoryInfo[] diArr = di.GetDirectories();

        // Display the names of the directories.
        Console.WriteLine("<select id=\"Folders\">");
        foreach (DirectoryInfo dri in diArr) {
            Console.WriteLine("<option value=\"" + dri.Name + "\">" + dri.Name + "</option>" );
        }
        Console.WriteLine("</select>");
    }
}

Open in new window

0
 
sleimanAuthor Commented:
Web
0
 
RobOwner (Aidellio)Commented:
I would have a function that scanned a given directory and returned an array making it generic enough to be used for other things if required.
Then in your web app you would loop through the array and create the html list you require.
0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
RobOwner (Aidellio)Commented:
I'm not experienced with c# but this should get you started.

namespace MyService
{
    public class WebService1 : System.Web.Services.WebService
    {
        public WebService1()
        {
            InitializeComponent();
            Console.WriteLine(HTMLDirs("c:\"));
        }

        [WebMethod]
 public String HTMLDirs(String my_dir) 
    {
        // Make a reference to a directory.
        DirectoryInfo di = new DirectoryInfo(my_dir);

        // Get a reference to each directory in that directory.
        DirectoryInfo[] diArr = di.GetDirectories();

        // Display the names of the directories.
        String my_html = "<select id=\"Folders\">";

        foreach (DirectoryInfo dri in diArr) {
            my_html += "<option value=\"" + dri.Name + "\">" + dri.Name + "</option>";
        }
        my_html += "</select>";
        return my_html;
    }  // end HTMLDirs
    }  // end class
}

Open in new window

0
 
sleimanAuthor Commented:
Ok, Can you give me an example of a function that can call this routine by passing the my_dir as a string value and then displaying the results after control is returned.
0
 
sleimanAuthor Commented:
Also, how can I use or refernce the function result in my html section?
0
 
RobOwner (Aidellio)Commented:
Can you post your html section? or other code you've got ?

Essentially you include the class you've created above, instantiate it and then call the public function.
0
 
sleimanAuthor Commented:
here is what I have now and need to replace the Select list item with the result coming back from the funtion.


<body>
<legend><span class="enhance">Fiscal Year</span> <select id="rootFolder">
            <option value="2011-2012" style="font-family:tahoma;font-weight:bold;color:RED">2011-2012</option>
            <option value="2010-2011" style="font-family:tahoma;font-weight:bold;color:RED">2010-2011</option>
            <option value="2009-2010" style="font-family:tahoma;font-weight:bold;color:RED">2009-2010</option>
      </select>
<table>
....
</table>      
</body>
</html>
0
 
RobOwner (Aidellio)Commented:
sorry what I was after was the code that generates this html. ie the C# code.  That way we can manipulate that with the correct function
0
 
sleimanAuthor Commented:
Is this what you need?

[WebMethod]
///////////////////////////////////////////////////////////////
public static string GetFolderList(string APath)
///////////////////////////////////////////////////////////////
{

    DirectoryInfo di = new DirectoryInfo(APath);
      DirectoryInfo[] diArr = di.GetDirectories();

      ListDictionary folders = new ListDictionary();      

      string my_html = "<Select id=\"Folders\">";

      foreach (DirectoryInfo dri in diArr) {
            my_html += "<option value=\"" + dri.Name + "\">" + dri.Name + "</option>";
            }
      
   my_html += "</select>";
   return my_html;
}
0
 
sleimanAuthor Commented:
Hi there. the my_html is returning exactly what I need. all I need now is how to reference the my_html in the html code:



<body>
<legend><span class="enhance">Fiscal Year</span>  ?????????

<table>
....
</table>      
</body>
</html>
0
 
sleimanAuthor Commented:
your solution gave me enough to build the list items. I needed to use he document.write(...); to use the returned value in html. thanks for you help with this.
0
 
RobOwner (Aidellio)Commented:
no problem.  glad you got there in the end :)
0
 
sleimanAuthor Commented:
Just a question ck follow up. Is there a way to sort the list items inside the function?

In the example that gave 2011-2012 would need to be on the and be selected. I looked for some Java scripts but could not get them to work.
0
 
RobOwner (Aidellio)Commented:
Do you want to sort in the javascript or asp?

Using the FileSystemInfos object as well you can:

DirectoryInfo di = new DirectoryInfo("C:\\");
FileSystemInfo[] files = di.GetFileSystemInfos();
var orderedFiles = files.OrderBy(f => f.CreationTime);

Open in new window

0
 
sleimanAuthor Commented:
I only need to sort the folders so they can appear correctly in the drop down list.

Js or asp works.
0
 
RobOwner (Aidellio)Commented:
then i would do it server side.  did you try the code i posted above?
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now