Question dropdownlist problem

budin

Member
Joined
Aug 25, 2011
Messages
12
Programming Experience
Beginner
hello,im have a problem, i hope somebody can help me..

this is the situation, i develop system, the client want me to create dropdownlist, 1st dropdownlist is the state, 2nd dropdownlist is a folder name..

example:

when you choose the 1st dropdown list, let say 'cat' then the 2nd dropdownlist show the folder exist in 'cat' that you choose before...how i want to show the folder in 2nd dropdownlist when you select 'cat'

please help me..
 
thanks for the suggestion, if i want the dropdownlist display all folder file in my C:\ i can apply this method also?im new in .net, then i develop the system in web form not windows form....
 
Sure, you can put pretty much anything you like into the dropdownlist.... the reason to use the CascadingDropdownList is to gain the functionality of one dropdown driving another - what you do with that driver is up to you.
 
do you have any sample dropdownlist that display folder in drive?im use thie code..but nothing happen...

Dim folderInfo As New IO.DirectoryInfo("c:\windows")
Dim arrFilesInFolder() As IO.FileInfo
Dim fileInFolder As IO.FileInfo


arrFilesInFolder = folderInfo.GetFiles("*.*")
For Each fileInFolder In arrFilesInFolder
DropDownList1.Items.Add(fileInFolder.Name)
Next

it just blank dropdown with no list...if u can help me, is really greatful
 
You'll need to create a ListItem from your array object, then add that to the DropDownList.
 
thanks for the reply..about the question

Two questions:
1) Do you want to display the folders on the server or those on the client?
ans:i want to display the folder on the server, so the client can see what folder have in server..
2) Where exactly did you put this code?
ans:i put that code in form load....


im really need help because my UAT is next week...
 
I myself would prefer to go the Ajax route.
Nevertheless, following example should demonstrate in plain no-ajax WebForms mode how to get the files displayed in the dropdown:

Note: the DropDownFolders dropdown has it's AutoPostback property = True, which is not necessarily a good idea and a pointer that an Ajax solution might be better.

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub DropDownFolders_SelectedIndexChanged(ByVal sender As Object,
    ByVal e As System.EventArgs) Handles DropDownFolders.SelectedIndexChanged
        GetFiles(DropDownFolders.SelectedValue)
    End Sub

    Protected Sub Page_Load(ByVal sender As Object,
    ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            GetFolders()
        End If
    End Sub

    Private Sub GetFolders()
        Dim folders = System.IO.Directory.GetDirectories("C:\").
            Select(Function(source As String) New IO.DirectoryInfo(source))
        DropDownFolders.DataSource = folders
        DropDownFolders.DataTextField = "Name"
        DropDownFolders.DataValueField = "FullName"
        DropDownFolders.DataBind()
    End Sub

    Private Sub GetFiles(ByVal folder As String)
        Dim current = New System.IO.DirectoryInfo(folder)
        DropDownFiles.DataSource = current.GetFiles
        DropDownFiles.DataBind()
    End Sub
End Class


Also note that you'll have to provide some errorhandling.
 
first of all, thanks for the code, i have try the code, but it nothing happen..just blank...about the datasource=folders, what is mean?
 
first of all, thanks for the code, i have try the code, but it nothing happen..just blank
Did you set the AutoPostBack property of the first dropdown to True?
If not, the DropDown.SelectedIndexChanged event does not fire.
Also be sure the selected folders has files, otherwise the second DropDownList stays empty.
Did you set a breakpoint in both event handlers to verify if they are executing and what the results are if they executed?

...about the datasource=folders, what is mean?
That means that in the DataBind method, the control will get it's info from the datasource folders wich is an IEnumerable( Of DirectoryInfo) when building and adding the ListItems for the control, automagically.
 
the 1st dropdownlist is done..thank you very much...but the 2nd dropdownlist not have list..i click the item on the 1st dropdownlist, but nothing happen on 2nd dropdownlist...i already set the autopostback property to true at 1st dropdownlist.

thanks for the info about the datasource=folders.. i learn a lot from you Don.
 
i click the item on the 1st dropdownlist, but nothing happen on 2nd dropdownlist...i already set the autopostback property to true at 1st dropdownlist
What happens if you set a breakpoint in the event handler for the first dropdown?
Does the eventhandler execute?
If it does you probably selected a folder with no files in it.

Can you show your relevant code and relevant markup?
 
don, its work...thanks for helping me...i have a question, the code u give me make the 2nd dropdownlist list the file in the folder, how about the 2nd dropdownlist, i want display sub folder?

for example,

in server have folder name car, then car folder have sub folder name color...how i want to display the sub folder name in 2nd dropdownlist when user select car in 1st dropdownlist?
 
Back
Top