Thoughts on grids and viewstate
Fritz raises an interesting point about grids and viewstate. I replied suggesting that simply resetting the DataSourceID property (to the same value) triggers a process that in the end fixes the issue. Sure, it's kind of hacky, but it works.
However, I coundn't agree more with Fritz that 1) disabled viewstate on the grid; 2) caching through the data source control are overall the best approach to displaying data through grids without killing the page--in download, but also in upload.
I thought I should share a few additional remarks on the topic here.
Caching is fine, especially now that it is kind of "free". You just set EnableCaching and go. Note that, for some reasons, caching is not enabled (Beta 2, at least) on ObjectDataSource when you use custom collections. In this case, caching should be implemented in the bound objects--your BLL.
- Easy and blissful caching might lead to sub-optimal code if this means that you let the page cache everything results from a query. Keep the size of the data in the cache under control.
- Back to viewstate and grids, note that the size of the viewstate that results from a grid (both DataGrid and GridView) mostly depends on styles and additional helper properties being saved. Control state is not the point here, as it is saved to the viewstate hidden field anyway. It differs from classic viewstate because of the different API and because it can't be disabled.
- True data in the viewstate for a grid is not really much--just the displayed strings.
While disabling the viewstate is definitely a good option keeping everything else that is critical to the control to the control state, I'd spend some thoughts on the fact that style properties take up most of the viewstate and they're 90% of the time constant and written to the .aspx when not in a theme. Why persisting to the viewstate? With this info in the viewstate, the grid can rebuild itself with no data access when in the IsPostBack branch. This makes sense if you want to save a roundtrip to the database, but not if you have data cached.