问题登记。 ASP.NET 视图状态
我一直在阅读这篇优秀的文章 http://msdn.microsoft.com/ en-us/library/ms972976.aspx 表示 ViewState 不负责表单字段在回发之间保留其值。那么表单字段值永远不会存储在 ViewState 中吗?
编辑:我的意思是表单字段是 ASP.NET 控件,如 TextBox、Dropdownlist 等。
编辑:如果用户在 ASP.NET 文本框中输入一个值并提交表单,新页面仍然具有我正在考虑的具有该值的文本框这是因为 ViewState 但文章说事实并非如此!
I've been going through this excellent article http://msdn.microsoft.com/en-us/library/ms972976.aspx that says ViewState is not responsible for form fields to retain their values between postbacks. So form fields values are never stored in ViewState?
EDITED: What I mean form fields are ASP.NET controls like TextBox, Dropdownlist etc.
EDITED: If an user enters a value in an ASP.NET textbox and submit the form, the new page still has the textbox with that value I was thinking this is because of ViewState but the article says it's not so!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正如您所说,表单值不存储在视图状态中。 (例如)TextBox 控件的文本在两次回发之间保留的原因是它实现了 IPostBackDataHandler-契约并自动将 Request.Form-collection 中的键映射到控件的相应属性。这两种机制经常被混淆。
请参阅http://www.mikesdotnetting.com/Article/ 65/ViewState-form-fields-labels-and-Javascript 以获得很好的解释。
As you say, form values are NOT stored in the viewstate. The reason that (for example) the text of a TextBox control is retained between two postbacks is because it implements the IPostBackDataHandler-contract and automatically maps the keys in the Request.Form-collection to the appropriate properties of the control. These two mechanisms are often confused.
See http://www.mikesdotnetting.com/Article/65/ViewState-form-fields-labels-and-Javascript for a good explanation.
文本字段不会在 ViewState 中携带其值,因为它们的值是通过 HTTP POST 显式发送的(请参阅
Request.Form
),并在 Page_Load 之前恢复到控件。DropDownList 确实使用 ViewState 来存储其内容。
Text fields don't carry their value in ViewState because their value is explicitly sent through the HTTP POST (See
Request.Form
) and restored to the control before Page_Load.DropDownLists do use ViewState to store their contents.
只有 Asp.Net Control 才会存储在 ViewState 中。没有 html 字段。
所以
会起作用,
不会起作用。
only Asp.Net Control will stored in the ViewState. No html fields.
So
will work and
will not work.
正如 dknaack 所说:否
但是您可能还想看看
ControlState
因为ViewState
可以被禁用。As stated by dknaack: No
But you may also want to have a look at
ControlState
sinceViewState
can be disabled.viewstate 的观点是跟踪 web 控件中的“变化”。作为开发人员,您有责任确保每次页面加载时重新创建控件的“初始”状态。然后,asp .net 机制确定回发场景期间发生的更改,以决定应在服务器端触发哪些事件。
要了解 ASP .Net 回发机制的工作原理以及 Viewstate 的适用范围,请查看我最初在 SO 上提出的这个问题;
回发是如何工作的?
The point of viewstate is to track the "change" in your webcontrols. It's your responsibility as a developer to ensure that the "initial" state of your controls is recreated each page load. Then the asp .net mechanism determines the change that occurred during a postback scenario to decide which events should be fired on the server side.
For an overview of how the ASP .Net postback mechanism works and where Viewstate fits in have a look at this question I originally asked on SO;
How does Postback Work?