If you are a classic asp programmer then you must know that web applications are stateless. In classic asp state management was done using hidden variable and scripting i.e. Values are stored in hidden variables and reloaded to controls whenever needed. We needed this technique since web applications run on HTTP which is a stateless protocol.
If you have worked on asp.net or for that matter migrated to asp.net from asp then you should be glad to know that you don't need to do that stuff here.
So are asp.net applications stateful?
Answer is NO. Since asp.net web applications still run on HTTP which is again stateless. What asp.net or for that matter most of the web application frameworks do for you is that they hide the state management part from you, which is now framework managed but this comes with a CATCH word. You must use Server Controls for your application to manage state properly. So asp.net is not stateful but asp.net server controls are. We will cover server controls some other time...
Now the question arises how are server controls stateful?
Actually if you see the source of generated html page in your browser, you will find a hidden variable with name __ViewState. This variable holds behaviour/UI Information for all the controls.
What is ViewState and what is the type of Viewstate?
View state is the accumulation of the values of a server control's properties. These values are automatically placed in the server control's ViewState property, which is an instance of the StateBag class.
How this Works?
I am assuming you are all familiar with asp.net page life cycle. If not please refer
When you request a page for first time, the page gets created in server’s memory and all the controls are populated with either default values or value set via code. Every control basically hold two types of data first one is behavioural in nature and is called ControlState. Some examples can be like grid’s pagination information or textbox control’s Text Changed event, this type of information relies heavily on ViewState so if ViewState is turned off these will not work. To avoid such a scenario ControlState is turned ON by default and there is no way you can turn it off. Second type is actual state e.g. DataGrid’s data so if you are just displaying data using datagrid and don’t want that data on postback you can safely turn it off by turning ViewState off.
Can you give some examples of what all can be stored in ControlState?
Control Name | Properties |
MultiView | ActiveViewIndex |
Wizard | ActiveStepIndex |
FormView | PageIndex , Mode , DefaultMode |
GridView | Selectedndex, PageIndex , EditIndex , SortDirection , SortExpression |
CheckBoxList | PreviousItemsCount , PreviousOffset |
Where is ControlState stored?
Control State is not stored separately but in same __ViewState variable which holds viewstate data
The blog is helpfull,Interesting and informative...Good post...Thanks
ReplyDeletenice article
ReplyDeletemay u share some interview Quesation in state mgmt
Nice article
ReplyDelete