Title: Can my UpdatePanel reference a DataTable without a DB hit everytime it posts back?
Tags: asp.net;.net;datatable;updatepanel;postback
Question: I have a paging repeater inside of an UpdatePanel so that I can show, say 10 records at a time of a DataTable. When hitting the next/back buttons it will, of course, show the next 10 or previous 10 records. Is there a way I can have it reference the same DataTable when I hit next/back without having to get the DataTable again from the DB on page load? I think I'm just having a bit of a brain fart. Thanks for the help.


Here is the accepted answer: One option is to use .Net's caching APIs. Add your data to the cache for a certain duration or with a dependency, and then retrieve it rather than calling your database query.

http://msdn.microsoft.com/en-us/library/system.web.caching.cache.add.aspx

```public void AddItemToCache(Object sender, EventArgs e) {    
    if (Cache["Key1"] == null)
      Cache.Add("Key1", "Value 1", null, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration, CacheItemPriority.High, onRemove);
}
```

Comment for this answer: Storing data in the Session object could result in greater memory usage as it would need to be stored for every session. Storing a large data set in the ViewState may dramatically increase the page size and speed. The Cache object is more suited to this scenario and provides greater flexibility and extensibility.
Comment for this answer: alternatively you could add it to the Session or the ViewState. Not sure which is considered best practice though.

Here is another answer: I agree with @TimS but if you have scenario like you want to show the records based on some criteria you need to use ViewState (though it is most discouraged practise) to store the datatable specific to the page's criteria and do the work of next and previous.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:01/27/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<asp.net><.net><datatable><updatepanel><postback>', 'Title': 'Can my UpdatePanel reference a DataTable without a DB hit everytime it posts back?', 'ViewCount': 240, 'id': 9041119, 'user_AboutMe': "<p>I'm a developer living in Phoenix, AZ; currently working at a software consulting company in Scottsdale. I come to SO every time I need help, so if I'm able I like to try to help others as well.</p>\n", 'user_AccountId': 1004692.0, 'user_DisplayName': 'Goose', 'user_Reputation': 3191.0, 'user_Views': 291.0, 'user_id': 1018753.0}}