佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > asp.net 動(dòng)態(tài)創(chuàng)建TextBox控件及狀態(tài)數(shù)據(jù)如何加載

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

熱門(mén)標(biāo)簽:電子圍欄 科大訊飛語(yǔ)音識(shí)別系統(tǒng) 銀行業(yè)務(wù) Linux服務(wù)器 Mysql連接數(shù)設(shè)置 阿里云 團(tuán)購(gòu)網(wǎng)站 服務(wù)器配置

接著上文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)寫(xiě)入服務(wù)器端控件
  • asp.net 動(dòng)態(tài)生成控件并獲取其值
  • ASP.NET動(dòng)態(tài)添加用戶控件的方法

標(biāo)簽:大理 廣元 蚌埠 衡水 衢州 江蘇 棗莊 萍鄉(xiāng)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《asp.net 動(dòng)態(tài)創(chuàng)建TextBox控件及狀態(tài)數(shù)據(jù)如何加載》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    洛川县| 玉林市| 巫山县| 济南市| 弥勒县| 三台县| 平顶山市| 四川省| 新昌县| 屯留县| 兴安县| 西乌| 云和县| 孝义市| 东至县| 定远县| 南投市| 兰考县| 长顺县| 阜新| 收藏| 丰城市| 玉环县| 广饶县| 南陵县| 临安市| 曲阳县| 浙江省| 天祝| 宜兰县| 巴里| 渝中区| 互助| 德昌县| 句容市| 衡水市| 青神县| 永嘉县| 长寿区| 皮山县| 沁源县|