Solved

ASP class object: call a sub from a  hyperlink

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now