Why does my debugger tell me that ViewState.Count = 0 when ViewState is very big?
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 hav开发者_C百科ing 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?
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"
or viewstatemode (.net 4.0)
at the gridview control and databind on every page load including postbacks.
精彩评论