Click here to Skip to main content
15,899,313 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
I want to know more about ViewState. I know that ViewState is used to Persist User entered data and control values in round trips.
I have two Questions:
1. If I disable viewstate for complete page, then what will happened? How can then I persist the data? for eg if I have a signup page and it has ten fields, I disable viewState of the page, then How can I persist data?
2. Do MVC use ModelBinder as alternate of ViewState or some other technique Please tell
Posted

I will try to answer the first part of your question:
1. If I disable viewstate for complete page, then what will happened? How can then I persist the data? for eg if I have a signup page and it has ten fields, I disable viewState of the page, then How can I persist data?

Lets start by a very simple example. Create a web page and add a textbox and a button to post the page. Disable the ViewState for the page and then run the page in debug mode. Add some text in the texbox and click the button to post the page. In the page load, check the value of the textbox. You may be surprised to see that the value you entered is available even though the ViewState was disabled.
So it's important to understand what ViewState stores. Usually the form data is available in the page even with ViewState disabled. ViewState is useful in determining the changed data. Another case in our example where we would have needed ViewState is if you would have set ForeColor property (and many such which are available with ASP.NET TextBox control) of the textbox.
It's usually recommended to disable the ViewState if you are not using it as it makes the page heavier.
MSDN has a very comprehensive article on ViewState here: Understanding ASP.NET View State[^].
Go through it and most of your doubts will be cleared.
You also ask how do we persist data otherwise. ViewState is a client side state management technique in asp.net. There are other client side mechanism of storing data (like hidden variable, cookies, querystring etc). There are server side techniques as well. Again, there is a very nice article on MSDN for the same which will help you understand state management very well - ASP.NET State Management Recommendations[^]

I won't be answer the 2nd part as I am new to MVC as well.

Hope that helps!
 
Share this answer
 
Comments
Abhinav S 7-Aug-13 8:24am    
5.
You can still go ahead and store data in sessions - so disabling viewstate does not mean you cannot store data across sessions.
 
Share this answer
 
Comments
userBeginner 7-Aug-13 7:47am    
Thanks, sir what about MVC?
Abhinav S 7-Aug-13 7:52am    
Same there. Nothing changes. Use sessions if you have to.
Go through http://weblogs.asp.net/imranbaloch/archive/2011/07/26/using-the-features-of-asp-net-mvc-3-futures.aspx.
Ankur\m/ 7-Aug-13 8:22am    
ViewState is a client side state management technique whereas Session is a server side technique. Session is certainly not an alternative to ViewState.
Abhinav S 7-Aug-13 8:29am    
If the OP is looking at storing data across sessions, he can use any of client or server state mechanisms.
So while one is not a direct alternate of the other, they are all ways of achieving a similar goal.

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