ViewState 或 HiddenField
如果我要存储一段简单的数据(例如整数或字符串),我可能会选择将其存储在 ViewState 中,或使用 HiddenField 控件。
为什么我会选择其中之一而不是另一个?
ViewState
- 对于用户来说很难解码(认为并非不可能),这可能是理想的
HiddenField
- 值可以在 JavaScript 中使用
还有其他优点和缺点吗?
If I have a simple piece of data to store (an integer or string for example) I might choose to store that in ViewState, or using a HiddenField control.
Why would I choose one over the other?
ViewState
- Hard for the user to decode (thought not impossible), which might be desirable
HiddenField
- Value can be used in JavaScript
Are there other pros and cons?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
事实并非如此,ViewState 实际上存储在隐藏字段中,因此唯一真正的区别是编码。
除非您需要使用 JavaScript 操作该值,或者您希望完全关闭此页面上的 ViewState,否则我会使用 ViewState。 主要是因为有第三方工具(像这个)可以理解 ViewState 并赢得胜利不理解您的自定义隐藏字段。
Not really, ViewState is actually stored in a hidden field so the only real difference is the encoding.
Unless you need to manipulate the value with JavaScript or you hope to turn off ViewState on this page altogether then I'd use ViewState. Mostly just because there are third party tools (like this one) which understand ViewState and which won't understand your custom hidden field.
从可维护性的角度来看,我会使用 ViewState。 您需要编写的代码更少,这可以归结为软件中的故障点更少。 这也意味着您之后的任何开发人员都将更轻松地维护您的解决方案。
如果您对此不太满意,请在页面上编写一个属性访问器,充当外观以从 ViewState 检索值。 稍后,如果您觉得有必要将其转换为隐藏字段,则访问器可以为代码的其余部分无缝地处理该切换。 请确保您记录了这样做的原因。
From a maintainability point of view, I'd use ViewState. It's less code for you to write, which comes down to fewer points of failure in your software. It also means that any developers coming after you will have an easier time maintaining your solution.
If you're not entirely comfortable with that, write a property accessor on the page that acts as a facade to retrieve the value from the ViewState. Later, if you feel compelled to convert it to a hidden field, the accessor can handle that switch seemlessly for the rest of the code. Just be sure you document your reasons for doing so.
视图状态仅在您所在的页面或回发的页面上有效。 通过隐藏字段,您可以使用 Page 对象的 PreviousPage 方法访问导航到的下一页上的数据(以及其他数据),如下所示:
Viewstate is only good on the page you are on or posting back to. With a hidden field you can access the data on the next page you navigate to (as well as other data) by using PreviousPage method of the Page object like so:
ViewState 存储在页面本身中,因此会增加页面大小,并可能导致 性能问题。
我们还可以将应用程序配置为将视图状态保存在服务器上而不是在页面本身上,这可以防止某些安全问题。
乔米特
The ViewState is stored in the page itself so it increases the page size and it may cause performance issues.
Also we can configure the application to save the viewstate on server rather than on page itself which might protect from some security issues.
Jomit
隐藏字段在页面上不可见,并且可以在视图源中查看其值,但视图状态的值已编码并且不可读。
隐藏字段值发布在下一页上。 (注意:使用server.transfer获取隐藏字段的值)。
The hidden field are invisible on page and their values can be viewed in view source but the value of view-state are encoded and are not readable.
The hidden field value are posted on next page. (Note: use server.transfer to get the value of hidden fields).