国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > .NET > 正文

asp.net 動(dòng)態(tài)創(chuàng)建TextBox控件及狀態(tài)數(shù)據(jù)如何加載

2020-01-18 00:22:29
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

接著上文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)信息了。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 湖州市| 赣榆县| 酒泉市| 迁西县| 江城| 乐东| 疏附县| 仪陇县| 溧阳市| 玛纳斯县| 耒阳市| 平塘县| 塔城市| 洪雅县| 六枝特区| 潞城市| 郴州市| 贵阳市| 大兴区| 江源县| 孝义市| 门源| 阳高县| 五家渠市| 湄潭县| 呼伦贝尔市| 涿州市| 松原市| 临澧县| 远安县| 乐清市| 大同县| 夏津县| 寿阳县| 长武县| 阜城县| 偏关县| 广平县| 禄劝| 卢氏县| 营口市|