Solved

display random images

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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