I the Page_Load I have a random event and a public variable

Dim rndImage As Integer

        rndImage = 1 + Int(Rnd() * 13)
        Select Case rndImage
            Case 1
                ImageRnd.ImageUrl = "/jpro/images/rnd/Image1.gif"
                rndAnswer = "A7b"
           Case 2
                ImageRnd.ImageUrl = "/jpro/images/rnd/Image2.gif"
                rndAnswer = "5RX"
       End Select

Then in the Button_Click event the string is to be compared with input from a textbox

If rndAnswer = Textbox1.text Then
  ' do events
end if

To debug I output the value of both variables, and for some reason it looks like the program is going thru the Load event twice.

Example :

The output for the load event was
rndAnswer = "5RX"

But in the button event
rndAnswer = "A7b"

I thought maybe because PostBack is basically a page reload. Then I would add a boolean var to the load event:

If boolCheckPublic = false then
  ' do events
  boolCheckPublic = true
End If

But that did not work.

Recommended Answers

All 9 Replies

Try using CompareTo() method.

if TextBox.Text.CompareTo(rndAnser) = 0 Then ' Means that are not equal - False Value

else ' True Result ( > 0 or < 0 value)

End if

We need to see your Page_Load routine. The Load event will indeed run each and every time. If there is code that should only run the first time the user visits the page, you isoloate that code within an "IsPostBack" conditional.

It sounds to me like you could try checking if it's a postback then execute proper code, because Page_Load will execute every time, no matter post back or a regular visit.

If Not Page.IsPostBack then
... do something
End If

I will have to get back to you on using 'IsPostBack'

I know I tried that recently, but it did not work right. Although I forget why.
It worked initially but something else further down the line didn't work.

Since what I'm using now 'session variables' appears to only work intermittentley and is admittedley ( a bad idea )
I'm going to try that again and see what I can come up with.

I will of coarse also look into CompareTo() as well.

I stopped this project altogether for a wbout a week.
I had an error I could not figure out that was starting to lead me to beleive this project was doomed.
( It turns out I was experimenting with passing a GET string with the old )

<input type="hidden" name="x" value="<%= variable %>">

And .NET didn't like it.

I had forgotten about it and for days I was tearing my hair out trying to find out what was wrong.
I happened to flip my project back into 'debug' mode and the error showed up.

Anyway Thanks

I'm not sure if this is a problem you've been having when you say "... It worked initially but something else further down the line didn't work ..."

Be aware that if you set a variable before postback and then check it after postback it will be reset, use Session or ViewState to keep variable values regardless of postback state.

I did that and it worked.

I remember the problem now.
It works, only now I need the Is.PostBack to be set to False.
Sort of like resetting the entire program so it can be run a second time.

something like

If Not Page.IsPostBack = True Then
     ' do events
ElseIf Page.IsPostBack = True Then
            Page.IsPostBack = False
        End If
End If

But IsPostBack is read only so I guess I'm screwed.

Yep PostBack variable is off limits, use your own variable and check it something like so...

If Page.IsPostBack AND MyVar = True Then
MyVar = False
MyVar = True
End If

You can still use hidden form elements. Just iterate through the Request.Form. I don't think you need Session or ViewState or tricks with PostBack. Just pass in a hidden form element value. If it's true, run your randomizer. If not, don't.

I Think I might just use Javascript client-side to select the random image.

If I call the Javascript from another page the result will be hidden and so the protection will still work.

He he... I Read your Post here:

Like Achems Razor says... "the simplest answer tends to be the correct answer"

Thanks a mill..., I know this will work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.