Do you know the TextChanged event of Asp.net TextBox above? Here we will talk about how to load status data.
Although there is a method to call state transfer in Control, there is a judgment condition here. if (_ controlState> = ControlState. ViewStateLoaded) General get requests do not meet the conditions here.
Copy codeThe Code is as follows:
Internal enum ControlState
{
Constructed,
FrameworkInitialized,
ChildrenInitialized,
Initialized,
ViewStateLoaded,
Loaded,
PreRendered
}
We know that this. controlState = ControlState. frameworkInitialized; In the ProcessRequestMain method, this is called after Init. initRecursive (null); In this method, there is such a sentence _ controlState = ControlState. initialized;, there is such a base in the LoadAllState () method. loadViewStateRecursive (second. second); and In LoadViewStateRecursive, _ controlState = ControlState. viewStateLoaded contains code. Therefore, when we use the dynamic condition control in Page_load, if (_ controlState> = ControlState. viewStateLoaded) condition is true,
Therefore, before running this. form1.Controls. Add (txt);, the txt value is demo1,
But it changes after running:
Of course, the txt. Text value here is also the old value from my last post. The new value is rebound in the LoadPostData method of the control. The default LoadViewStateRecursive method has an important judgment.
Copy codeThe Code is as follows:
Internal void LoadViewStateRecursive (object savedState ){
// Nothing to do if we have no state
If (savedState = null | flags [disableViewState])
Return;
.......
_ ControlState = ControlState. ViewStateLoaded
}
We can see that the above is a CustTextBoxt: TextBox Control. If we directly add a TextBox Control, that's the txt here. text is always demo1. It can be seen that whether the State data is loaded when the control is dynamically added is related to the storage of the State data. The State data is saved mainly by the SaveViewState. Here, the SaveViewState returned data during the first post:
Therefore, the data from the last post can be retrieved for the second time.
The methods related to SaveViewState mainly include:
Copy codeThe Code is 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 is not enabled or visible, because the browser's post data will not include this control
// 3. The instance is a derived instance, which might be 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 particle 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 storage status information is mainly in the SaveViewState method of StateBag. Here there is a check if (item. IsDirty), and there is a judgment in the SaveViewState method of TextBox.
Copy codeThe Code is as follows:
If (SaveTextViewState = false ){
ViewState. SetItemDirty ("Text", false );
}
It is related to its SaveTextViewState attribute.
We can summarize that the dynamically created control is the loader status data when it is added by default. If it is static added data, it is LoadAllState to process the loading of status data. The loading of State data is closely related to the SaveViewState of the control. If the return value of this method is null, no state information is required.