This note is intended for those who do not know what ControlState is and how to use it. If you are familiar with this technique of saving the state of the page, you can safely skip this topic.
All asp.net programmers are aware of ViewState technology, which offers a default method for maintaining the state of web pages and controls. I’m not going to talk about pros and cons here, and without it, it’s known that ViewState is a double-edged sword, on one side is convenience, and on the other is a huge amount of generated pages (for example, if you turn on ViewState for a GridView) . It just so happened that one of the main mechanisms of asp.net during development often turns off for the sake of performance.
Normally, ViewState is disabled for the entire page simply by specifying
<% Page EnableViewState = "false"%>
The benefit in this case is significant, the pages will contain only markup (well, plus, asp.net binding) and besides, no control will be guaranteed to generate ViewState-data.
There is one thing, but turning off ViewState, you turn it off completely and, for example, the following code that works with ViewState and described in this article about snippets
habrahabr.ru/blog/net/41790.html just will not work as it should.
public int MyProperty
{
get
{
object o = ViewState [ "MyProperty" ];
return (o! = null )? ( int ) o: 0;
}
set
{
ViewState [ "MyProperty" ] = value ;
}
} * This source code was highlighted with Source Code Highlighter .
Meanwhile, there is a way to use ViewState with EnableViewState = "false". This technique is called ControlState. And if ViewState can be turned off, then ControlState is not. Consider an example:
[ Serializable ]
struct PageStateStruc
{
public FormState CurrentState;
public int ? CurrentResume;
public int CurrentSection;
}
PageStateStruc PageState;
')
protected override void OnInit ( EventArgs e)
{
Page .RegisterRequiresControlState ( this );
base .OnInit (e);
}
protected override object SaveControlState ()
{
return PageState;
}
protected override void LoadControlState ( object savedState)
{
PageState = (PageStateStruc) savedState;
} * This source code was highlighted with Source Code Highlighter .
Here is defined a certain data structure PageStateStruc, an instance of which we will save. Notice the Serializable attribute, which is required for the structure used. An instance of the PageState structure is immediately defined.
Next, in the OnInit handler, the RegisterRequiresControlState method is called, which registers the current page as a control whose controlState should be saved. Instead of this, you can specify any user control. It is recommended to call the RegisterRequiresControlState in OnInit.
Two overloaded methods SaveControlState and LoadControlState, which, as is seen, simply load and unload the necessary data complete the picture. Calling these methods asp.net takes over.
It's all! PageState data between requests for the page will be automatically saved. In fact, to save data between page calls, we only need to determine the type of data and describe how this data needs to be stored / loaded. The rest is done by asp.net.
Afterword
I recommend disabling ViewState page-wide. In cases where you need to save some data, use ControlState. However, if this data is just the value of a single variable or property, then it would be wiser to use the HiddenField control. Several experiments have shown that the markup that HiddenField will generate for one field will take less space than the ViewState line, which the ControlState mechanism will generate for the same field.