Solved

ASP class object: call a sub from a  hyperlink

Posted on 2004-04-22
3
418 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
SQL Server Web Traffic Limitations 4 68
Summernote required 3 184
SQL to update characters in table column 6 147
Code Manager | Snippits 2 36
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

837 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