惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
J
Java Code Geeks
WordPress大学
WordPress大学
博客园 - 【当耐特】
博客园 - 叶小钗
小众软件
小众软件
博客园 - 聂微东
宝玉的分享
宝玉的分享
量子位
人人都是产品经理
人人都是产品经理
博客园_首页
罗磊的独立博客
腾讯CDC
美团技术团队
Google DeepMind News
Google DeepMind News
W
WeLiveSecurity
I
InfoQ
Engineering at Meta
Engineering at Meta
云风的 BLOG
云风的 BLOG
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Threat Research - Cisco Blogs
Google DeepMind News
Google DeepMind News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
H
Hacker News: Front Page
B
Blog RSS Feed
L
LangChain Blog
C
Check Point Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
G
GRAHAM CLULEY
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Microsoft Azure Blog
Microsoft Azure Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
博客园 - Franky
S
Schneier on Security
Attack and Defense Labs
Attack and Defense Labs
Microsoft Security Blog
Microsoft Security Blog
N
Netflix TechBlog - Medium
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Tenable Blog
Simon Willison's Weblog
Simon Willison's Weblog
L
LINUX DO - 热门话题
阮一峰的网络日志
阮一峰的网络日志
Hacker News: Ask HN
Hacker News: Ask HN
A
Arctic Wolf
Schneier on Security
Schneier on Security
The Last Watchdog
The Last Watchdog
Latest news
Latest news
T
The Exploit Database - CXSecurity.com

博客园 - 心利

DocOptimizer 0.9.0 Beta Released SharePoint GroupedItemPicker Control Drag & Drop between SharePoint Document libraries Improving SharePoint User Experience With JQuery-Client Side Form Validate SharePoint How To:定位错误:“An SPRequest object was not disposed .." SharePoint Tips and Tricks --如何用JS向PeopleEditor填充数据 SharePoint Tips and Tricks -- Ribbon,People Editor 使用T4为数据库自动生成实体类(C#) (Tips&Tricks)用客户端模板精简JavaScript代码 (Tips &Tricks)如何为Windows Mobile 创建拨号连接--C# System.Xml FAQ Part 1 Windows Ce vs Windows Mobile Asp.net程序中生成Excel报表 My Page StartKit项目概览 做一个更好的程序员 .Net类库中实现的HashTable 为Asp.net控件写单元测试(ViewState) 使用Control Adapters优化Asp.net控件 桌面背景:rss新闻阅读器(图)
ViewState 简述一(With Example And Apply to Asp.net)
心利 · 2007-05-20 · via 博客园 - 心利

ViewState是asp.net中最重要的功能之一,因为它提供了一种完全自动化的状态管理方式,当服务器端代码改变页面的状态时(server control's property), ViewState机制会自动存储这些变化,从而使客户端得到的是一致的页面。

让我们看一个例子:

<asp:Label runat="server" ID="lblMessage" Font-Name="Verdana" Text="Hello, World!" EnableViewState="false"></asp:Label>
<br />
<asp:Button runat="server" Text="Change Message" ID="btnSubmit"></asp:Button>
<br />
<asp:Button runat="server" Text="Empty Postback"></asp:Button>


"Change Message”用来改变label的Text,代码如下:

private void btnSubmit_Click(object sender, EventArgs e)
{
lblMessage.Text 
= "Goodbye, Everyone!";
}
 

" Post Back"什么也不做就是post back.

当用户请求这个页面,LifeCycle 1,

  • asp.net会在Init事件触发之前,初始化Label控件,其text属性的默认值设置为hello world
  • 向用户发送"hello world"

用户点击了"Change Message" ,将请求重新发回服务器,LifeCycle 2

  • asp.net会在Init事件触发之前,初始化Label控件,其text属性的默认值设置为hello world
  • 在Load事件触发之后,处理PostBackEvent,将Lebel的text属性改为 "goodbye , Everyone"
  • 向用户发送"goodbye , Everyone"

那么这时如果用户点击了"post back",我们希望服务器返回什么呢?当然是"good bye ,EveryOne".但是我们知道通常情况下Asp.net通过实例化一个Page-derived对象处理客户端的请求,并在处理完毕后迅速将其销毁.也就是说我们没有办法去存储类似"Goodbye ,Everyone"这种信息,当用户Post back后,当前状态将会丢失,服务器会运行 LifeCycle 1, 返回Hello world.

ViewState机制就是为了解决这种问题而设计的。假如我们将Label控件的 EnableViewState属性设置为True,那么LifeCyle 2 ,会变为:

  • asp.net再Init事件触发之前,初始化Label控件,其text属性的默认值设置为hello world
  • 再Load事件触发之后,处理PostBackEvent,将Lebel的text属性改为 "goodbye , Everyone"
  • 检测象"Goodbye ,Everyone"之类状态的变化,将其存储在一个字符串中。
  • 向用户发送"goodbye , Everyone",并将包含状态信息的字符串以hidden form field的方式一并发送给用户

当用户Post back,会将包含状态信息的Field ,Post给服务器

  • asp.net会在Init事件触发之前,初始化Label控件,其text属性的默认值设置为hello world
  • 再Init事件触发之后,解析客户端的请求信息(Hidden form field), 还原各个控件的状态信息
  • 向用户发送"Goodbye Everyone"。

Ps: 将在下一篇文章中学习ViewState's Performance , and server side viewState.

This article based on Scott Mitchell's <<Understanding ASP.NET ViewState>>

And Dino Esposito's <<The Asp.net View State>>,thanks them.