Question Keeping count of button clicks on a webpage

Spyd3r0us

New member
Joined
Sep 2, 2010
Messages
4
Programming Experience
Beginner
Hello all,
Newbie to programming and VB.NET, but it is a class requirement for grad school so I suspect I will be on here a lot for the semester.

My professor wants us to create a program for kids that tests their math skills. Basically it will randomize 2 numbers and ask them to add or subtract them. It will give them a text box to type their answer. Once they enter the answer they will click on a calculate button and it will display if the answer is right or wrong. I have this part of the program working.

What I am having a problem with is the following... I need to keep track of the number of tries that are attempted and display the correct answer after 3 failed attempts.


I have tried the following for the button click but it doesn't seem to want to work. It just displays " 1 time" every time I change the answer and click the button.

VB.NET:
Expand Collapse Copy
Dim numTimes as Integer = 0

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

numtimes += 1

label1.text ("Button1 has been clicked" & numTimes & " times.") 
End Sub
 
The web pages are stateless which means that your variable is reset on each postback.
Add the following property to you code and use it as counter:

VB.NET:
Expand Collapse Copy
    Public Property NumTimes() As Integer
        Get
            Dim id As String = CType(ViewState("numtimes"), Integer)
            If id IsNot Nothing Then
                Return id
            Else
                Return 1
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("numtimes") = value
        End Set
    End Property

Note: use the NumTimes in the same way you are using numtimes variable ;)
 
Last edited:
Back
Top