Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
1.73/5 (3 votes)
See more:
Please tell me the difference between static variable, sessionstate and viewstate - all of these three.
I find, all of them can store value of a variable within round trips.

What should we use and in which condition? Performance wise?

Thanks
Posted
Updated 21-Dec-17 5:24am
v2
Comments
Sandeep Mewara 12-Jan-11 3:30am    
Did you try Google?
R. Giskard Reventlov 12-Jan-11 3:50am    
That's a very big question and inappropriate for this forum which is here to answer specific programming questions. Your question would be best researched using Google or at the library.

This is a short explaination:

Viewstate makes the roundtrip, it's rendered to the html and sent back during postback/callback.

Session state is a dictionary of objects that lives as part of your session. There is one for each session/user.

a static variable is global to the application on one server.


Regards
Espen Harlinn
 
Share this answer
 
Comments
OriginalGriff 12-Jan-11 4:34am    
"a static variable is global to the application on one server"
Not quite - it is not available outside the defining class unless it is specifically declared as public. Normally, a static variable would not be public (although a static property might).
Espen Harlinn 12-Jan-11 4:59am    
As I said - a short explanation. We both know that many questions merit more comprehensive answers, but the full answer usually put people, without the proper motivation, to sleep. A reasonably motivated developer would know the answer to this kind of questions or read a book.
A static variable holds information that is common to all instances on a class: It is shared between them instead of being created anew with each instance.

Sessionstate is a variable that is held on the server. Every time your user goes to the server to get a page, the server has to know how much of the last web page needs to be "remembered" when the new information is retrieved.

Viewstate is similar to sessionstate, but it remembers changes to the state of a page across postbacks.

In all seriousness, this is just a brief outline, there is a lot more to understand. I suggest you try Google, and read up on this, because there is a lot to learn, and a heck of a lot more that I can type in here!
 
Share this answer
 
ViewState is only used on a single page that handles postbacks. Once you
redirect to the new page, the ViewState is lost.

Session can persist the value across the application.So,even if you redirect to another page you don't lose the value.

Static Variable will be shared across all instances of aspx page.Hence if you have two users of same aspx at the same time value is shared which is not good & I personally would suggest never use static variables in a web scenario.
 
Share this answer
 
And, then, you have bullies like @Richard Deeming & Kats2512 who remove valid solutions.
 
Share this answer
 
Comments
[no name] 21-Dec-17 11:43am    
Maybe you have to explain that more. And the most aproriate Forums for this are "Bugs and Suggestions" or "Spam and Abuse Watch" and not as a solution to a Q dated in 2011 ....?
Richard Deeming 21-Dec-17 14:29pm    
And then you have idiots like this, who post complaints as "solutions" to questions, ignoring the fact that the question was solved SEVEN FRICKIN' YEARS AGO!

But hey, I suppose we should just let you carry on farming those rep-points, eh? >:(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900