asp.net 動態創建TextBox控件 如何加載狀態信息

接著上文Asp.net TextBox的TextChanged事件你真的清楚嗎? 這里我們來說說狀態數據時如何加載的。
雖然在Control中有調用狀態轉存的方法,但是這里有一個判斷條件 if (_controlState >= ControlState.ViewStateLoaded)? 一般的get請求這里的條件是不滿足的。

internal enum ControlState
{
??? Constructed,
??? FrameworkInitialized,
??? ChildrenInitialized,
??? Initialized,
??? ViewStateLoaded,

??? Loaded,
??? PreRendered
}

我們知道在page的ProcessRequest中this.ControlState = ControlState.FrameworkInitialized; ProcessRequestMain方法中在Init后有調用this.InitRecursive(null);在這個方法里面有這么一句_controlState = ControlState.Initialized;,在LoadAllState()方法中有這么一句?base.LoadViewStateRecursive(second.Second);,而LoadViewStateRecursive中又有_controlState = ControlState.ViewStateLoaded這句帶代碼,所以我們在Page_load中動態條件控件時, if (_controlState >= ControlState.ViewStateLoaded)條件成立,如圖:

所以在運行this.form1.Controls.Add(txt);這句以前,txt的值為demo1,

如圖


但是運行以后之就發生變化了:

當然這里的txt.Text值也是我上次post過來的舊值,新值是在控件的LoadPostData方法中重新綁定。在默認的LoadViewStateRecursive方法中有一個很重要的判斷

??????? internal void LoadViewStateRecursive(object savedState) {
??????????? // nothing to do if we have no state
??????????? if (savedState == null || flags[disableViewState])
??????????????? return;?

。。。。。。。

_controlState = ControlState.ViewStateLoaded
?? ??? ??? ?}

大家看到我上面是一個CustTextBoxt : TextBox控件,如果我們直接添加TextBox控件的話,那么著這里的txt.Text一直都是demo1,可見控件動態添加的時候是否加載狀態數 據與狀態數據的保存有關。而狀態數據的保存主要就是SaveViewState完成的,這里我第一次post的時候SaveViewState返回數據:

所以第二次能取到上次post過來的數據。

