View Full Version : Persisting variable values in a webform?


The Stoat
12-08-2005, 03:01 AM
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

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
12-08-2005, 05:18 AM
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

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.


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
12-08-2005, 05:54 AM
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.


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 :)