Basic idea
Accuse a state - to let control a regular job, need memory to dominate a condition data sometimes. For example, if wrote one each,the definition accuses, have the different option card that shows different message among them, should accuse to make work like anticipate, accusing a need to know what choose in process going there and back is which option card. ViewState attribute is usable hereat purpose, but development staff may close view position in page level, interrupt effectively thereby accuse. To solve this problem, ASP.NET page frame calls the new function that controls a state one kind publicly in ASP.NET 2.0 edition.
ControlState attribute allows to keep specific at accusing property information, unlike ViewState attribute can be shut euqally. If want to use,accuse a state, accuse RegisterRequiresControlState method must be called in initialization process, rescript next SaveControlState and LoadControlState method.
View status - view status is ASP.NET page frame use at saving the page between process going there and back and the method that control a cost below acquiescent circumstance. When the HTML form that presents a page, the string that the current condition of the page that in needing to answering hair process, withholds and value will be coded by Base64 of alignment melt into, output view status in concealing a field. The PageStatePersister that defines oneself through coming true kind with memory page data, you can change acquiescent behaviour and view position memory reachs another position (like SQL Server database) . About flow and storing page condition is not the give typical examples in hidden page field, refer to the give typical examples of mechanism of view status endurance please.
The code that the ViewState property that you can carry use page saves the data in process going there and back Web server to use his visits view state. ViewState attribute is one includes close key / the value is right (include view status data among them) dictionary.
Respective advantage and inferior position
View status
The advantage of use view status:
· does not need status of any server resource view to include the construction inside page code in.
· realizes simple view status to need not be used any define process designing oneself. Be opposite below acquiescent circumstance accuse a maintenance that enables condition data.
The value in the position of safe function view that · strengthens is calculated through Ha Xi and compress, and be aimed at Unicode implementation have code, its security wants prep above to use conceal region.
The defect of use view status
· function note is in as a result of view status memory page itself, so if the value with greater memory, the user shows page and the speed when dispatching a page may be decelerated. Be opposite especially mobile equipment, its bandwidth is finite normally.
· equipment restricts mobile equipment to may do not have sufficient memory capacity to store much view status data.
The condition of safe risk view with potential · stores on the page one or more conceal inside the country. Although view status hopes with breathing out,the format stores data, but it can be distorted. If examine a page to output a source directly, can see conceal inside the country information, this causes potential security problem.
Accuse a state
Use the good point that charges a position:
· does not need circumstances of any server resource acquiesce to fall, those who accuse a condition memory to go up in the page conceal inside the country.
Because · dependability accuses a status of condition unlike view to be able to be shut in that way, accusing a state is management accuses the more reliable method of condition.
· versatility can be written from the definition adapter is controlled how to store dominate a condition data and the memory niche that dominate a condition data.
Use the weakness that accuses a state:
Although ASP.NET page frame provided a basis to accuse a state,· needs a few process designing, but the condition that accusing a state is one each definition holds a mechanism. Accuse a state to make full use of, you must write code to save accuse a state with to load.
Accuse a condition and view status give typical examples
This give typical examples demonstrates what the name is IndexButton how to found accuse from the definition, should accuse use accuse a condition to safeguard crucial position information between many pages request. A condition and view state accuse in what introduce in ASP.NET 2.0 edition similar, but the independence on the function at view status. Webpage development staff may stem from function reason and ban use whole page or individual accuse view status, but they cannot be banned with accuse a state. Accusing a state is to be memory to accuse only important data (if a page accuses the page is counted) and of the design, time these data talent must be used when hair make control a regular job (even if is banned with view status insusceptible also) . Below acquiescent circumstance, ASP.NET page frame will accuse a condition memory to obscure an element in a of the page in, view status stores as much in obscuring an element here. Although ban,use view position, or it is use Session when running state, the page accuses a condition mediumly to still can be transmitted carry to the client, return a server next. Answering when hair, ASP.NET can undertake to concealing elemental content alignment is changed turning over, will accuse a condition to load to had registered those who accuse a state to accuse to every in.
Illuminate of this give typical examples be in what state saves in accusing a condition and view state to accuse from the definition at the same time. Give typical examples is here medium, indexButton accuses a derive from Button kind, still defined an Index attribute, in protecting this property existence to accuse a state. To undertake comparative, indexButton still defined an IndexInViewState attribute, this attribute memory is in ViewState dictionary. Accuse the difference between a condition and view status for understanding, use the program with accessary article to demonstrate IndexButton to accuse please.
IndexButton controls a source
Using System;Using System.ComponentModel;Using System.Security.Permissions;Using System.Web;Using System.Web.UI;Using System.Web.UI.WebControls;
Namespace CustomerControls
{
{
AspNetHostingPermission(SecurityAction.Demand, level = AspNetHostingPermissionLevel.Minimal) ,
AspNetHostingPermission(SecurityAction.InheritanceDemand, level = AspNetHostingPermissionLevel.Minimal) ,
ToolboxData("<{0}:INdexButton Runat=\” Server\ “></{0}:INdexButton>" )
NdexButton>” )
Public Class IndexButton: Button
{
Private Int IndexValue;
Private Int IndexValue;
Bindable(true) ,
Category(”Behavior” ) ,
DefaultValue(0) ,
Description(”The Index Stored In Control State. Description(”The Index Stored In Control State.. Description(”The Index Stored In Control State…
Description(”The Index Stored In Control State….
Public Int Index
{
Get
{
Return IndexValue;
}
Set
{
IndexValue = Value;
}
}
}
Bindable(true) ,
Category(”Behavior” ) ,
DefaultValue(0) ,
Description(”The Index Stored In View State. Description(”The Index Stored In View State.. Description(”The Index Stored In View State…
Description(”The Index Stored In View State….
Public Int IndexInViewState
{
Get
{
Object Obj = ViewState[”IndexInViewState” ];
Return (obj==Null) ? 0: (Int)obj;
}
Set
{
ViewState[”IndexInViewState” ] = Value;
}
}
Protected Override Void OnInit(EventArgs E)
{
Base.OnInit(e);
Page.RegisterRequiresControlState(this);
}
Protected Override Object SaveControlState()
{
/ / call base kind method, from base kind get accusing a state base value
/ / if IndexValue differs Yubingjuji kind accuse a condition to not be Null
/ / use Pair comes as convenience data structure efficient save (and reductive in LoadControlState method)
/ / accuse a state by what two parts comprise
Object Obj = Base.SaveControlState();
If (indexValue! = 0)
{
If (obj! = Null)
{
Return New Pair(obj, indexValue);
}
Else
{
Return (indexValue);
}
}
Else
{
Return Obj;
}
}
Protected Override Void LoadControlState(object State)
{
If (state! = Null)
{
Pair P = State As Pair;
If (p! = Null)
{
Base.LoadControlState(p.First);
IndexValue = (int)p.Second;
}
Else
{
If (state Is Int)
{
IndexValue = (int)state;
}
Else
{
Base.LoadControlState(state);
}
}
}
}
}
}
Code discussion
IndexButton accuses implementation illuminate 3 tasks, must carry out ability of these 3 tasks to make accuse participate in accuse a state:
·Rescript OnInit method calls RegisterRequiresControlState method to be registered to the page, accuse a state in order to participate in. Must request to finish this job in the light of every.
·Rescript SaveControlState method, with data is being saved in accusing a state.
·Rescript LoadControlState method, with from dominate a data of condition to load. This method is called base kind method, get base kind to accusing a state base value. If IndexValue field is 0, and base kind accuse a condition to also be empty, pair kind can use as convenient data structure, use save and reductive accuse a state by what two parts comprise.
Analytic summary
From MSDN a series of in light of technical reference, controlState should be to basically be in accuse from the definition on use, “ASP.NET page frame offerred ControlState attribute to regard the means that charges a data from the definition in the memory in server process going there and back as ” , this is MSDN those who go up is former sentence, ASP.NET2.0 just provided a basis for ControlState, when ControlState the condition that is one each definition holds a mechanism, the mechanism that holds position that is to say needs you to develop him personnel to finish, and unlike ViewState, it has its acquiescent condition to hold a mechanism. Be in accusing an use ControlState from the definition just perhaps is Microsoft original idea, be avoid to be banned in page level after using ViewState, accuse from the definition still can move normally. Of course the meaning here is, certain accuse moving correctly is the state information that relies on it, in ASP.NET1.1, if ban,used ViewState, such accusing cannot move correctly. But after introducing ControlState, differred, because ControlState is,ban use what do not drop.
So Microsoft just reminds development personnel “ is opposite those is in to answer only please in sending a process to controlling an a few crucial keys data is used accuse a state, and do not want what will accuse a condition regard view as condition to reserve option uses ” . Speak clearly, controlState and ViewState are two things completely, although they can finish same job, new the ControlState both neither that roll out is to be used replacing ViewState also is not to use the reserve that makes ViewState. Its mission is the job that the place that makes up for ViewState cannot finish, development of personnel of hair of get out of the way goes more hale accuse. Say for example, of development accusing an a certain state from the definition is crucial, lack it to accuse from the definition cannot work normally, so ControlState entered the court with respect to this. And ControlState is the condition that defines oneself holds a mechanism, also restricted the use of ControlState freedom, you not only should call face of RegisterRequiresControlState method Xiang Ye to register in OnInit method, and should rescript SaveAdapterControlState() , two him methods go to LoadAdapterControlState(object State) what should implementation save, how to save. According to my present understanding, if you need to save this to accuse 10 kinds of different position, then you had be toed save one by one, another goes on one to load. The original intention that also saw Microsoft from this bit, is that apparent, if do not need ControlState,that does not use it, otherwise how does let us develop personnel to do?
This is a foundation only, a moment ago I said, it seems that Microsoft also is so those who say, what ControlState is aimed at is accuse from the definition, actually we should go really open accuses basically for example Label accuses ControlState, microsoft also allows, this is a bit deep content, this is involved accused an adapter (ControlAdapter) . If need to understand the content of this respect, look please with accuse an adapter open basic ControlState. Http://sifang2004.cnblogs.com/archive/2006/06/01/415288.html
Appendix
To understand the content above more adequately, need has a knowledge to the following content:
Pair kind
Use as memory the basic structure of two relevant objects. It is to be in whole ASP.NET (in managing task period occasional to configure a process that handles an order like page condition) the economic tool that has a variety of use kind. The aleatoric position of the structure that can need to include two relevant boy or girl friends in oneself code and data of not certain need bind decided position to use Pair kind. Pair kind do not cite its object First and Second are enclosed in attribute; This kinds serve as them directly communal kind the field publishs code.
Pair kind there are a variety of uses in implementation of page condition reservation. The commonnest use is to serve as ViewState and ControlState aggregate container at the same time. Below this kind of circumstance, first attribute is used at ViewState, and Second is used at ControlState.
PageStatePersister kind
HTTP requests and be being answered do not have condition originally. Want to carry condition message between HTTP request, ASP.NET server page can store Page condition. This condition calls view position, it is included page and dominate a setting and data, these settings and data make page and accuse when resembling referring them the server to return a client to carry again next last time it seems that, user place sees and to it interactive page and accuse same. A few kinds of mechanisms can be in between the successive request of pair of identical pages memory view status. PageStatePersister is abstract kind represent mechanisms of these state information memory base kind.
Want to carry reservation view state in the client that cannot sustain mechanism of endurance of existing view status, OK and patulous PageStatePersister kind, the method of view status endurance that introduces you oneself, the type that and can use page adapter is configuration of ASP.NET application process the client end that offers a page according to be its uses different view status endurance mechanism. From PageStatePersister kind of derive kind must rescript Save abstract method, so that store in endurance medium view status is mixed,accuse a state, rescript at the same time Load method in order to collect state information. If want to know how to write the derive of PageStatePersister kind, ask mechanism of endurance of referenced view status.
