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.mzph.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的新手吗? 或者,也许您只是停留在如何创建简单的…

Vite 4.0 正式发布!

源码共读我新出了:第40期 | vite 是如何解析用户配置的 .env 的链接:https://www.yuque.com/ruochuan12/notice/p40也可以点击文末阅读原文查看,欢迎学习记笔记~12 月 9 日,Vite 4.0 正式发布。下面就来看看 Vite 4.0 有哪些更新吧…

图像标注技巧_保护互联网上图像的一个简单技巧

图像标注技巧补习 (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 …

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.我 被自己的网站困在家里。…

你构建的代码为什么这么大?如何优化~

大家好&#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;需要大家投…

程序详细设计之代码编写规范_我在不编写任何代码的情况下建立了一个设计策划网站

程序详细设计之代码编写规范It’s been just over a month since MakeStuffUp.Info — my first solo project as an independent Creator; was released to the world. It was not a big project or complicated in any way, it’s not even unique, but I’m thrilled where …

偷偷告诉你们一个 git 神器 tig,一般人我不告诉TA~

大家好&#xff0c;我是若川。众所周知&#xff0c;我参加了掘金创作者人气作者投票活动&#xff0c;最后3天投票。今天可投28票&#xff0c;明天32票&#xff0c;后天36票&#xff08;结束&#xff09;。投票操作流程看这里&#xff1a;一个普通小前端&#xff0c;将如何再战掘…

DAO层使用泛型的两种方式

package sanitation.dao;import java.util.List;/** * * param <T>*/public interface GenericDAO <T>{/** * 通过ID获得实体对象 * * param id实体对象的标识符 * return 该主键值对应的实体对象*/ T findById(int id);/** * 将实体对象持…

将是惊心动魄的决战~

大家好&#xff0c;我是若川。一个和大家一起学源码的普通小前端。众所周知&#xff0c;我参加了掘金人气创作者评选活动&#xff08;投票&#xff09;&#xff0c;具体操作见此文&#xff1a;一个普通小前端&#xff0c;将如何再战掘金年度创作者人气榜单~。最后再简单拉拉票吧…