Variable Lifetime

B

bucrepus

I created a simple asp.net web application, with a button.
I declared the var tmpStr
Imports ....
Public Class myClass
dim tmpStr as string

Private Sub Page_Load
check for postback and set tmpStr
if not ispostback then
tmpStr = "ABCDEFG"
'I debug check tmpstr here and it is "ABCDEFG"
end if
end sub

AND THE BUTTON CLICK..
private sub myButton_Click(...)
if tmpStr = "ABCDEFG" then do something..
look at the value of tmpStr and it is empty
end sub

Is it normal for tmpStr to loses its value after pageload. tmpstr is null
when I click the button. MSDN says var. retains its value(lifetime) until
app ends if not a local var.
Thanks KT
 
G

Guest

Hi

You have to understand the sequence of these events
In the first loading of the page, Page_Load is run through, tmpStr is assigned to "ABCDEFG". Button has not been clicked, myButton_Click event is not triggered. Then page unload from server, tmpStr is destroyed
In the upcoming postbacks if myButton clicked, Page_Load is run through, however IsPostback is true, tmpStr is not assigned. myButton_Click event is triggered.
So if tmpStr is assigned to "ABCDEFG" without the condition of IsPostback, you will get the right value in myButton_Click event

Bin Song, MC

----- bucrepus wrote: ----

I created a simple asp.net web application, with a button
I declared the var tmpSt
Imports ...
Public Class myClas
dim tmpStr as strin

Private Sub Page_Loa
check for postback and set tmpSt
if not ispostback the
tmpStr = "ABCDEFG
'I debug check tmpstr here and it is "ABCDEFG
end i
end su

AND THE BUTTON CLICK.
private sub myButton_Click(...
if tmpStr = "ABCDEFG" then do something.
look at the value of tmpStr and it is empt
end su

Is it normal for tmpStr to loses its value after pageload. tmpstr is nul
when I click the button. MSDN says var. retains its value(lifetime) unti
app ends if not a local var
Thanks K
 
B

bruce barker

a class variable has the same life of its class. it is created and destoryed
when the class is created or destoryed.

on render asp.net create a new version of your page class, calls the events
then releases. when the postback happens the same thing happens as they are
seperate page requests.

-- bruce (sqlwork.com)
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top