Documente Academic
Documente Profesional
Documente Cultură
http://www.codeproject.com/Articles/31344/Beginner-s-Guide-To-View-...
Okey Ikechuwu
621
Sign out
home help
articles
quick answers
discussions
features
community
what is a viewstate in asp.net ?
Next
Article Browse Code Stats Revisions Alternatives Comments & Discussions (122)
About Article
This article is a step by step guide to view state with details Type Article CPOL 29 Nov 2008 192,991 275 times
Table of Contents
Introduction What is state management ? Different types of state management? What is view state ? Advantages of view state ? Disadvantages of view state ? When we should use view state ? When we should avoid view state? Where is view state stored ? How to store object in view state ? How to trace your view state information? Enabling and Disabling View State How to make view state secure? Some Important Points
Introduction
First of all I want to thank Sean Ewington for his great initiative to write Beginner's Walk for Web Development article. I have decided to write some articles on state management There are a few article on Code project on State Management, basically on Session, Caching, Cookies, etc. Though all are very good article, still I have planned for write some article on state management. and I believe that should definitely helps to all the Beginners. And I have organized the content in a way that it would be helpful to not only beginners also to advance user also. In this article, I will cover the fundamentals of State Management and Details of View State.
1 of 10
4/17/2013 4:04 PM
http://www.codeproject.com/Articles/31344/Beginner-s-Guide-To-View-...
Top News
A Tour of C++ Component Extensions
Get the Insider News free each morning.
As given in above pages, page is recreated before its comes to clients and happened for each and every request. So it is a big issue to maintain the state of the page and information for a web application. That is the reason to start concept of State Management. To overcome this problem ASP.NET 2.0 Provides some features like View State, Cookies, Session, Application objects etc. to manage the state of page. There are some few selection criteria to selected proper way to maintain the state, as there are many way to do that. Those criteria are: How much information do you need to store? Does the client accept persistent or in-memory cookies? Do you want to store the information on the client or on the server? Is the information sensitive? What performance and bandwidth criteria do you have for your application? What are the capabilities of the browsers and devices that you are targeting? Do you need to store information per user? How long do you need to store the information? Do you have a Web farm (multiple servers), a Web garden (multiple processes on one machine), or a single process that serves the application? So, when ever you start to think about state management, you should think about above criteria. based on that you can choose the best approaches for manages state for your web application.
Related Articles
ViewState: Various ways to reduce performance overhead Automatic ViewState Properties with the ViewState attribute DataGrid's ViewState optimization ViewState and Server.Transfer Best practices Save ViewState on the File System ViewState Compression Keep ASP.NET ViewState out of ASPX Page for Performance Improvement ASP.NET Internals: Viewstate and Page Life Cycle Access ViewState Across Pages Extend Repeater to support DataPager ViewState in Dynamic Control
GridView needs more plumbing code to support Calendar controls Sorting a GridView bound to a DataTable Retaining State for Dynamically Created Controls in ASP.NET applications ViewState and Postback How to make ViewState secure in ASP.NET Yet, another ViewState viewer Help! My ViewState Is Out Of Control Record Navigation using Events in c# Web Paging Control
2 of 10
4/17/2013 4:04 PM
http://www.codeproject.com/Articles/31344/Beginner-s-Guide-To-View-...
ViewState["Var"]=Count;
string Test=ViewState["TestVal"];
Sometimes you may need to typecast ViewState Value to retreive. As I give an Example to strore and retreive object in view state in the last of this article.
3 of 10
4/17/2013 4:04 PM
http://www.codeproject.com/Articles/31344/Beginner-s-Guide-To-View-...
The control never change The control is repopulated on every postback The control is an input control and it changes only of user actions.
ViewState["Value"] = MyControl.Text;
Now, Run you application, In Browser, RighClick > View Source , You will get the following section of code
Fig : View state stored in hidden field Now , look at the value. looks likes a encrypted string, This is Base64 Encoded string, this is not a encoded string. So it can easily be decoded. Base64 makes a string suitable for HTTP transfer plus it makes it a little hard to read . Read More about Base64 Encoding . Any body can decode that string and read the original value. so be careful about that. There is a security lack of view state.
//Create a simple class and make it as Serializable [Serializable] public class student { public int Roll; public string Name; public void AddStudent(int intRoll,int strName) { this.Roll=intRoll; this.Name=strName; } }
//Store Student Class in View State student _objStudent = new student(); _objStudent.AddStudent(2, "Abhijit"); ViewState["StudentObject"] = _objStudent; //Retrieve Student information view state student _objStudent; _objStudent = (student)ViewState["StudentObject"];
4 of 10
4/17/2013 4:04 PM
http://www.codeproject.com/Articles/31344/Beginner-s-Guide-To-View-...
Now Run your web application, You can view the details of View State Size along with control ID in Control Tree Section. Don't worry about "Render Size Byte" , this only the size of rendered control.
TextBox1.EnableViewState =false;
To turnoff the view state of entire page, we need to set EnableViewState to false of Page Directive as shown bellow.
Even you disable view state for the entire page , you will see the hidden view state tag with a small amount of information, ASP.NET always store the controls hierarchy for the page at minimum , even if view state is disabled. For enabling the same, you have to use the same property just set them as True as for example, for a single control we can enabled view state in following way,
Collapse | Copy Code
TextBox1.EnableViewState =true;
5 of 10
4/17/2013 4:04 PM
http://www.codeproject.com/Articles/31344/Beginner-s-Guide-To-View-...
Many of ASP.NET Programmers assume that this is an Encrypted format, but I am saying it again, that this is not a encrypted string. It can be break easily. To make your view state secure, There are two option for that, First, you can make sure that the view state information is tamper-proof by using "hash code". You can do this by adding "EnableViewStateMAC=true" with your page directive. MAC Stands for "Message Authentication Code"
A hash code , is a cryptographically strong checksum, which is calculated by ASP.NET and its added with the view state content and stored in hidden filed. At the time of next post back, the checksum data again verified , if there are some mismatch, Post back will be rejected. we can set this property to web.config file also. Second option is to set ViewStateEncryptionMode="Always" with your page directives, which will encrypt the view state data. You can add this in following way
Always, mean encrypt the view state always, Never means, Never encrypt the view state data and Auto Says , encrypt if any control request specially for encryption. For auto , control must call Page.RegisterRequiresViewStateEncryption() method for request encryption.
we can set the Setting for "EnableViewStateMAC" and ViewStateEncryptionMode" in web.config also.
Note : Try to avoid View State Encryption if not necessary , because it cause the performance issue.
That's all for view state. Hope you have enjoyed this article, please don't forget to give me your valuable suggestions. If anything need to update or changed please post your comments and please give me suggestion.
Reference
6 of 10
4/17/2013 4:04 PM
http://www.codeproject.com/Articles/31344/Beginner-s-Guide-To-View-...
MSDN Reference
History
Written on Saturday, 29th November, 2008 Small Correction on Monday December 2008
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
.NET Consultant | Former Microsoft MVP - ASP.NET | CodeProject MVP, Mentor, Insiders| Technology Evangelist | Author | Speaker | Geek | Blogger | Husband Blog : http://abhijitjana.net Web Site : http://dailydotnettips.com Twitter : @AbhijitJana My Kinect Book : Kinect for Windows SDK Programming Guide
Article Top
Like
Tweet
Rate this:
Poor
Excellent
Vote
Noise Medium
Per page 25
Update
Comment
Superb Article dude..u Rocks!!
Reply Email View Thread Permalink Bookmark
tarun _sharma
My vote of 5
Member 8391376
7 of 10
4/17/2013 4:04 PM
http://www.codeproject.com/Articles/31344/Beginner-s-Guide-To-View-...
Really awesome article on ViewState at one place with concise solution. Thx, -Rajan
Reply Email View Thread Permalink Bookmark
My vote of 5
Nice Article
Reply Email View Thread Permalink Bookmark
abhi3eee
My vote of 5
This is called knowledge
Reply Email View Thread Permalink Bookmark
abhi993
Good article
Sampath Sridhar
Thanks for the article. Able to get good understanding of Viewstate through this one.
Reply Email View Thread Permalink Bookmark
Abhinesh M
The article was very easy to understand and implement viewstate. Thanks.
Reply Email View Thread Permalink Bookmark
My vote of 4
Very well written
Reply Email View Thread Permalink Bookmark
Mukesh_B
My vote of 5
Very nice
Reply Email View Thread Permalink Bookmark
Uday P.Singh
5.00/5 (1 vote)
Nice Article
vinay raghavendr
Article is very good, good explanation with example. I look forward of articles on remaining types of state management.
Reply Email View Thread Permalink Bookmark
My vote of 5
Excellent. Thanks
Reply Email View Thread Permalink Bookmark
ZPop
5.00/5 (1 vote)
My vote of 5
ravikant.sharma01
great article and very easy and it made the view state my cup of tea
Reply Email View Thread Permalink Bookmark
8 of 10
4/17/2013 4:04 PM
http://www.codeproject.com/Articles/31344/Beginner-s-Guide-To-View-...
My vote of 5
good article for beiginners
Reply Email View Thread Permalink Bookmark
chaitanyasingh
5.00/5 (1 vote)
rajyash
1.00/5 (1 vote)
View State
Excellent tutorials
Reply Email View Thread Permalink Bookmark
rajyash
View State
Excellent Explanation
Reply Email View Thread Permalink Bookmark
Member 8164277
bluesathish
hi Abhijit Jana, thanks for your nice article. And i've the doubt that how we can clear (or) dispose the viewstate objects. Since its uses client side resources, we need to clear (or) dispose it when it is not in use. kindly share your suggestion for this. Regards, Bluesathish
Reply Email View Thread Permalink Bookmark
Comment
Abhijit Parab
My vote of 5
Its easily understable
Reply Email View Thread Permalink Bookmark
Swathi Nagaraj
my vote for 4
it is good article written by you.
Reply View Thread Permalink Bookmark
girishmeena
My vote of 4
It is good for starting learners....
Reply Email View Thread Permalink Bookmark
anilsarapati
My vote of 4
nagpalvikas
9 of 10
4/17/2013 4:04 PM
http://www.codeproject.com/Articles/31344/Beginner-s-Guide-To-View-...
Adding a scenario would have added cherry to the cake. It would have made the article even more interesting if you would have given a scenario where View state is beneficial.
Reply Email View Thread Permalink Bookmark
About an article
rahul7596
excellent Article It is really helpful.but please tell me where can i found your other articles of state management?
Reply Email View Thread Permalink Bookmark
My vote of 4
good one...
Reply View Thread Permalink Bookmark
S.K.Tripathi
My vote of 4
Very good Article....
Reply Email View Thread Permalink Bookmark
usrikanthvarma
My vote of 5
very good understanding is possible... great work
Reply Email View Thread Permalink Bookmark
Member 9334911
Refresh
1 2 3 4 5 Next
General
News
Suggestion
Question
Bug
Answer
Joke
Rant
Admin
Article Copyright 2008 by Abhijit Jana Everything else Copyright CodeProject, 1999-2013 Terms of Use
Permalink | Advertise | Privacy | Mobile Web02 | 2.6.130408.1 | Last Updated 29 Nov 2008
10 of 10
4/17/2013 4:04 PM