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

推荐订阅源

量子位
T
The Blog of Author Tim Ferriss
U
Unit 42
Microsoft Security Blog
Microsoft Security Blog
WordPress大学
WordPress大学
Vercel News
Vercel News
MongoDB | Blog
MongoDB | Blog
P
Proofpoint News Feed
D
DataBreaches.Net
The GitHub Blog
The GitHub Blog
大猫的无限游戏
大猫的无限游戏
C
Check Point Blog
Blog — PlanetScale
Blog — PlanetScale
I
InfoQ
Y
Y Combinator Blog
F
Full Disclosure
B
Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
G
Google Developers Blog
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
博客园 - 叶小钗
S
SegmentFault 最新的问题
腾讯CDC
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
Visual Studio Blog
Apple Machine Learning Research
Apple Machine Learning Research
人人都是产品经理
人人都是产品经理
Recent Commits to openclaw:main
Recent Commits to openclaw:main
The Register - Security
The Register - Security
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Azure Blog
Microsoft Azure Blog
云风的 BLOG
云风的 BLOG
Last Week in AI
Last Week in AI
F
Fortinet All Blogs
C
CXSECURITY Database RSS Feed - CXSecurity.com
Hugging Face - Blog
Hugging Face - Blog
T
Threatpost
GbyAI
GbyAI
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
L
LangChain Blog
T
Tenable Blog
C
Cisco Blogs
T
Threat Research - Cisco Blogs
Google Online Security Blog
Google Online Security Blog

博客园 - 心利

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.