variables in memory and reloading a page

G

Guest

Hi,

I have 2 questions:

1.
IF i create an object on the Page_Load event function of a specific aspx
page. Like so:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim myObject as new AnObject
End Sub

When is this object deleted from memory? When a new page is loaded? Is this
object created on the server side or the client?

2.
Say i currently have an aspx page open and i want to reload this page to
update the values, how do i do this?
 
H

Hans Kesting

Joshua said:
Hi,

I have 2 questions:

1.
IF i create an object on the Page_Load event function of a specific
aspx page. Like so:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim myObject as new AnObject
End Sub

When is this object deleted from memory? When a new page is loaded?
Is this object created on the server side or the client?

When the reference goes out of scope (= your Page_Load has ended), then
the object is not accessible anymore. It is then eligible for garbage collection
and will be removed from memory "some time in the future".
A different request that accesses the same Page_Load has it's own copy of
myObject.

As asp.net is a server side technology, this object is created on the server.

2.
Say i currently have an aspx page open and i want to reload this page
to update the values, how do i do this?

Automatically reload? You can do this with a <meta http-equiv=refresh content=10>
tag in the head-section (the "10" is the number of seconds before the refresh).

By hand (user)? add a button to the page with an "empty" onclick handler.

From the server? You can't (without a lot of work)


Hans Kesting
 
G

Guest

Hans Kesting said:
When the reference goes out of scope (= your Page_Load has ended), then
the object is not accessible anymore. It is then eligible for garbage collection
and will be removed from memory "some time in the future".
A different request that accesses the same Page_Load has it's own copy of
myObject.

Does this mean that if i want to do anythin with global variables in another
class that i must dim the object of that class, change the variables in that
class and then add the object back to the application state again all within
the page_load function?

Like this:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Handles MyBase.Load
Dim myObject as new AnObject
myObject = Application.Get("myObject")
myObject.GetVariableinMyObject()
'do stuff with the object

Application("myObject") = myObject 'update the global variables that
were changed to the applicationstate
End Sub
 
G

Guest

And also:

When the reference goes out of scope (= your Page_Load has ended), then
the object is not accessible anymore. It is then eligible for garbage collection
and will be removed from memory "some time in the future".
A different request that accesses the same Page_Load has it's own copy of
myObject.

So does this mean that once the page has finished loading and is just
sitting there waiting for user to interact, that the object created in the
page_load function has been collected?

What im after is a way to create an object in memory that will be created on
page_load and will remain in memory until a button on the page is clicked. So
that operations can be performed on the object determinant of what button is
clicked. This is how i do it with windows form apps, is it different with
ASP.net?
 
H

Hans Kesting

Joshua said:
And also:



So does this mean that once the page has finished loading and is just
sitting there waiting for user to interact, that the object created
in the page_load function has been collected?

What im after is a way to create an object in memory that will be
created on page_load and will remain in memory until a button on the
page is clicked. So that operations can be performed on the object
determinant of what button is clicked. This is how i do it with
windows form apps, is it different with ASP.net?

It's *very* different (I have a similar problem in reverse when I switch
from asp.net to winform).
When the browser request a page, then an instance of the page-class
is created. It "does it's stuff" and creates html (usually) to be sent to the
browser via IIS. After sending that html the page-class can
do some cleaning-up and goes out of scope. You can't access this
particular instance anymore after that.
When it is *really* garbage-collected (and memory freed etc.) you can't say.

If you want to maintain state on the server, you need to use some of the provided
possiblilities:
- Session: if you want to keep things for a specific user
- Cache: has application scope (everyone sees the same cached objects)
- ViewState: for a specific postback cycle. Take care: this will be sent
to the browser and back to the server on postback!

Hans Kesting
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top