Keeping random background

J

Jim in Arizona

I created a public application based variable not within any procedure,
like so:

Partial Class lobbytimes_lobbytimes
Inherits System.Web.UI.Page

Public BackgroundImage As String

Protected Sub Page_Load(ByVal ..)

I then created a background image randomizer subroutine and placed that
into 'If not Page.IsPostback' decision on the page load routine, like so:


Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

If Not Page.IsPostBack Then
LoadBackground()
End If

End Sub

And here's the LoadBackground routine:

Sub LoadBackground()

Randomize()
Dim imagegen(2) As String
Dim x As Short
x = CShort(Rnd() * 2)
imagegen(0) = "../lobbytimes/backgrounds/a1.gif"
imagegen(1) = "../lobbytimes/backgrounds/a2.gif"
imagegen(2) = "../lobbytimes/backgrounds/1.jpg"
BackgroundImage = imagegen(x)

End Sub

And on the aspx page, I put this:

<body background="<%Response.write(BackgroundImage)%>">

So, the problem is that when the page first loads, the random background
image loads fine. But, on any postback event (which there are several on
this application), the background image disappears and all that's left
is a white background (nothing).

Sure, I could place the LoadBackground routine under the Page_Load Sub,
but it would be annoying to see the background change 100 times a day.

I wanted the random image to load once and stay there while they used
the application.

Any ideas?

TIA,
Jim
 
S

Steve C. Orr [MCSD, MVP, CSM, ASP Insider]

At the end of your LoadBackground function you should store the chosen
background value somewhere to persist it between postbacks.
Perhaps ViewState would be a good place. Alternately you might consider
Session state or perhaps a cookie.
Then in your Page_Load event you'd have code similar to this:

If Page.IsPostback() Then
'Load from ViewState
Else
'call LoadBackground function
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top