Solved

ASP class object: call a sub from a  hyperlink

Posted on 2004-04-22
3
426 Views
Last Modified: 2008-03-06
I am experimenting with Class objects in classic ASP/vbscript on my local machine testing server.

I have a Sub within a Class that lists subfolders in a directory.
It is rather simple. I use a "For Each" to iterate throught the directory attaching a hyperlink to each folder listing that should list the files within the subfolder when clicked.
This works fine (and I especially like the fact that it reveals no path information in the address bar), but when I click the hyperlink it shows the directory contents in internet explorer revealing the exact path information.

What I would like to do is call another sub which lists files within the subfolder when the hyperlink is clicked without changing pages and without revealing path information.

can this be done?
If so, how?

Here is the code:

<%
Class clsFman

Public Sub Class_Initialize()
Call lstFoldrs
End Sub

Private Sub lstFoldrs
    Dim oFSO, oFolder, curFolder, oSubFolder, oFile
    Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
      curFolder = Server.MapPath("../../path")
      Set oFolder = oFSO.GetFolder(curFolder)
        
            Response.Write("<table width=480px align=center border=0 cellpadding=0  cellspacing=2>")
            Response.Write("<tr bgcolor=#FFCC00 height=20>")
            Response.Write("<td align=center width=250><b><font size=1 face=Tahoma>FOLDER</font></b></td>")
            Response.Write("<td align=center width=100><b><font size=1 face=Tahoma>CLIENT NAME</font></b></td>")
            Response.Write("<td align=center width=100><b><font size=1 face=Tahoma>CREATED ON</font></b></td>")
            Response.Write("<td align=center width=75><b><font size=1 face=Tahoma>PROJECT</font></b></td>")
            Response.Write("<td align=center width=35><font size=1 face=Tahoma>delete</font></td>")
            Response.Write("</tr>")

      For Each oSubFolder in oFolder.SubFolders
            Response.Write("<tr bgcolor=#F8F8F8>")
            Response.Write("<td height=18 align=left valign=middle><b><font face=Tahoma color=666666>&nbsp;<a href=" & oFolder &"/" & oSubFolder.Name &"/>" & oSubFolder.Name &"</a></font></b></td>")
            Response.Write("<td></td>")
            Response.Write("<td></td>")
            Response.Write("<td></td>")
            Response.Write("<td></td>")
            Response.Write("</tr>")
            
      Next

            Response.Write("</table>")

End Sub

Private Sub lstFiles
    Dim oFSO, oFolder, curFolder, oSubFolder, oFile
    Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
      curFolder = Server.MapPath("../../path")
      Set oFolder = oFSO.GetFolder(curFolder)
        
            Response.Write("<table width=480px align=center border=0 cellpadding=0  cellspacing=2>")
            Response.Write("<tr bgcolor=#FFCC00 height=20>")
            Response.Write("<td align=center width=250><b><font size=1 face=Tahoma>FOLDER</font></b></td>")
            Response.Write("<td align=center width=100><b><font size=1 face=Tahoma>CLIENT NAME</font></b></td>")
            Response.Write("<td align=center width=100><b><font size=1 face=Tahoma>CREATED ON</font></b></td>")
            Response.Write("<td align=center width=75><b><font size=1 face=Tahoma>PROJECT</font></b></td>")
            Response.Write("<td align=center width=35><font size=1 face=Tahoma>delete</font></td>")
            Response.Write("</tr>")


      For Each oFile in oFolder.Files
Dim X
X = X + 1
If X Mod 2 then
            Response.Write("<tr bgcolor=#E4E4E4>")
        Else
          Response.Write("<tr bgcolor=#F8F8F8>")
        End If
            Response.Write("<td height=18 width=250 align=left valign=middle><font face=Tahoma>&nbsp;<a href='" & oSubFolder &"' & '" & oFile.Name & "'>" & oFile.Name &"</a></font></td>")
            Response.Write("<td width=75 align=left valign=middle><font face=Tahoma>&nbsp;</font></td>")
            Response.Write("<td width=100 align=left valign=middle><font face=Tahoma>&nbsp;" & FormatDateTime(oFile.DateCreated, 2) &"</font></td>")
            Response.Write("<td width=75 align=left valign=middle><font face=Tahoma>&nbsp;</font></td>")
            Response.Write("<td width=35 align=center valign=middle><b><font face=Tahoma>X</font></b></td>")
            Response.Write("</tr>")

      Next

            Response.Write("</table>")
      
End Sub

Public Sub Class_Terminate()
End Sub
End Class
%>


Thanks in Advance
0
Comment
Question by:bodanon
  • 2
3 Comments
 
LVL 28

Accepted Solution

by:
sybe earned 500 total points
ID: 10897076
some remarks:

- you use none of the advantages of a class. You just have a Sub in a Class which you could  have as a plain Sub too.
- in your case you have the path to the directory depend on the position of the file in the webdirectory (using Server.MapPath). Better use a parameter for that, that will also solve the issue you want to achieve.
Don't use Server.MapPath in Classes, because it makes its functioning dependent on the position of the calling file in the web-directory. You want a Class to be usable from anywhere.


For example use Properties to set the directory
<%
Class Example
    Private sDirectory, oFSO

    Public Sub Class_Initialize()
        Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
    End Sub

    Private Sub Class_Terminate
        Set oFSO = Nothing
    End Sub

    Public Property Let Directory(ByVal s)
        sDirectory = s
    End Property

    Public Property Get Directory
        Directory = sDirectory
    End Property

    Public Sub ListFolders()
        Dim oFolder
        Set oFolder = oFSO.GetFolder(sDirectory)

       '  whatever you want to code
    End Sub

End Class

Set MyClass = new Example

' set a directory
MyClass.Directory = Server.MapPath("/")
' list files
MyClass.ListFolders

' set another directory
MyClass.Directory = Server.MapPath("/testpath")
' list files
MyClass.ListFolders

Set MyClass = Nothing

%>
0
 

Author Comment

by:bodanon
ID: 10925048
I see.

You are right. Class objects are a bit more intricate than I realized, but it seems they are capable of providing the same capabilities as a COM object (if coded right).

This definatley points me in the right direction.
Thanks for the insight.
0
 
LVL 28

Expert Comment

by:sybe
ID: 10925985
>> but it seems they are capable of providing the same capabilities as a COM object

Well, yes and no.

No, because many COM objects are programmed in more advanced languages then VBScript, and bring the possibilities of those languages to VBScript. With a pure VBScipt Class you can not add non-VBScript functionality to VBScript (of course).

Yes, because a script-class is an object. And if you succeed in creating a script-class that does the same as a COM object, you will find that a script class might even be faster then a COM object. At least I found some evidence of that here: http://www.taka.nl/programming/asp/comparing_fileupload.asp
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question