视图状态与回发
我想我回答了我自己的问题,但我想确保我理解正确。我最初认为,当用户在表单中提供值时,回发时这些值将作为视图状态的一部分提交,因为 TextBox.Text 是视图状态的一部分。现在我发现用户提供的值实际上直到 OnLoad 事件之后才应用于控件。这让我很困惑,因为我认为视图状态是在 OnLoad 之前(或调用 Controls.Add() 时)加载到控件中的。我已经仔细阅读了页面和控件生命周期上的文档几次,我现在才意识到处理回发数据有一个不同的步骤(此步骤没有出现在很多文档中:(
1)所以回发数据,用户在字段中输入的值在 OnLoad 事件之后应用,而 Viewstate 数据在 OnLoad 事件之前应用?
2) 所以本质上这意味着服务器在回发时获取 TextBox.Text 属性的两个值,即Viewstate 中的一个,类似于前一个请求中的“旧”值,以及用户在表单中提供的新值?
3).net框架是否像Viewstate一样应用回发数据,因为它通过它的ID属性找到适当的控件?这很重要,因为我正在动态创建控件,甚至可能会有随着时间的推移而改变结构的表单,并且需要考虑如何处理 ID。到目前为止,我还没有设置 ID 属性,一切正常,但以后事情可能会更复杂。
4)视图状态数据是否在客户端被修改过?或者视图状态与服务器在上一个请求中发送的内容是否相同(假设没有篡改)?我以前的印象是,服务器将所有控件属性编码到视图状态中,而在客户端,当用户提交表单时,视图状态字段被解码、修改、编码,并修改后提交给服务器。我以为有一堆 JavaScript 为我做这一切。现在我想我全错了。相反,视图状态似乎永远不会在客户端发生变化,并且所有客户端更改都在回发数据中,以便服务器的下一个请求加载视图状态,加载回发,并在下一个响应中提供新的更新的视图状态?
I sort of answered my own question I think but I want to make sure I am understanding correctly. I initially thought that when a user provided values in a form, that on postback the values were submitted as part of the Viewstate, because TextBox.Text is part of the viewstate. Now I have found that user supplied values actually aren't applied to the controls until after the OnLoad event. This confused me because I thought that viewstate was loaded into the controls before OnLoad(or when calling Controls.Add()). I have gone over the documentation on page and control lifecycles a few times and I am just now realizing that there was a different step for handling postback data(this step didn't appear in a lot of documentation :(
1) So postback data, the values user's type into the fields, is applied after the OnLoad event, and Viewstate data is applied just before the OnLoad event?
2) So essentially all this means is that on postback the server gets two values for a TextBox.Text property, the one in Viewstate, which is like the "old" value from the previous request, and the new value supplied by the user in the form?
3) Does the .net framework apply postback data the same was as Viewstate, in that it finds the appropriate control via it's ID property? This is important because I am creating controls dynamically and I may even have forms that change structure overtime and need to think about how I handle ID's. So far I haven't been setting the ID property and everything works fine but things may be more complicated later on.
4) Does viewstate data ever get modified at all on client side? Or is the viewstate identical to what was sent by the server in the previous request(assuming no tampering)? My impression used to be that the server encoded all the control properties into the viewstate, and on the client side when the user submitted the form, the viewstate field was decoded, modified, encoded, and submitted to the server with modifications. I assumed there was a bunch of javascript doing all this for me. Now I think I had it all wrong. Instead it seems that the Viewstate never changes on client side, and all the client changes are in the postback data such that the next request the server loads viewstate, loads postback, and provides a new updated viewstate in the next response?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1) 两者都在Load之前加载
2)基本上是的
3)首先应用ViewState,然后发布数据
引用Scott Mitchell(见下文)
4) 除非您正在做一些超出常规的事情,否则 ViewState 永远不会在客户端进行修改。 “ViewState”是一个 HTML 表单字段,在服务器端处理。
以下是了解 ASP 中的一些图片Scott Mitchell 的 .NET View State 可能会对您有所帮助。
(来源:microsoft.com< /a>)
(来源:microsoft.com< /a>)
额外阅读材料:http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx
1) Both are loaded before Load
2) Basically, yes
3) ViewState is applied first, then Post Data
To quote Scott Mitchell(see below)
4) Unless you're doing something way outside of the box, ViewState is never modified client-side. "ViewState" is an HTML form field and is processed on the server side.
Here's a few images from Understanding ASP.NET View State by Scott Mitchell that may help you.
(source: microsoft.com)
(source: microsoft.com)
Bonus Reading Material: http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx
不,ViewState 的目的只是保留自上次“保存视图状态”页面事件以来的页面状态,即该事件在页面呈现给客户端之前不久发生。
当客户端对下拉框进行选择或更改文本框中的文本时,隐藏的 ViewState 属性(作为静态 HTML 标记存在于客户端页面上)不会动态更改/编码这些值,它保持与页面打开时相同。最初渲染的。
那么如何保留页面的新状态,即如何在 ASP 控件中保留用户下拉选项和文本框值?这些下拉选项和文本框值在回发数据中捕获。
——斯科特·米切尔
No, the point of the ViewState is simply to preserve the state of the page since the last "Save View State" page event, i.e. that event occurs shortly before the page is rendered to the client.
When the client makes selections to a dropdown box or changes text in a textbox the hidden ViewState property, which exists on the client page as a static HTML tag, is not dynamically changing / encoding those values, it remains the same as when the page was originally rendered.
So how is the new state of the page being preserved, i.e. how are user dropdown selections and text box values retained in ASP controls? Those dropdown selections and text box values are captured in Post Back data.
- Scott Mitchell