I am building a simple "file browser" page. It doesn't really do much yet - All I need it to do for now is browse up and down through the folders. The aspx page has a table with two cells - the left cell (cellFolders) holds the directory names, and the right cell (cellFiles) shows the file names. Easy-peasy.
So here is the problem. I load the page, and the files and folders show up as expected. I click on a folder name to browse that folder. A property called CurrentPath should have the folder added to it. For example, if the current path is "C:", and I click on a folder called "admin", then the current path should now be "C:\admin". Then everything should refresh and display.
What *is* happening is that I click on the "admin" folder, and the page reloads, but the CurrentPath doesn't change. If I click on "admin" a second time, then the page reloads, and the CurrentPath is now "C:\\admin".
I know this is some kind of issue in regards to the viewstate, or when the changes are getting picked up, but I just can't seem to get around it. Below is my code-behind. If anyone can clue me in, I'd appreciate it. - Thanks!
====================================
Imports System
Imports System.IO
Namespace DotNetNuke
Public Class FilePicker
Inherits DotNetNuke.BasePage
Public Property CurrentPath() As String
Get
Dim o As Object = Viewstate("CurrentPath")
If o Is Nothing Then
Return System.Web.HttpContext.Current.Request.PhysicalApplicationPath
'Viewstate("CurrentPath") = System.Web.HttpContext.Current.Request.PhysicalApplicationPath
Else
Return CStr(o)
End If
End Get
Set(ByVal Value As String)
Viewstate("CurrentPath") = Value
End Set
End Property
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents cellFolders As System.Web.UI.HtmlControls.HtmlTableCell
Protected WithEvents cellFiles As System.Web.UI.HtmlControls.HtmlTableCell
Protected WithEvents ImageButton1 As System.Web.UI.WebControls.ImageButton
Protected WithEvents LinkButton1 As System.Web.UI.WebControls.LinkButton
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ShowFolders(CurrentPath)
ShowFiles(CurrentPath)
End Sub
Private Sub ShowFolders(ByVal path)
'Clear the tree
cellFolders.Controls.Clear()
'Debug
Response.Write("Current Path: " & CurrentPath & "<br>")
Dim myImage1 As New ImageButton
'Dim myLink1 As New LinkButton
'Add an uplevel folder
myImage1.ImageUrl = "images/folderup.gif"
cellFolders.Controls.Add(myImage1)
myImage1.ImageAlign = ImageAlign.AbsMiddle
myImage1.ToolTip = "Go up one directory"
myImage1.CausesValidation = True
AddHandler myImage1.Click, AddressOf FolderUpImage_Click
cellFolders.Controls.Add(New LiteralControl("<br>"))
'List all the folders in the current path
Dim s() As String
s = System.IO.Directory.GetDirectories(path)
Dim en As System.Collections.IEnumerator
en = s.GetEnumerator
While en.MoveNext
Dim myImage As New ImageButton
Dim myLink As New LinkButton
myImage.ImageUrl = "images/folder.gif"
myImage.ImageAlign = ImageAlign.AbsMiddle
myImage.ToolTip = GetFileName(CStr(en.Current))
myImage.CausesValidation = True
AddHandler myImage.Click, AddressOf FolderImage_Click
myLink.Text = GetFileName(CStr(en.Current))
myLink.CssClass = "normal"
myLink.CausesValidation = True
AddHandler myLink.Click, AddressOf FolderLink_Click
cellFolders.Controls.Add(myImage)
cellFolders.Controls.Add(New LiteralControl(" "))
cellFolders.Controls.Add(myLink)
cellFolders.Controls.Add(New LiteralControl("<br>"))
End While
End Sub
Private Sub ShowFiles(ByVal path)
'Clear the tree
cellFiles.Controls.Clear()
'List all the files in the current path
Dim d() As String
d = System.IO.Directory.GetFiles(path)
Dim en As System.Collections.IEnumerator
en = d.GetEnumerator
While en.MoveNext
Dim myImage As New ImageButton
Dim myLink As New LinkButton
myImage.ImageUrl = "images/html.gif"
myImage.ImageAlign = ImageAlign.AbsMiddle
myLink.Text = GetFileName(CStr(en.Current))
myLink.CssClass = "normal"
cellFiles.Controls.Add(myImage)
cellFiles.Controls.Add(New LiteralControl(" "))
cellFiles.Controls.Add(myLink)
cellFiles.Controls.Add(New LiteralControl("<br>"))
End While
End Sub
Private Function GetFileName(ByVal value) As String
Return Microsoft.VisualBasic.Right(value, Len(value) - InStrRev(value, "\", , CompareMethod.Text))
End Function
Private Sub FolderImage_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
CurrentPath &= "\" & sender.tooltip
End Sub
Private Sub FolderLink_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
CurrentPath &= "\" & sender.text
End Sub
Private Sub FolderUpImage_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
CurrentPath &= Microsoft.VisualBasic.Left(CurrentPath, InStrRev(CurrentPath, "\", , CompareMethod.Text) - 1)
End Sub
End Class
End Namespace
-Todd Davis
http://www.SeaburyDesign.com