473,394 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

ASP.Net, page scope variables why???

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
Mar 11 '07 #1
1 1976
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.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
"Max" <ma******************@fastwebnet.itwrote in message
news:JE*****************@tornado.fastwebnet.it...
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


Mar 11 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Hal Vaughan | last post by:
Being self taught, this is one thing I've always had trouble with -- I finally get it straight in one situation and I find I'm not sure about another. I have a class that keeps calling an...
33
by: Arthur | last post by:
>>>a= >>> for p in a: print p 1 2 3 >>> p 3 My naive expectation was that p would be 'not defined' from outside
3
by: Anonymous | last post by:
Is namespace the same thing as scope? While reading the book "Thinking in C++", I was under the impression that namespace is, well, a namespace--a feature to create a hiearchy for identifiers...
6
by: pembed2003 | last post by:
Hi all, I am reading the book "C++ How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
3
by: Grant Wagner | last post by:
Given the following working code: function attributes() { var attr1 = arguments || '_'; var attr2 = arguments || '_'; return ( function (el1, el2) { var value1 = el1 + el1; var value2 = el2...
7
by: Michael G | last post by:
I am a little surprised that the following that $x is visible outside of the scope in which it is (?)defined(?) (not sure if that is the correct term here). I am trying to find where in the php...
5
by: pembed2003 | last post by:
Hi all, I am reading the book "C How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
7
by: WXS | last post by:
Vote for this idea if you like it here: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=5fee280d-085e-4fe2-af35-254fbbe96ee9...
7
by: petermichaux | last post by:
Hi, I have tried the following based on suggestions of the best way to insert JavaScript into a page. This is instead of using eval(). Unfortunately IE says "unexpected call to property or...
0
MMcCarthy
by: MMcCarthy | last post by:
We often get questions on this site that refer to the scope of variables and where and how they are declared. This tutorial is intended to cover the basics of variable scope in VBA for MS Access. For...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.