Solved

display random images

Posted on 2004-04-28
3
432 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
[X]
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
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Suggested Courses

734 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