Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

display random images

Posted on 2004-04-28
3
418 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

829 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