difference between viewstate and session in asp.net

Posted by Shanky11 under ASP.NET on 5/27/2013 | Points: 10 | Views : 5671 | Status : [Member] | Replies : 10
we can use session instead of viewstate to hold value in a asp.net page.
then why we use viewstate????????????/




Responses

Posted by: Raj.Trivedi on: 5/27/2013 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hello Shanky,

ViewState can be used if you want to store data between post back of same page

but if you want to send or Store data across pages

for eg :-- if you want to pass data from one page to another page you will have to use Session, for this eg Viewstate will not work.

It is recommended to use session when there is need of data passing across pages and view state is recommended when you want to store data temporarily during postback of the same page.

Regard's
Raj.Trivedi
"Sharing is Caring"
Please mark as answer if your Query is resolved

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 5/27/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
The View State is client state mechanism in the ASP.Net statement management.

The view state is control it will be used to maintain the state of the control across the posted back to the server. The value will be stored in the hidden control. Every time it needs to encryption and decryption (serialize and deserialize across the post backs).

The view state can be enabled by setting the property called EnableViewState="true/false". It can be set in the machine config/ web config/ page directives/ control level.

By default it is false. You can enable it by setting boolean flag.

This can be used with in the page and not across the page.

Viewstate["Version"] = txtVersionName.Text.ToString();

The view state also can store the data using the key and value combination. The data will be serialized and stored in the hidden control You can view the source code page and find the hidden control with _viewstate.


Drawbacks:
- Increase the page payload (when you have grid with many rows then every thing has to be loaded and retain again with serialize and deserilize)
- Additional overhead while serialize and deserialize.
- Increase the memory allocation on the server.





You can see the assigning and retrieval of view state in asp.net

protected void Button1_Click(object sender, System.EventArgs e)
{
ViewState["FavoriteColor"] = TextBox1.Text.ToString();
ViewState["City"] = TextBox2.Text.ToString();
Label1.Text = "Your data saved in ViewState.";
}
protected void Button2_Click(object sender, System.EventArgs e)
{
string color = (string)ViewState["FavoriteColor"];
string city = (string)ViewState["City"];

Label1.Text = "Hi your favorite color is: " + color +
"<br />and you came from: " + city;
}

If this post helps you mark it as answer
Thanks

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shanky11 on: 5/28/2013 [Member] Starter | Points: 25

Up
0
Down
for the page level we cn stor value in session then why we uses viewsate

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Oswaldlily on: 5/29/2013 [Member] Starter | Points: 25

Up
0
Down
Eg1) Sometimes ,Login UserId requires for all Pages..
Here U go for Session..

2)Some data cant be accessible when move to new functions/methods in same Page.
So store in Viewstate so that u can access from all functions throughout the page(Page level)...



Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shanky11 on: 5/30/2013 [Member] Starter | Points: 25

Up
0
Down
for the same page we can also use session
then why viewsate????????????????

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: perfectchourasia-9163 on: 5/30/2013 [Member] Starter | Points: 25

Up
0
Down
Viewstate can be used at Page level whereas session used at Application level.
Viewstate stored in hidden field whereas session stored in memory.


ER sandeep chourasia
sandeepchrs@yahoo.com (on facebook)
http://www.aspnetcodes.com/

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Oswaldlily on: 5/30/2013 [Member] Starter | Points: 25

Up
0
Down
1)Big values like datasets should not be saved in viewstate as they may tend to slow down the performance of the web page.
so u can switch over to session in this kind of situation alone for Page level..

For storing small values in Page level u can be use Viewstate.


2 even if a user sits on a page for an hour, then clicks somewhere, the page will still maintain the viewstate, while the Session will probably have expired



Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shanky11 on: 5/30/2013 [Member] Starter | Points: 25

Up
0
Down
i didi nt get this point
Viewstate stored in hidden field whereas session stored in memory.

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Annaharris on: 5/30/2013 [Member] Starter | Points: 25

Up
0
Down
Viewstate can be used to pass values within the page, but session can be used to pass values across pages within your project.

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shanky11 on: 5/31/2013 [Member] Starter | Points: 25

Up
0
Down
but for the page level we can also use session then why to use viewstate??????????????????????

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response