接著上文Asp.net TextBox的TextChanged事件你真的清楚嗎? 這里我們來(lái)說(shuō)說(shuō)狀態(tài)數(shù)據(jù)時(shí)如何加載的。
雖然在Control中有調(diào)用狀態(tài)轉(zhuǎn)存的方法,但是這里有一個(gè)判斷條件 if (_controlState >= ControlState.ViewStateLoaded) 一般的get請(qǐng)求這里的條件是不滿足的。
復(fù)制代碼 代碼如下:
internal enum ControlState
{
Constructed,
FrameworkInitialized,
ChildrenInitialized,
Initialized,
ViewStateLoaded,
Loaded,
PreRendered
}
我們知道在page的ProcessRequest中this.ControlState = ControlState.FrameworkInitialized;ProcessRequestMain方法中在Init后有調(diào)用this.InitRecursive(null);在這個(gè)方法里面有這么一句_controlState = ControlState.Initialized;,在LoadAllState()方法中有這么一句 base.LoadViewStateRecursive(second.Second);,而LoadViewStateRecursive中又有_controlState = ControlState.ViewStateLoaded這句帶代碼,所以我們?cè)赑age_load中動(dòng)態(tài)條件控件時(shí), if (_controlState >= ControlState.ViewStateLoaded)條件成立,如圖:
所以在運(yùn)行this.form1.Controls.Add(txt);這句以前,txt的值為demo1,
如圖
但是運(yùn)行以后之就發(fā)生變化了:
當(dāng)然這里的txt.Text值也是我上次post過(guò)來(lái)的舊值,新值是在控件的LoadPostData方法中重新綁定。在默認(rèn)的LoadViewStateRecursive方法中有一個(gè)很重要的判斷
復(fù)制代碼 代碼如下:
internal void LoadViewStateRecursive(object savedState) {
// nothing to do if we have no state
if (savedState == null || flags[disableViewState])
return;
。。。。。。。
_controlState = ControlState.ViewStateLoaded
}
大家看到我上面是一個(gè)CustTextBoxt : TextBox控件,如果我們直接添加TextBox控件的話,那么著這里的txt.Text一直都是demo1,可見(jiàn)控件動(dòng)態(tài)添加的時(shí)候是否加載狀態(tài)數(shù)據(jù)與狀態(tài)數(shù)據(jù)的保存有關(guān)。而狀態(tài)數(shù)據(jù)的保存主要就是SaveViewState完成的,這里我第一次post的時(shí)候SaveViewState返回?cái)?shù)據(jù):
所以第二次能取到上次post過(guò)來(lái)的數(shù)據(jù)。
其中與SaveViewState有關(guān)的方法主要有:
復(fù)制代碼 代碼如下:
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 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;
}
}
到這里我們知道保存狀態(tài)信息主要是在StateBag 的SaveViewState方法中,這里有一個(gè)檢查 if (item.IsDirty) ,在TextBox的SaveViewState方法中有一個(gè)判斷
復(fù)制代碼 代碼如下:
if (SaveTextViewState == false) {
ViewState.SetItemDirty("Text", false);
}
與它的SaveTextViewState 屬性有關(guān)。
那么我們可以總結(jié)一下:動(dòng)態(tài)創(chuàng)建的控件默認(rèn)是在被添加的時(shí)候加載器狀態(tài)數(shù)據(jù),如果是靜態(tài)添加的數(shù)據(jù)那就是LoadAllState來(lái)處理狀態(tài)數(shù)據(jù)的加載。狀態(tài)數(shù)據(jù)的加載與控件的SaveViewState密切相關(guān),如果該方法的返回值為null既沒(méi)有狀態(tài)信息,那也不需要加載什么狀態(tài)信息了。
您可能感興趣的文章:- ASP.NET動(dòng)態(tài)添加控件一例
- asp.net 動(dòng)態(tài)添加多個(gè)用戶控件
- asp.net(C#) 動(dòng)態(tài)添加非ASP的標(biāo)準(zhǔn)html控件(如添加Script標(biāo)簽)
- asp.net動(dòng)態(tài)加載自定義控件的方法
- ASP.NET 頁(yè)面中動(dòng)態(tài)增加的控件、添加事件
- ASP.net 動(dòng)態(tài)加載控件時(shí)一些問(wèn)題的總結(jié)
- ASP.NET 動(dòng)態(tài)寫入服務(wù)器端控件
- asp.net 動(dòng)態(tài)生成控件并獲取其值
- ASP.NET動(dòng)態(tài)添加用戶控件的方法