ASP.Net, page scope variables why???

M

Max

I 've got to understand this. I come from dotnet windows applications, not
webforms, ok, but this is too strange and I've already asked this NG and
tried what suggested without solution
Why this doesn't work: I press Button1 and the textbox shows "hello" - then
I press button2 and textbox shows empty field!
What else to do, everything is public here.
Please help
Max


Public Class Default5
Inherits System.Web.UI.Page

Public s As String ''''(or Dim s as string, it's the same)

''''''''''''''''''''''

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

s = "hello"

TextBox1.Text = s

End Sub

'''''''''''''''''''''''''

Public Sub Button2_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button2.Click

TextBox1.Text = s

End Sub

end class
 
E

Eliyahu Goldin

Max,

A webform life cycle is very different from a windows form one. A windows
form stays in the memory all the time. A webform's life is very short - from
the http request arrival to sending the response back. A few seconds or
less. After the page has been served to the client, there is nothing left
from the page in the server's memory unless you take a special care to
persist what you may need in feature requests.

In your case the page first makes a request (postback) to serve the server
event Button1_Click. The response is sent and the page's gone. Then another
request arrives, this time for the server event Button2_Click. At this stage
nothing is left on the server from the previous request. The page builds
from scratch with s As String ''''.

To use s in the second postback, you need to persist it. There are several
ways of doing this, most common are in a session variable and in ViewState.
Make your research on the differences.
 

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top