Variable Scope on the Page

R

Raheel Hussain

hi,

can any body tell me why the variable loses its value,
i m actually declaring a variable which is public, and assigning a
values in Function1 and reading the values in Function2

following is a sample code for wht i m doing.
---------------------------------------
Public Class SupportRequest
Inherits System.Web.UI.Page
Dim Var1 As Integer
..
..
Private function Function1
Var1=1
End Function

Private Function Function2
'Var1 returns 0
Textbox1.text= Var1
End Function

End Class
 
M

Mark Fitzpatrick

Remember, the web is inherently stateless. That means every page view is
like a brand new occurance of the page. The variables and data is generally
not kept. You have to store the variable somewhere to handle this sort of
thing. Luckily, ASP.Net has the Viewstate. You can store the variable in the
viewstate after you perform some work on it in button one, then retrieve it
in button 2 (and do some work with it again and store it, etc..). Whenever
you access a Viewstate item you'll need to check if it is null first, just
to make sure because if you haven't saved the variable to the viewstate then
it will come back as null and possibly mess up your code.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top