Keeping random background

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim in Arizona

    #1

    Keeping random background

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

    Partial Class lobbytimes_lobb ytimes
    Inherits System.Web.UI.P age

    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.EventArg s) 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="<%R esponse.write(B ackgroundImage) %>">

    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
  • Steve C. Orr [MCSD, MVP, CSM, ASP Insider]

    #2
    Re: Keeping random background

    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

    --
    I hope this helps,
    Steve C. Orr,
    MCSD, MVP, CSM, ASPInsider



    "Jim in Arizona" <tiltowait@hotm ail.comwrote in message
    news:%23uRufkIf HHA.588@TK2MSFT NGP06.phx.gbl.. .
    >I created a public application based variable not within any procedure,
    >like so:
    >
    Partial Class lobbytimes_lobb ytimes
    Inherits System.Web.UI.P age
    >
    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.EventArg s) 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="<%R esponse.write(B ackgroundImage) %>">
    >
    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

    Comment

    Working...