Then do you really know the TextChanged event of the ASP.net textbox above? Here we say how the state data is loaded.
Although there is a method of invoking state switching in control, there is a general GET request for the condition if (_controlstate >= controlstate.viewstateloaded) where the condition is not satisfied.
Copy Code code as follows:
Internal enum ControlState
{
Constructed,
Frameworkinitialized,
Childreninitialized,
Initialized,
Viewstateloaded,
Loaded,
Prerendered
}
We know this.controlstate = controlstate.frameworkinitialized in the ProcessRequest of page; In the ProcessRequestMain method, there is a call to This.initrecursive (null) after Init; In this method there is a sentence _controlstate = controlstate.initialized; There is such a base in the Loadallstate () method. Loadviewstaterecursive (second. Second), and loadviewstaterecursive in the _controlstate = controlstate.viewstateloaded This sentence with code, so we in the Page_Load when the dynamic condition control, if (_ ControlState >= controlstate.viewstateloaded) condition is established, as shown in figure:
So in the run THIS.FORM1.CONTROLS.ADD (TXT); this sentence before, txt value is demo1,
As shown in figure
But after the operation, it changed:
Of course here's txt. The text value is also the old value I posted last time, and the new value is rebind in the control's LoadPostData method. There is a very important judgment in the default Loadviewstaterecursive method
Copy Code code as follows:
internal void Loadviewstaterecursive (object savedstate) {
Nothing to does if we have no State
if (savedstate = null | | flags[disableviewstate])
Return
。。。。。。。
_controlstate = controlstate.viewstateloaded
}
You see I have a Custtextboxt:textbox control above, if we add a TextBox control directly, then here's txt. Text is always demo1, and whether the state data is loaded when the control is dynamically added is related to the preservation of state data. And the state data preservation is mainly saveviewstate completed, here I post the first time SaveViewState return data:
So the second time I can get the data from the last post.
The main methods related to SaveViewState are:
Copy Code code as follows:
public class Textbox:webcontrol, IPostBackDataHandler, IEditableTextControl {
protected override Object SaveViewState () {
if (savetextviewstate = = False) {
Viewstate.setitemdirty ("Text", false);
}
Return base. SaveViewState ();
}
private bool Savetextviewstate {
get {
//
Must be saved when
1. There is a registered event handler for SelectedIndexChanged
2. Control are not enabled or visible, because the browser ' s post data would not include this control
3. The instance is a derived instance, which might being overriding the OnTextChanged method
if (TextMode = = Textboxmode.password) {
return false;
}
if ((events[eventtextchanged]!= null) | |
(IsEnabled = false) | |
(Visible = false) | |
(ReadOnly) | |
(This. GetType ()!= typeof (TextBox)) {
return true;
}
return false;
}
}
}
public class Webcontrol:control, IAttributeAccessor {
protected override Object SaveViewState () {
Pair mystate = null;
Save values cached out of view state
if (_webcontrolflags[disableddirty]) {
viewstate["Enabled"] =!flags[iswebcontroldisabled];
}
if (controlstylecreated) {
The style shares the StateBag of its owner WebControl
Call SaveViewState to let style participate in state management
Controlstyle.saveviewstate ();
}
Object basestate = base. SaveViewState ();
Object astate = null;
if (attrstate!= null) {
Astate = Attrstate.saveviewstate ();
}
if (basestate!= null | | | astate!= NULL) {
MyState = new Pair (basestate, astate);
}
return mystate;
}
}
public class Control:icomponent, IParserAccessor, IUrlResolutionService, Idatabindingsaccessor, Icontrolbuilderaccessor, Icontroldesigneraccessor, iexpressionsaccessor {
Protected virtual Object SaveViewState () {
Save values cached out of view state
if (Flags[visibledirty]) {
viewstate["Visible"] =!flags[invisible];
}
if (Flags[validaterequestmodedirty]) {
viewstate["Validaterequestmode"] = (int) Validaterequestmode;
}
if (_viewstate!= null)
return _viewstate.saveviewstate ();
return null;
}
}
Public sealed class Statebag:istatemanager, IDictionary {
Internal Object SaveViewState () {
ArrayList data = null;
if (bag. Count!= 0) {
IDictionaryEnumerator e = bag. GetEnumerator ();
while (E.movenext ()) {
Stateitem item = (Stateitem) (E.value);
if (item. IsDirty) {
if (data = = NULL) {
data = new ArrayList ();
}
#if ObjectStateFormatter
Data. ADD (New Indexedstring ((string) e.key));
#else
Data. ADD (E.key);
#endif
Data. ADD (item. Value);
}
}
}
return data;
}
}
Here we know that the Save state information is mainly in the StateBag SaveViewState method, here is a check if (item. IsDirty), there is a judgment in the SaveViewState method of the TextBox
Copy Code code as follows:
if (savetextviewstate = = False) {
Viewstate.setitemdirty ("Text", false);
}
Related to its Savetextviewstate property.
Then we can conclude that the dynamically created control defaults to the loader state data when it is added, and if it is statically added data that is loadallstate to handle the loading of the state data. The loading of state data is closely related to the saveviewstate of the control, and there is no state information to load if the method's return value is null and has no state information.