views:

386

answers:

4

I'm not sure if this is possible in ASP.NET, but here is the problem I have: some of the data lists on my web application are displayed using a GridView; however, because some of these lists can contain a lot of data, they sometime push the page weight up into the several megabyte range due to the ViewState. Since we don't want to requery the database for this information each time there is a postback on the page I was wondering if there was a way that I could cache the data given to the control on the server and then access it the next time there is a postback.

So is this even possible? If so can someone point me in the direction of more information on how to do this? If it is not possible, does anyone have any suggestions on how I might be able to solve this problem?

+3  A: 

You can use a different store for your viewstate besides the page. This Article describes some methods for doing that.

The gist of this is to override SavePageStateToPersistenceMedium and LoadPageStateFromPersistenceMedium.

Jeff Martin
Careful with the SavePageStateToPersistenceMedium and LoadPageStateFromPersistenceMedium overloads. They break when using the asp.net ajax <asp:UpdatePanel /> control. This burnt us pretty badly on a large project.
Andrew Robinson
@Andrew - We use a lot of ASP.NET Ajax in this application, having that break would be a "Bad Thing™"
Rob
+2  A: 

You might try turning off ViewState on the GridView. This can frequently be done without affecting functionality. Works very well if your GridView is only displaying data and doesn't allow edits, updates, deletes, etc.

http://www.pluralsight.com/main/screencasts/screencast.aspx?id=understanding-viewstate

EnableViewState="false"

In general, you have to create a balance between ViewState on the client and some type of cache on the server. Don't be horribly afraid of re-querying your database. It can cache the data quite well. Possibly look at enabling page caching. I would avoid stuffing all that data into Session which comes with its own set of problems.

http://msdn.microsoft.com/en-us/library/hdxfb6cy.aspx

<%@ Page Language="C#" AutoEventWireup="true" ... %>
<%@ OutputCache ... %>
Andrew Robinson
The custom GridView I'm working on is going to be for read-only views, so not having access to edits isn't a concern. However, one of the things that we are trying to allow for is quickly applying sorts to the data set and right now caching everything in a DataTable on the server side seems like...
Rob
...a better way of doing things. However, I'm still not sure if it is the best way given our circumstances.
Rob
A: 

Here is an example of how you can store the Viewstate in local temp files: http://www.marklio.com/marklio/PermaLink.aspx?guid=af76f1a0-2d44-44c3-bfb5-029740b8e683

You are still using Viewstate with this approach, but instead of filling up the HTML you are creating temp files on the server side. Thus the user won't notice that the Viewstate is huge. You could also rewrite the sample above to store the Viewstate data somewhere else, like a database or even in memory.

The advantage of this approach is that you won't have to rewrite much code. The disadvantage is that those temp files will be filling up space on the server and needs to be cleaned up once in a while.

Thomas Petersen
A: 

Rob, what i would try in your case is to cache the data retrieved from the database in session - the first time grid is requesting it.

If you try this and combine it with the disabling of the view state for the grid, you can come up with pretty slick solution.

If you cannot count on Session for the caching, there are always some alternative persistence mechanisms that might do the job.

But from the design aspect of the given situation, i would definitely go with the data caching approach.

ljubomir