当 ViewState 非常大时,为什么我的调试器告诉我 ViewState.Count = 0?
我正在使用 GridView 处理 ASP.net 页面。我遇到的问题是 ViewState 非常大。
我想弄清楚如何配置我的 GridView 以在 ViewState 中放入更少的数据,但我遇到的第一个问题是我看不到 ViewState 中的内容。
我尝试在页面代码中的各个点放置断点(包括 Page_Load 和 gridview_PageIndexChanging)。在每个断点处,我发现 ViewState.Count 为 0。
为什么我的 ViewState 看起来是空的,而实际上它很大?
I'm working on an ASP.net page with a GridView. I have the problem that the ViewState is very large.
I would like to figure out how to configure my GridView to put less data in ViewState, but the first problem I'm having is that I can't see what's in ViewState.
I've tried putting breakpoints at various points in the page code (including Page_Load and gridview_PageIndexChanging). At each breakpoint, I find that ViewState.Count is 0.
Why does my ViewState appear to be empty when it is, in fact, very big?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 ASP.NET 中,每个控件都维护自己的视图状态。当您检查页面事件中的 ViewState.Count 时,您正在检查页面的 viewstate 属性的 statebag。
如果页面类中有任何属性,例如
ViewState["test"] = value
,您将看到它的计数。您将无法从页面类中看到 gridview 的视图状态。您的页面上的视图状态计数为空,因为您没有向视图状态添加任何属性。但是当浏览器渲染页面时,页面及其控件的所有视图状态信息都被放入视图状态的隐藏变量中。
要将更少的数据放入视图状态中,您将必须进行分页。如果是分页的,gridview 只会为可见行放置视图状态,这应该是最小的。如果您想完全禁用 gridview 的 viewstate,您可以在每个页面上的 gridview 控件和数据绑定上执行
enableviewstate="false"
或viewstatemode (.net 4.0)
负载包括回发。In asp.net, each control maintains its own viewstate. When you are checking for the ViewState.Count inside your page events, you are checking for the statebag of your page's viewstate properties.
If you have any properties such as
ViewState["test"] = value
in the page class, you will see the count for it. You won't be able to see the gridview's viewstate from your page class. The viewstate count is empty on your page because you dont have any properties added to viewstate.But when the browser renders the page, all the viewstate information for the page and its controls are put into the hidden variable for viewstate.
To put less data for your gridview into viewstate, you will have to do paging. If its paged, gridview only puts viewstate for the visible rows, which should be minimal. If you want to completely disable viewstate for the gridview, you can do an
enableviewstate="false"
orviewstatemode (.net 4.0)
at the gridview control and databind on every page load including postbacks.