Broodmdh
August 17th, 2005, 08:33 AM
I am trying to design an image gallery, and I am having some problems. Basically, all images are stored in subdirectories so I populate a dropdownlist with the names of the available directories. Once the user selects a directory (or in the case of the first post of the page, the first directory listed), the image names are loaded into an array and the first one is displayed. For some reason, when I select another directory from the dropdownlist, I don't get to load the image names from that directory (the image names from the originally selected dir remain). Also, I am using a session variable to track the position in the array of the currently displayed image. This variable is incremented/decremented when the user clicks the 'Next'/'Previous' buttons. Is there a better way to do this?
Protected WithEvents PlaceHolder1 As System.Web.UI.WebControls.PlaceHolder
Public WithEvents picBox As New System.Web.UI.WebControls.Image()
Public WithEvents cmdNext As New Button()
Public WithEvents cmdPrevious As New Button()
Public strImageFiles() As String = Directory.GetFiles("c:\inetpub\wwwroot\site2\Images")
Public strImageFiles() As String
Public WithEvents lstDir As New DropDownList()
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim lblDir As New Label()
lblDir.Text = "Gallery: "
lstDir.DataSource = Directory.GetDirectories("c:\inetpub\wwwroot\site2\Images", "*")
lstDir.DataBind()
lstDir.AutoPostBack = True
If Me.IsPostBack = False Then
Session("intImage") = 0
End If
PictureGallery()
End Sub
Private Sub PictureGallery()
Dim panBox As New Panel()
Dim lblInstruct As New Label()
Dim lblSpacer As New Label()
Dim lblSpacer2 As New Label()
Dim lblspacer3 As New Label()
strImageFiles = Directory.GetFiles(lstDir.SelectedItem.Text, "*.jpg")
If panBox.Controls.Count <> 0 Then
Dim c As Control
For Each c In panBox.Controls
panBox.Controls.Remove(c)
Next
End If
panBox.Controls.Clear()
lblSpacer.Text = "<br>"
lblSpacer2.Text = "<br>"
lblspacer3.Text = "<br>"
cmdNext.Text = "Next"
cmdNext.Width = Unit.Pixel(75)
cmdPrevious.Text = "Previous"
cmdPrevious.Width = Unit.Pixel(75)
picBox.ImageUrl = strImageFiles(Session("intImage"))
picBox.Height = Unit.Pixel(300)
picBox.Width = Unit.Pixel(740)
picBox.ImageAlign = ImageAlign.AbsMiddle
lblInstruct.Text = lstDir.SelectedItem.Text 'Verify selected value in dropdownlist
panBox.Width = Unit.Pixel(748)
panBox.Height = Unit.Pixel(325)
panBox.BorderStyle = BorderStyle.Ridge
panBox.HorizontalAlign = HorizontalAlign.Center
panBox.Controls.Add(lstDir)
panBox.Controls.Add(lblspacer3)
panBox.Controls.Add(picBox)
panBox.Controls.Add(lblSpacer)
panBox.Controls.Add(cmdPrevious)
panBox.Controls.Add(cmdNext)
panBox.Controls.Add(lblInstruct)
PlaceHolder1.Controls.Add(panBox)
End Sub
Private Sub lstDir_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstDir.SelectedIndexChanged
Session("intImage") = 0
End Sub
Private Sub cmdNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNext.Click
If Not Session("intImage") = UBound(strImageFiles) Then
Session("intImage") += 1
End If
End Sub
Private Sub cmdPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrevious.Click
If Not Session("intImage") = 0 Then
Session("intImage") -= 1
End If
End Sub
Protected WithEvents PlaceHolder1 As System.Web.UI.WebControls.PlaceHolder
Public WithEvents picBox As New System.Web.UI.WebControls.Image()
Public WithEvents cmdNext As New Button()
Public WithEvents cmdPrevious As New Button()
Public strImageFiles() As String = Directory.GetFiles("c:\inetpub\wwwroot\site2\Images")
Public strImageFiles() As String
Public WithEvents lstDir As New DropDownList()
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim lblDir As New Label()
lblDir.Text = "Gallery: "
lstDir.DataSource = Directory.GetDirectories("c:\inetpub\wwwroot\site2\Images", "*")
lstDir.DataBind()
lstDir.AutoPostBack = True
If Me.IsPostBack = False Then
Session("intImage") = 0
End If
PictureGallery()
End Sub
Private Sub PictureGallery()
Dim panBox As New Panel()
Dim lblInstruct As New Label()
Dim lblSpacer As New Label()
Dim lblSpacer2 As New Label()
Dim lblspacer3 As New Label()
strImageFiles = Directory.GetFiles(lstDir.SelectedItem.Text, "*.jpg")
If panBox.Controls.Count <> 0 Then
Dim c As Control
For Each c In panBox.Controls
panBox.Controls.Remove(c)
Next
End If
panBox.Controls.Clear()
lblSpacer.Text = "<br>"
lblSpacer2.Text = "<br>"
lblspacer3.Text = "<br>"
cmdNext.Text = "Next"
cmdNext.Width = Unit.Pixel(75)
cmdPrevious.Text = "Previous"
cmdPrevious.Width = Unit.Pixel(75)
picBox.ImageUrl = strImageFiles(Session("intImage"))
picBox.Height = Unit.Pixel(300)
picBox.Width = Unit.Pixel(740)
picBox.ImageAlign = ImageAlign.AbsMiddle
lblInstruct.Text = lstDir.SelectedItem.Text 'Verify selected value in dropdownlist
panBox.Width = Unit.Pixel(748)
panBox.Height = Unit.Pixel(325)
panBox.BorderStyle = BorderStyle.Ridge
panBox.HorizontalAlign = HorizontalAlign.Center
panBox.Controls.Add(lstDir)
panBox.Controls.Add(lblspacer3)
panBox.Controls.Add(picBox)
panBox.Controls.Add(lblSpacer)
panBox.Controls.Add(cmdPrevious)
panBox.Controls.Add(cmdNext)
panBox.Controls.Add(lblInstruct)
PlaceHolder1.Controls.Add(panBox)
End Sub
Private Sub lstDir_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstDir.SelectedIndexChanged
Session("intImage") = 0
End Sub
Private Sub cmdNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNext.Click
If Not Session("intImage") = UBound(strImageFiles) Then
Session("intImage") += 1
End If
End Sub
Private Sub cmdPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrevious.Click
If Not Session("intImage") = 0 Then
Session("intImage") -= 1
End If
End Sub