How to Display a Random Image
You many want to display a random ad or image on your page. When you do, use the code below. In this example,
you would need to hit the refresh button to get a new image. The image you see here
is a random image from my soccer folder.
h804.jpg
ASP.NET CODE
<asp:Image ID="imgRandom" runat="server" />
<asp:Label ID="Label1" runat="server" Text="Label" />
VB.NET CODE
Imports System.IO
Private Sub Page_Load()
Dim imageToDisplay As String = GetRandomImage()
imgRandom.ImageUrl = Path.Combine("~/images/soccer/", imageToDisplay)
lblRandom.Text = imageToDisplay
End Sub
Private Function GetRandomImage() As String
Dim rnd As New Random()
Dim images() As String = Directory.GetFiles(MapPath("~/images/soccer/"),
"*.jpg")
Dim imageToDisplay As String = images(rnd.Next(images.Length))
Return Path.GetFileName(imageToDisplay)
End Function
Multimedia
Web Development
Search Engine Optimatization