ASP.NET如何工作的ViewState [英] ASP.NET How ViewState works

查看:156
本文介绍了ASP.NET如何工作的ViewState的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的.aspx页面上的文本框和按钮。 TextBox的EnableViewState属性设置为false。但是,当我在文字框中输入一些文字,然后点击按钮输入的文本仍然是美元。在文本框中p $ psent。我希望,因为的EnableViewState设置为false文本框为空。我缺少的东西吗?


解决方案

请检查这个code ++项目文章更好地了解的ViewState和回发数据。

这是一样的东西:


  

为什么有些控件保留值
  即使禁用ViewState中后
  而其他人没有?


  
  

答案是控制哪些
  农具<击> IPostBackEventHandler IPostBackDataHandler像
  文本框,复选框,等将保留
  国家甚至失效后,
  视图状态。原因是在
  加载回发数据的阶段,这些
  控件将获得状态信息
  从贴后背的形式。


  
  

但像标签控件哪些没有
  实施<击> IPostBackEventHandler IPostBackDataHandler会
  没有得到任何状态信息
  贴后背的数据,因此依赖
  完全是靠视图状态保持
  状态。


下面是有关段落您的问题。


  

在页面生命周期,两个事件
  与ViewState的关联:


  
  

      
  • 加载视图状态:这一阶段遵循的初始化阶段
      页面生命周期。在这一阶段,
      保存在ViewState信息
      previous回传被装入
      控制。因为没有必要检查
      和负载previous数据,当页面
      加载的第一次这
      阶段将不会发生。在随后的
      网页回发因为可能会有
      为控制previous数据显示,
      页面会经过这个阶段。


  •   
  • 保存视图状态:这一阶段precedes页面的渲染阶段。
      在这个阶段中,当前的状态
      控件(值)序列化成
      64位带codeD串并坚持
      隐藏控制(__ViewState)在
      该页面。


  •   
  • 加载回发数据阶段:虽然这个阶段有无关
      ViewState中,它会导致最
      开发者之间的误解。这个
      当页面有阶段只发生
      被贴后背。 ASP.NET控件
      该实施<击> IPostBackEventHandler IPostBackDataHandler
      将更新从它的值(州)
      适当的回发数据。该
      要注意这个重要的事情
      阶段如下:


      
      

        
    1. 国家控制(值)不从的ViewState但是从中检索
        贴后背的形式。

    2.   
    3. Page类将移交回发数据,只有那些
        其中实行控制
        <击> IPostBackEventHandler IPostBackDataHandler

    4.   
    5. 这个阶段遵循加载视图状态阶段,在换句话说状态
        加载视图中设置控件
        国家舞台将被覆盖
        这个阶段。

    6.   

  •   

I have a textbox and button on my .aspx page. The EnableViewState property of the textbox is set to false. But when I enter some text in the textbox and click the button the entered text is still present in the textbox. I expect the textbox to be blank since EnableViewState is set to false. Am I missing something?

解决方案

Please check this Code Project article to better understand ViewState and Postback Data.

It is something like :

Why some controls retain values even after disabling the ViewState while others do not?

The answer is Controls which implements IPostBackEventHandler IPostBackDataHandler like Textbox, Checkbox, etc. will retain the state even after disabling the viewstate. The reason is during the Load Postback Data stage, these controls will get state information from Posted back form.

But controls like label which do not implement IPostBackEventHandler IPostBackDataHandler will not get any state information from posted back data and hence depend entirely on viewstate to maintain the state.

Below is related paragraph to your question.

In page life cycle, two events are associated with ViewState:

  • Load View State: This stage follows the initialization stage of page lifecycle. During this stage, ViewState information saved in the previous postback is loaded into controls. As there is no need to check and load previous data, when the page is loaded for the first time this stage will not happen. On subsequent postback of the page as there may be previous data for the controls, the page will go through this stage.

  • Save View State: This stage precedes the render stage of the page. During this stage, current state (value) of controls is serialized into 64 bit encoded string and persisted in the hidden control (__ViewState) in the page.

  • Load Postback Data stage: Though this stage has nothing to do with ViewState, it causes most of the misconception among developers. This stage only happens when the page has been posted back. ASP.NET controls which implement IPostBackEventHandler IPostBackDataHandler will update its value (state) from the appropriate postback data. The important things to note about this stage are as follows:

    1. State (value) of controls are NOT retrieved from ViewState but from posted back form.
    2. Page class will hand over the posted back data to only those controls which implement IPostBackEventHandler IPostBackDataHandler.
    3. This stage follows the Load View State stage, in other words state of controls set during the Load View State stage will be overwritten in this stage.

这篇关于ASP.NET如何工作的ViewState的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