Solved

display random images

Posted on 2004-04-28
3
390 Views
Last Modified: 2010-04-07
hey there;

i'd like to display random images each time my asp.net page is reloaded.

these images are all stored in a specified folder on the server in either jpg, gif and png formats.

i simply wish to reach in the folder, grab a random file, then display it.  

must be vb.net code as i'm not a c# programmer.  also, i'm *not* looking for any xml solutions (like the ad rotator uses).

thanks.
0
Comment
Question by:vbnewbie01
  • 2
3 Comments
 
LVL 4

Expert Comment

by:pml_siva
ID: 10937752

string[]  sDir = Directory.GetFiles(@"E:\images");
                  
Random rnd = new Random();
int iRndIndex = rnd.Next(sDir.Length);
string strDisp = "<img src= '" + sDir[iRndIndex] + "'";
Response.Write(strDisp);
0
 
LVL 4

Accepted Solution

by:
pml_siva earned 100 total points
ID: 10937814
Dim sDir As String() = Directory.GetFiles("E:\images")
 Dim rnd As Random = New Random
 Dim iRndIndex As Integer = rnd.Next(sDir.Length)
 Dim strDisp As String = "<img src = '" & sDir(iRndIndex) & "'"
 Response.Write(strDisp)
0
 
LVL 1

Author Comment

by:vbnewbie01
ID: 10941690
strong work, pml siva;.  thanks.

however, i made a couple of modifications in case anyone else is looking to do a similar thing by having your image displayed using an asp.net control.

first, i put the code in my Page_Load event, then i had to append the opening line to read System.IO (in front of the DIrectory.GetFiles)

next, i put an image control (asp:image id="imgRandomPhoto" runat="server") in my html code.

i called it in the Page_Load event using:

imgRandomPhoto.imageURL=sDir(iRndIndex)

entire code written below
---------------------------------------------------

Dim sDir As String() = System.IO.Directory.GetFiles("C:\Inetpub\wwwroot\Website\uploaded_images")

Dim rnd As Random = New Random
Dim iRndIndex As Integer = rnd.Next(sDir.Length)
Dim strDisp As String = "<img src = '" & sDir(iRndIndex) & "'"
            
imgRandomPhoto.imageURL=sDir(iRndIndex)

----------------------------------------------------

thanks again.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

757 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

20 Experts available now in Live!

Get 1:1 Help Now