Solved

ASP class object: call a sub from a  hyperlink

Posted on 2004-04-22
3
411 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

810 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