Cascading Listbox problem

Blake81

Well-known member
Joined
Feb 23, 2006
Messages
304
Location
Georgia, USA
Programming Experience
1-3
I'm making an ASP page for myself so I can listen to the music on my hard drive when I'm away from home. I have two listboxes and two buttons. When the page loads, the first listbox displays all of the subfolders of the directory where my music is stored. I'm trying to get the second listbox to show the subfolders of whatever folder I select in the first listbox whenever I select it. I also want the second listbox to show MP3 files as well if they exist in that folder instead of subfolders. This is being really strange, because it won't show the subfolders in the second listbox when I select one from the first, but it will show subfolders when I click one of the buttons, and I haven't even put any code behind the buttons yet. Can someone help me get this to do what I want? I'd appreciate it. Here's my code.
VB.NET:
[SIZE=2][COLOR=#0000ff]Partial[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] _Default
[/SIZE][SIZE=2][COLOR=#0000ff]Inherits[/COLOR][/SIZE][SIZE=2] System.Web.UI.Page
[/SIZE][SIZE=2][COLOR=#0000ff]Protected[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Page_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Load
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] mymusic [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] IO.DirectoryInfo([/SIZE][SIZE=2][COLOR=#800000]"L:\Music and Stuff\My Music"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] x [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] x = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] FileIO.FileSystem.GetFiles([/SIZE][SIZE=2][COLOR=#800000]"L:\Music and Stuff\My Music"[/COLOR][/SIZE][SIZE=2]).Count - 1
ListBox1.Items.Add(FileIO.FileSystem.GetFiles([/SIZE][SIZE=2][COLOR=#800000]"L:\Music and Stuff\My Music"[/COLOR][/SIZE][SIZE=2]).Item(x))
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] y [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] y = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] FileIO.FileSystem.GetDirectories([/SIZE][SIZE=2][COLOR=#800000]"L:\Music and Stuff\My Music"[/COLOR][/SIZE][SIZE=2]).Count - 1
ListBox1.Items.Add(FileIO.FileSystem.GetDirectories([/SIZE][SIZE=2][COLOR=#800000]"L:\Music and Stuff\My Music"[/COLOR][/SIZE][SIZE=2]).Item(y))
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Protected[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] ListBox1_SelectedIndexChanged([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] ListBox1.SelectedIndexChanged
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] z [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] z = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] FileIO.FileSystem.GetFiles(ListBox1.SelectedValue).Count - 1
ListBox2.Items.Add(FileIO.FileSystem.GetFiles(ListBox1.SelectedValue).Item(z))
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ztwo [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] ztwo = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] FileIO.FileSystem.GetDirectories(ListBox1.SelectedValue).Count - 1
ListBox2.Items.Add(FileIO.FileSystem.GetDirectories(ListBox1.SelectedValue).Item(ztwo))
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE]
 
I think ashaishnaicker means set the autopostback property of your listbox to true.
Listboxes do not by default cause a postback when the selected index is changed therefore nothing much will happen when you select something. Buttons on the other hand do cause a postback by default even if there is no event handler for them. So when you select something then nothing happens but when you then click a button it does. After you set the autopostback property of the listbox to true then the second listbox will be filled by your code without having to press any buttons.
 
If you haven't heard of Atlas you should check it out: ASP.NET "Atlas"
“Atlas” makes it possible to easily take advantage of AJAX (Asynchronous JavaScript and XML) techniques on the web and enables you to create ASP.NET pages with a rich, responsive UI and server communication.

One of the controls “Atlas” offers is the CascadingDropDown which offers the functionality you desire. Check out the demonstration here: CascadingDropDown Demonstration
 
Also, have you though about using a treeview to show your hierarchy of folders?

This would be more user friendly, since you are most probably already used to navigating folders in this way
 
Back
Top