The following is question that is asked by one of the Community Member from Beyond Relational Website to my personal ID.
“I have one doubt in sessions. Sessions are using in the website that sessions variables are storing in server. Suppose at a time if ten users send the request to server, 10 sessions variables are storing in the server are not. Suppose ten session variables are created lot memory required in server. In the session replace using the view state. I want to get clear difference between sessions and view state”
Yes It is. For each user each session gets created and stores separately. If there are 100 users, accessing same data and you are creating session for that, 100 sessions will be created even though you have same data. 100 different session IDs are stored in the server for the requests and session is tracked based on session ID. Yes if you have number users accessing and storing large data in sessions then page load will be slow definitely.
When every client is send request to the server store Session is accessible at the client side in the form of cookies. So Every time client hits the server in the user machine does it creates cookies each time?
Actually cookies are 2 types.
- In Memory Cookie
- Persistent Cookie
If the Persistent cookie gets created for the request, then Every time client hits the server in the user machine does it creates cookie. This cookie is persistent until user deletes browser cookies.
These are typical ASP.NET beginner question, but I am not sure how many web developers today use View-State and Session State efficiently and effectively. It is important to understand the concepts and equally important to utilize them where and when required. Here are some best and important practices to be followed considering performance of the web applications regarding view state and session state.
Can I replace session using the viewstate.? Session vs ViewState
No. Both are different concepts that cannot be replaced one with another. Session is persistent across single browser, whereas ViewState is for single page. ViewState is stored on browser or client means in webpage, session is on server.
Whenever you are using the sessions clearing sessions are important. Remember that closing the browser does not get session to clear. You need to call
- Session.Clear() - clears the session.
- Session.Abandon() - destroys the session completely .
It is a best practice to clear the session while you are no longer need them or their purpose is solved.
When to use What?
If you need to access a variable in same page, then you can go for view state.
- If you are accessing variables on the same page (to get data) under post back use ViewState.
- If the data you store in the ViewState is not large enough to cause performance headaches, go ahead with Viewstate.
For accessing variables across any other (throughout) pages you can use session.
Suppose you have requirement like to open a child window through client script and the child window requires some information related to parent page. In such case, You can store the information in session before loading the first page and clear the session when the child window gets loaded, obtain the information from session for parent window.
Clearing the session is very important, if data is not needed any more in other pages.
If you have ViewState of your page is large, use HTTP Compression.
We do not really need to store the ViewState for Label. In most cases we can avoid ViewState for it for a HyperLink and even Images and buttons too.
At worst turn the ViewState off for each control and see if your page renders fast or not.
The larger our page is, the longer it takes to render on the client side browser. Smaller page size is always better. If there is data in the control that will be needed when the Form is posted, then only enableViewstate, otherwise set it to false.
If the session mode is inproc then session gets destroys when application Pools memory is over and it gets recycled. Try to get data from data base with best practices instead of using Sessions for same database calls across pages. Having ViewState will also degrades SEO as the google will give less indexing for the page that has ViewState comparing the page with the same content but no ViewState. That is why MVC3 is most popular than asp.net web forms. No ViewState, No Page load for ASP.NET MVC projects.
ASP.NET4 gives us more control on Viewstate by ViewStateMode property, to disable ViewStateMode at page level and enable ViewStateMode at control level.
- Do not rely heavily on session variables. This can lead to many "object not set to an instance of an object" errors, which can be terrible to debug. No reliability.
- Never replace viewstate with session
- Don't store datasets in a session.
- Don’t store GridView data in a session.