asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search





Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Code Samples
Components
Tools
New
Free
Downloads
Vendors
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

Random Image Display

Here's a small code snippet that displays a random image. Every time a page containing a call of this subroutine is hit, an image from the input folder is displayed randomly.


Sub DisplayRandomImage( szImageFolder )

   Dim szImagesFolderPath, szImageTag
   Dim nRandom, nCounter, nFilesCount
   Dim oFS, oFolder, oFileCollection, oFile

   If Right(szImageFolder, 1) <> "/" Then szImageFolder = szImageFolder & "/"

   ' translate virtual folder into physical path
   szImagesFolderPath = Server.MapPath (szImageFolder)

   Set oFS = CreateObject("Scripting.FileSystemObject")
   Set oFolder = oFS.GetFolder(szImagesFolderPath)
   Set oFileCollection = oFolder.Files
   nFilesCount = oFileCollection.Count

   if nFilesCount = 0 then
       Response.Write "No image available"      ' no image in this folder
       Exit Sub
   end if

   Randomize(Cbyte(Left(Right(Time(),5),2)))
   nRandom = int(nFilesCount * rnd) + 1

   ' in Visual Basic you could directly access the member of a collection like this:
   ' oFileCollection.Item(nRandom)
   ' but in ASP you have to loop through the collection:

   nCounter = 0
   For Each oFile in oFileCollection
     nCounter = nCounter + 1
     if nCounter = nRandom then
       szImageTag = "<img src=""" & szImageFolder & oFile.Name & """>"
       Response.Write szImageTag
        Exit For
     end if
   Next

   Set oFileCollection = Nothing
   Set oFolder = Nothing
   Set oFS = Nothing

End Sub

Submitted by Heinz Duschanek





email this code sample to a colleague


Support the Active Server Industry



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers