Click to See Complete Forum and Search --> : Dropdownlist Problems


Broodmdh
August 19th, 2005, 10:47 AM
I am populating a dropdownlist with a listing of subdirectories. When the user selects a dir from the list, I want the page to refresh. When the page is created (or refreshed), action will be taken based on the contents of the selected dir (or default dir if not post back). I am finding, however, that even when a user makes a selection from the dropdownlist, this change is not reflected upon postback. My code is as follows:


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
strDir = Directory.GetDirectories("c:\inetpub\wwwroot\site2\Images", "*")
lstDir.DataSource = strDir
lstDir.DataBind()
lstDir.AutoPostBack = True
PictureGallery()
End Sub

Private Sub PictureGallery()
strImageFiles = Directory.GetFiles(lstDir.SelectedIndex.Text, "*.jpg")

cmdNext.Text = "Next"
cmdNext.Width = Unit.Pixel(75)
cmdPrevious.Text = "Previous"
cmdPrevious.Width = Unit.Pixel(75)

lblInstruct.Text = lstDir.SelectedIndex 'This always outputs 0 regardless of which entry is selected in the list
End Sub


Any thoughts?

cmiskow
August 21st, 2005, 09:19 AM
The problem is that evey time the page loads (including postbacks), you are refreshing the dropdown's items, and clearing their selected state. You only want to bind the dropdown on the first page load, so change your Page_Load to this:


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostback Then
strDir = Directory.GetDirectories("c:\inetpub\wwwroot\site2\Images", "*")
lstDir.DataSource = strDir
lstDir.DataBind()
lstDir.AutoPostBack = True
End If
PictureGallery()
End Sub

Broodmdh
August 22nd, 2005, 01:34 PM
I made the changes that you suggested, but now whenever the user selects an entry from the dropdownlist (there are only 2 right now) other than the default (0), the selectedindex returns -1. Why would this be?

aquafin
September 8th, 2005, 04:09 AM
strImageFiles = Directory.GetFiles(lstDir.SelectedIndex.Text, "*.jpg")



when you place the code within the if loop , it should work,

But in the code does the selectedIndex have the text property?
I have some smiliar code , so just want to know if it is valid.