First, we need to use System.IO to display the Random Images on Page Load, if the browser is closed and the session ends, a new image will be chosen at random upon a new session start, and remain until the session ends, again.
Imports System.IO
Partial Class _Default Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load chooseImage() End Sub
Public Function chooseImage() As String If Session("img") Is Nothing Then Dim imgPath As String Dim fileCount As Integer = Directory.GetFiles(Server.MapPath("/media/img/"), "*.*", SearchOption.TopDirectoryOnly).Length fileCount = fileCount + 1 imgPath = "media/img/" & RandomNumber(1, fileCount) & ".jpg" Session("img") = imgPath Return imgPath Else Return Session("img").ToString() End If End Function
Private Function RandomNumber(ByVal min As Integer, ByVal max As Integer) As Integer Dim random As New Random() Return random.Next(min, max) End Function End Class
|
No responses found. Be the first to respond and make money from revenue sharing program.
|