|
|||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
Table of Contents
IntroductionFirst 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. What is state management?Web is 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 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:
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. Different types of state management?There are two different types of state management:
Client Side state management does not use any server resource , it store information using client side option. Server Side state management use server side resource for store data. Selection of client side and server side state management should be based on your requirements and the selection criteria that are already given. What is view state?
Example: If you want to add one variable in View State, ViewState["Var"]=Count; For Retrieving information from View State 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. Advantages of view state?This are the main advantage of using View State:
Disadvantages of view state?This are the main disadvantages of using View State:
When we should use view state?I already describe the criteria of selecting State management. A few point you should remember when you select view state for maintain your page state.
When we should avoid view state?You won't need view state for a control for following cases,
Where is view state stored?View State stored the value of page controls as a string which is hashed and encoded in some hashing and encoding technology. It only contain information about page and its controls. Its does not have any interaction with server. It stays along with the page in the Client Browser. View State use Suppose you have written a simple code , to store a value of control: ViewState["Value"] = MyControl.Text;
Now, Run you application, In Browser,
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 How to store object in view state?We can store an object easily as we can store string or integer type variable. But what we need ? we need to convert it into stream of byte. because as I already said , view state store information in hidden filed in the page. So we need to use Just take as example, //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;
}
}
Now we will try to store object of " //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"];
How to trace your view state information?If you want to trace your view state information, by just enable "
Now Run your web application, You can view the details of View State Size along with control ID in
Fig : View State Details
Enabling and Disabling View StateYou can enable and disable View state for a single control as well as at page level also. To turnoff view state for a single control , set TextBox1.EnableViewState =false;
To turnoff the view state of entire page, we need to set
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, TextBox1.EnableViewState =true;
and for a page level,
How to make view state secure?As I already discuss View state information is stored in a hidden filed in a form of
Fig : View state stored in hidden field
Many of ASP.NET Programmers assume that this is an
A
It
we can set the Setting for "
Some Important Points
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. ReferenceHistoryWritten on Saturday, 29th November, 2008 Small Correction on Monday December 2008
|
||||||||||||||||||||||||||||||||||||||||