Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
What is the diffrence between session and view state.Am a beginner in asp.net.
Am trying to get the diff between session and view state.I just created one sample application
Session["user"] = textbox1.Text;
ViewState["vwuser"] = textbox1Text;
Response.Redirect("newpage.aspx", false);
in newpage.aspx page load am trying to get values of session as well as viewstate,
label1.Text = Session["user"].ToString();
label2.Text = ViewState["vwuser"].ToString();
here am able to access session value but viewstate value is null.
so what is the diff between these two?
Why am unable to get value in viewstate?
why value in view state is null?
can one viewstae store more than one value loke collections?
Posted

1 solution

Hello,

ViewState can only save data during postback of your page. Here, what you are doing is transferring it to other page and hence viewstate loses data.

While Session maintains data for current user and that is why it does not loses data.

For details:
http://www.aspdotnet-suresh.com/2012/11/aspnet-difference-between-viewstate-and.html[^]

Hope this helps you :)
 
Share this answer
 
Comments
Am Gayathri 20-Aug-14 6:43am    
Thanks a lot...so data stored in view state can be used only in that page right?
Prakriti Goyal 20-Aug-14 6:55am    
Yes. :)
Am Gayathri 20-Aug-14 7:24am    
But then what is the use of that?
Think,i want to store the value of textbox i can use a varibale.
like instead of storing in ViewState, cant we store that data in any variable?
anyway when the page reloads, viewstate will be cleared right ?


My question ma be stupid :)
Prakriti Goyal 20-Aug-14 7:39am    
When the page gets reloaded, viewstate doesn't loses the data.
When the page redirects, then viewstate loses data.
You first try doing the same thing with variable and then you'll know the difference.
Raul Iloc 20-Aug-14 8:10am    
Good answer, you have my vote!

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