其中與SaveViewState有關的方法主要有:

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 methodif (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 stateif (_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 stateif (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 OBJECTSTATEFORMATTERdata.Add(new IndexedString((string)e.Key));
#elsedata.Add(e.Key);
#endifdata.Add(item.Value);}}}return data;}}

到這里我們知道保存狀態信息主要是在StateBag SaveViewState方法中,這里有一個檢查? if (item.IsDirty) ,在TextBox的SaveViewState方法中有一個判斷

??? if (SaveTextViewState == false) {
??????????????? ViewState.SetItemDirty("Text", false);
??????????? }?

與它的SaveTextViewState 屬性有關。

那么我們可以總結一下:動態創建的控件默認是在被添加的時候加載器狀態數據,如果是靜態添加的數據那就是LoadAllState來處理狀態數據的加載。狀態數據的加載與控件的SaveViewState密切相關,如果該方法的返回值為null既沒有狀態信息,那也不需要加載什么狀態信息了

轉載于:https://www.cnblogs.com/majiang/archive/2012/12/11/2813072.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/274441.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274441.shtml
英文地址,請注明出處:http://en.pswp.cn/news/274441.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

從零實現一個迷你 Webpack

大家好,我是若川。我持續組織了近一年的源碼共讀活動,感興趣的可以 點此掃碼加我微信 lxchuan12 參與,每周大家一起學習200行左右的源碼,共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試…

ios 刷新遮罩遮罩_在Adobe XD中進行遮罩的3種方法

ios 刷新遮罩遮罩Are you new to Adobe XD? Or maybe you’re just stuck on how to create a simple mask? Here are 3 quick tips for how to mask your photos and designs in Adobe XD.您是Adobe XD的新手嗎? 或者,也許您只是停留在如何創建簡單的…

C#除法運算

C#中進行算術運算容易牽扯到類型的自動轉換,這種自動轉換稱之為隱式轉換,當然還可以人為的強制轉換 隱式轉換要求:不丟失精度,而且轉換前后都為數值 強制轉換:容易丟失可能會丟失精度 1 namespace 除法運算2 {3 cl…

Vite 4.0 正式發布!

源碼共讀我新出了:第40期 | vite 是如何解析用戶配置的 .env 的鏈接:https://www.yuque.com/ruochuan12/notice/p40也可以點擊文末閱讀原文查看,歡迎學習記筆記~12 月 9 日,Vite 4.0 正式發布。下面就來看看 Vite 4.0 有哪些更新吧…

代碼復審

我們CodingCook復審的是WWW的代碼,他們的項目是時間管理助手(TimeLine)。只是跟根據自己的經驗來看,不一定準 先說一下整體的感覺。WWW的代碼用了應該是比較符合面向對象的思想,借口,封裝隨處可見&#xff…

圖像標注技巧_保護互聯網上圖像的一個簡單技巧

圖像標注技巧補習 (TUTORIAL) Have you ever worried about sharing your images on the Internet? Anytime you upload something to the web you risk the chance of your work being used (without permission) by another.您是否曾經擔心過要在Internet上共享圖像&#xf…

【VueConf 2022】尤雨溪:Vue的進化歷程

大家好,我是若川。我持續組織了近一年的源碼共讀活動,感興趣的可以 點此掃碼加我微信 lxchuan12 參與,每周大家一起學習200行左右的源碼,共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試…

WCF netTcpBinding寄宿到IIS7

config配置文件不多說 <?xml version"1.0" encoding"utf-8" ?> <configuration><system.serviceModel><behaviors><serviceBehaviors><behavior name"myBehavior"><serviceMetadata/></behavior…

ar軟件測試工具_如何為用戶測試制作快速的AR原型

ar軟件測試工具We had a project recently with an element of AR-based interaction, which it turned out was impossible to create as a prototype in either Invision or Framer (our current stack). This had a massive impact on our ability to test with users in a …

未來ui設計的發展趨勢_2025年的未來UI趨勢?

未來ui設計的發展趨勢Humans are restless.人類是不安的。 Once we find something that works, we get used to it and we crave the next big thing. The next innovation. When will the future finally arrive? And when it does, how long will it take us to get used …

內存泄露檢測 vld

VLD是一款開源檢測內存泄露軟件的簡稱&#xff1a;Visual Leak Detector 網站&#xff1a;http://vld.codeplex.com/ 使用&#xff1a; 1. 安裝vld 或者 下載相關 .h&#xff0c;lib&#xff0c;dll 文件 2. 方法很簡單&#xff0c;只要在包含入口函數的.cpp文件中包含vld.h就可…

Monorepo 在網易的工程改造實踐

大家好&#xff0c;我是若川。我持續組織了近一年的源碼共讀活動&#xff0c;感興趣的可以 點此掃碼加我微信 lxchuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試…

這一年,Vue.js 生態開源之旅帶給我很大收獲~

大家好&#xff0c;我是若川。我持續組織了近一年的源碼共讀活動&#xff0c;感興趣的可以 點此掃碼加我微信 lxchuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試…

CSSyphus:煩躁不安的煩惱設計指南。

I’m trapped at home with my website. Or maybe it’s trapped at home with me. While some are using the weird lump of time provided by lockdown to indulge in baking, dancing, painting, singing, I’m using it to play around with code.我 被自己的網站困在家里。…

重構與臭豆腐4

重構要繼續&#xff0c;臭豆腐要做。   這個重構中各種提取類&#xff0c;方法&#xff0c;字段&#xff0c;可以方便的理解&#xff0c;如果使用了設置模式就更加邏輯清晰了。切東西也要講究刀法的。 重構可以方便的使用設計模式。設計模式為重構提供了目標。 比如多個if 可…

你構建的代碼為什么這么大?如何優化~

大家好&#xff0c;我是若川。我持續組織了近一年的源碼共讀活動&#xff0c;感興趣的可以 點此掃碼加我微信 lxchuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試…

用戶體驗需求層次_需求和用戶體驗

用戶體驗需求層次Shortly after the start of 2020 I led the development of a new website, and it went live in August. A week before the deployment, I paused development and took a step back in order to write about the project. Taking that pause, that step ba…

VMwareWorkstation設置U盤啟動(或U盤使用)

最近在工作中&#xff0c;經常要和LINUX部署打交道&#xff0c;一般在生產環境部署之前需要在自己的機器上進行測試。比如使用U盤安裝操作系統等。 在機器上安裝了VMware Workstation9.0&#xff0c;運行多個測試虛擬機。理由所當然的要使用此做一些操作系統部署&#xff0c;…

類從未使用_如果您從未依賴在線銷售,如何優化您的網站

類從未使用初學者指南 (A beginner’s guide) If you own a small business with a store front, you might have never had to rely on online sales. Maybe you’re a small clothing store or a coffee shop. You just made that website so people could find you online, …

狼書三卷終大成,狼叔親傳Node神功【留言送書】

大家好&#xff0c;我是若川。之前送過N次書&#xff0c;可以點此查看回饋粉絲&#xff0c;現在又和博文視點合作再次爭取了幾本書&#xff0c;具體送書規則看文末。眾所周知&#xff0c;我在參加掘金人氣作者打榜活動&#xff08;可點擊跳轉&#xff09;&#xff0c;需要大家投…