State management is the process by which you maintain state and page information over multiple requests for the same or different pages. There are 2 types State Management:
- Client – Side State Management
- Server – Side State Management
Client – Side State Management
- View State – Asp.Net uses View State to track the values in the Controls. You can add custom values to the view state. It is used by the Asp.net page framework to automatically save the values of the page and of each control just prior to rendering to the page. When the page is posted, one of the first tasks performed by page processing is to restore view state.
- Control State – If you create a custom control that requires view state to work properly, you should use control state to ensure other developers don’t break your control by disabling view state.
- Hidden fields – Like view state, hidden fields store data in an HTML form without displaying it in the user’s browser. The data is available only when the form is processed.
- Cookies – Cookies store a value in the user’s browser that the browser sends with every page request to the same server. Cookies are the best way to store state data that must be available for multiple Web pages on a web site.
- Query Strings – Query strings store values in the URL that are visible to the user. Use query strings when you want a user to be able to e-mail or instant message state data with a URL.
Server – Side State Management
- Application State – Application State information is available to all pages, regardless of which user requests a page.
- Session State –Information is available to all pages opened by a user during a single visit.
Both application state and session state information is lost when the application restarts. To persist user data between application restarts, you can store it using profile properties.
We can use one of the below options to pass value between 2 pages in ASP.Net.
- If the value is a String then we can use either one of the following: QueryString, PostBack, Cookies, Session, Cache, Application, Database
- If the value is a Dataset or an object then we can use either one of the following: (Assuming DTO object not serialized) – Session, Application, Cache
- It is a built-in structure for automatically retaining values amongst multiple requests for the same page.
- The viewstate is internally maintained as a hidden field on the page but is hashed.
- View State is used to retain the state of server-side objects between post backs.
- Controls perform well – Label, TextBox, CheckBox, RadioButton, and HyperLink
- Controls perform less – DropDownList, ListBox, DataGrid, and DataList because of size and large data.
- Making roundtrips to the server.
- Item stored in Viewstate exist for the life of the current page. This includes PostBack (to the same page).
- No server resources are required.
- The values in view state are hashed, compressed, and encoded, thus representing a higher state of security than hidden fields
- It is good for caching data in Web frame configurations because the data is cached on the client
- Simple to use
- Page loading and posting performance decreases when large values are stored.
- Although view state stores data in a hashed format, it can still be tampered because it is stored in a hidden field on the page.
- EnableViewState turns on the automatic state management feature that enables server controls to re-populate their values on a round trip.
- Through EnableViewState=true /false we can define whether we have to maintain values of control in hidden fields when page is posted back again and again
- For example, if you are binding a control to data on every round trip then you do not need the control to maintain its view state, since you will wipe out any re-populated data in any case.
- Viewstate is enabled for all server controls by default. To disable it, set the EnableViewState property of the control to false.
- These are html input control with hidden type that store hidden data in the html.
- Syntax – <input type=”hidden” name=”EVENTTARGET” id=”EVENTTARGETID” value=”” />
- Can store only one value in their value property. The value is saved as a string so we need to Typecast.
- There are two types of server control hidden fields –
- Easy to implement
- Hidden fields are supported by all browsers
- Faster access of information because data is stored on client side
- No server resources are required.
- Not secure because the data is stored on Client side so can be tampered creating a security hole
- Decreases page performance if too many hidden fields(large data’s)
- Only support single value and do not support rich structures as HTML.
- Have to work around with delimiters etc to handle complex structures.
Disclaimer – F5debug Interview Questions & Answers Series:
You may recopy extracts from these pages (“the material”) to individual third party websites or to any intranet websites, but only if:
You acknowledge www.f5debug.net as the source of the material. Such acknowledgment should include reference to www.f5debug.net in the copy of the material and should also include “© Karthikeyan Anbarasan, www.f5debug.net “. You inform the third party that these conditions apply to him/her and that he/she must comply with them.