ASP.NET Page Variables Persistence

G

Guest

Hi,

I am designing an ASP.NET application in which particular pages require global variables that need to be persisted during postbacks. What is the best practice to do this?

Is it to use the viewstate object or is declaring the variables as static sufficient (I am currently using this method)?



Thank you,

Mark Anthony Spiteri
 
G

Gareth Jones

Hi,

I am designing an ASP.NET application in which particular pages require global variables that need to be persisted during postbacks. What is the best practice to do this?

Is it to use the viewstate object or is declaring the variables as static sufficient (I am currently using this method)?

You'll need to use the ViewState object, or perhaps the Session
object. A static variable isn't enough - in a web environment the
application is restarted for every request, so your static variables
are being reinitialised.

Gareth
 
G

Guest

Thank you for your reply.

Gareth Jones said:
You'll need to use the ViewState object, or perhaps the Session
object. A static variable isn't enough - in a web environment the
application is restarted for every request, so your static variables
are being reinitialised.

Gareth
 
G

Guest

Thank you for your reply

Gareth Jones said:
You'll need to use the ViewState object, or perhaps the Session
object. A static variable isn't enough - in a web environment the
application is restarted for every request, so your static variables
are being reinitialised.

Gareth
 
S

Scott Allen

Are the values of the page global variables supposed to be the same
for all of the users who visit the page?
 
G

Guest

No,

each user will have different variable values. Thus making use of the Application Object is futile.
 
S

Scott Allen

Yes, this also rules out using static variables. As suggested, Session
state and Viewstate are both options.

--s
 
J

John Saunders

Mark Anthony Spiteri said:
Hi,

I am designing an ASP.NET application in which particular pages require
global variables that need to be persisted during postbacks. What is the
best practice to do this?
Is it to use the viewstate object or is declaring the variables as static
sufficient (I am currently using this method)?

Use ViewState if you need the variables to persist for multiple postbacks to
the same page. Use Session state if you need longer persistence.

Using static variables is almost always wrong, as they will be shared
amongst users. They will also have to be synchronized if written to because
multiple requests may access them at the same time.
 

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,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top