要查看状态还是不查看状态?
我有一个 Webforms 网站,每页有 20 多个控件来显示数据,我有 2 个选项来保存值:
使用 Web 控件显示我的所有值并利用 ViewState。只需要在 IsNotPostback 时填充控件,它们每次使用 ViewState 都会呈现相同的值。
在每次从数据库回发时禁用 ViewState 并填充控件。
我只需要访问一次数据库,我将使用相同的查询填充 20 个控件。
哪一个更便宜?我应该使用ViewState吗?
I have an webforms website with 20+ controls per page to display data, I have 2 options to persist values:
Use web controls to show all my values and take advantage of ViewState. Just need to populate controls when IsNotPostback and they will render same values every time using ViewState.
Disable ViewState and populate controls on every postback from database.
I just have to go once to DB, I'm filling the 20 controls using the same query.
Which one is cheaper?, should I use ViewState or not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
根据您的输入,我想说肯定是在传输中来回增加 ViewState 开销,而不是一次又一次地执行 20 次数据库往返来加载数据。
当然,您可以在单个超快速数据库调用中缓存和/或查询所有需要的数据,这样最终也可能很便宜,具体取决于您的整体设计。
如今,考虑到快速 LAN / WAN 连接,ViewState 应该不是一个大问题,特别是如果该网站仅在 Intranet 中使用,那么我会离开 ViewState,但我也会尝试优化/最小化一般数据库调用和缓存以及。
based on your input I would say surely is better to have the ViewState overhead back and forth in the transfers rather than executing all the times 20 roundtrips to the database to load the data again and again.
Of course you could have caching and / or query all needed data in a single super fast db call so that could also be cheap in the end, depending on your overall design.
ViewState nowadays should not be a big issue thinking about fast LAN / WAN connections and especially if the web site is used only in the Intranet, then I would leave the ViewState, but I would also try to optimize/minimize db calls in general and cache as well.
通常,除非有很多数据,否则请使用ViewState。这取决于应用程序和情况。
我的建议是您现在使用它,并以后优化。您甚至可以禁用ViewState以获取携带大量数据的特定控件。看来您现在没有足够的理由现在不使用它。
Generally, do use viewstate unless you have a lot of data. It depends on the application and the situation.
My suggestion is that you use it now, and optimize things later. You can even disable viewstate for specific controls which carry lots of data. It seems you do not have a good enough reason not to use it right now.
我不认为 ViewState 对于 20 个左右的控件来说是一个很大的负担。对您来说,第一次尝试并在必要时进行改进会更容易。但这取决于您的应用程序中使用的数据。
我觉得下面很有趣。
通过压缩 ViewState 提高性能
I don't think ViewState can be a big burden for around 20 controls. It's much easier for you to do as the first attempt and do improvements later if necessary. However that depends on the data that uses in your application.
I find below interesting.
Improve Performance By Compressing ViewState
这取决于,我通常不会太担心下拉列表等控件的视图状态(前提是它没有填充数百个项目,这将使其无法使用)。
对于像网格这样的控件,我会关闭视图状态,特别是 telerik(它有很好的替代方案)。
顺便说一下,您可以使用缓存来保存数据库行程。
It depends, I generally do not worry too much about the view state for controls such as dropdown etc (provided it is not populated with 100s of items, which will make it unusable anyway).
For controls like grid, I would turn off the view state, specially telerik (which has nice alternatives).
You can use caching to save your database trips by the way.
使用jquery ajax渲染html,这样就不存在viewstate的问题。这将提高性能并减少服务器的负载。
动态加载
Render the html using the jquery ajax so that there is no problem of viewstate. This will give performance and put less load in the server.
Dynamicloading