Persisting variable values in a webform? (1 Viewer)

The Stoat

The Grim Squeaker
Local time
Today, 10:22
Joined
May 26, 2004
Messages
239
Hi

I'm just starting to design some basic pages with Visual Studio .net
I've created a web application project and a form which connects to a db and displays some data. I was wondering if the forms allow persistant variables i.e. If i add a variable "above" the code is it able to persist it's value after a button has been clicked on the form like VB or is it like a webpage that would require the variable to be stored in cookie or server-variable and retrieved after submitting the form? If it's the later any idea how?

Thanks TS

Code:
Public Class NavigateData
  Dim Inc As Integer
Web Form Designer Generated Code

    Private Sub BtnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNext.Click

        Inc = Inc + 1
        LBL_REPORT.Text = Inc

    End Sub
 

Kodo

"The Shoe"
Local time
Today, 05:22
Joined
Jan 20, 2004
Messages
707
The Stoat said:
Hi

I'm just starting to design some basic pages with Visual Studio .net
I've created a web application project and a form which connects to a db and displays some data. I was wondering if the forms allow persistant variables i.e. If i add a variable "above" the code is it able to persist it's value after a button has been clicked on the form like VB or is it like a webpage that would require the variable to be stored in cookie or server-variable and retrieved after submitting the form? If it's the later any idea how?

Thanks TS

Code:
Public Class NavigateData
  Dim Inc As Integer
Web Form Designer Generated Code

    Private Sub BtnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNext.Click

        Inc = Inc + 1
        LBL_REPORT.Text = Inc

    End Sub

if you do that then the value of one will remain in the controls viewstate each time because when you post back to the page, the value is reset to its' base of 0. So it won't increment. What you need to do is put the value in viewstate so that you can return it later if the initial value is 0.

So you would something like this to persist the value.


Code:
Public Property inc() As Integer
        Get
            Return ViewState("inc")
        End Get
        Set(ByVal value As Integer)
            ViewState("inc") = value
        End Set
    End Property
 

The Stoat

The Grim Squeaker
Local time
Today, 10:22
Joined
May 26, 2004
Messages
239
Kodo said:
if you do that then the value of one will remain in the controls viewstate each time because when you post back to the page, the value is reset to its' base of 0. So it won't increment. What you need to do is put the value in viewstate so that you can return it later if the initial value is 0.

So you would something like this to persist the value.


Code:
Public Property inc() As Integer
        Get
            Return ViewState("inc")
        End Get
        Set(ByVal value As Integer)
            ViewState("inc") = value
        End Set
    End Property

That is just the job cheers :)
 

Users who are viewing this thread

Top Bottom