视图状态与回发

发布于 2024-09-04 00:19:59 字数 817 浏览 6 评论 0原文

我想我回答了我自己的问题,但我想确保我理解正确。我最初认为,当用户在表单中提供值时,回发时这些值将作为视图状态的一部分提交,因为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱格式化 2024-09-11 00:19:59

1) 两者都在Load之前加载
2)基本上是的
3)首先应用ViewState,然后发布数据

引用Scott Mitchell(见下文)

动态添加的控件必须
以编程方式添加到网页
在每个页面访问。最好的
添加这些控件的时间是
页面初始化阶段
生命周期,发生在
加载视图状态阶段。也就是说,我们
想要有控制层次结构
在加载视图状态之前完成
阶段到来。正因如此,
最好创建一个事件处理程序
您的 Page 类的 Init 事件
代码隐藏类,并添加您的
那里有动态控制。

4) 除非您正在做一些超出常规的事情,否则 ViewState 永远不会在客户端进行修改。 “ViewState”是一个 HTML 表单字段,在服务器端处理。

以下是了解 ASP 中的一些图片Scott Mitchell 的 .NET View State 可能会对您有所帮助。

替代文本
(来源:microsoft.com< /a>)

alt 文本
(来源: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)

dynamically added controls must be
programmatically added to the Web page
on each and every page visit. The best
time to add these controls is during
the initialization stage of the page
life cycle, which occurs before the
load view state stage. That is, we
want to have the control hierarchy
complete before the load view state
stage arrives. For this reason, it is
best to create an event handler for
the Page class's Init event in your
code-behind class, and add your
dynamic controls there.

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.

alt text
(source: microsoft.com)

alt text
(source: microsoft.com)

Bonus Reading Material: http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx

顾铮苏瑾 2024-09-11 00:19:59

我曾经的印象是服务器对所有控件进行编码
属性进入视图状态,并在用户侧时在客户端
提交表单,viewstate字段被解码、修改,
编码,修改后提交给服务器。

不,ViewState 的目的只是保留自上次“保存视图状态”页面事件以来的页面状态,即该事件在页面呈现给客户端之前不久发生。

当客户端对下拉框进行选择或更改文本框中的文本时,隐藏的 ViewState 属性(作为静态 HTML 标记存在于客户端页面上)不会动态更改/编码这些值,它保持与页面打开时相同。最初渲染的。

那么如何保留页面的新状态,即如何在 ASP 控件中保留用户下拉选项和文本框值?这些下拉选项和文本框值在回发数据中捕获。

A server control can indicate that it is interested in examining the posted back data by implementing the IPostBackDataHandler interface. In this stage in the page life cycle, the Page class enumerates the posted back form fields, and searches for the corresponding server control. If it finds the control, it checks to see if the control implements the IPostBackDataHandler interface. If it does, it hands off the appropriate postback data to the server control by calling the control's LoadPostData() method. The server control would then update its state based on this postback data.

——斯科特·米切尔

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.

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.

A server control can indicate that it is interested in examining the posted back data by implementing the IPostBackDataHandler interface. In this stage in the page life cycle, the Page class enumerates the posted back form fields, and searches for the corresponding server control. If it finds the control, it checks to see if the control implements the IPostBackDataHandler interface. If it does, it hands off the appropriate postback data to the server control by calling the control's LoadPostData() method. The server control would then update its state based on this postback data.

- Scott Mitchell

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文