Welcome to AspAdvice Sign in | Join | Help

Force Refresh of DataSourceControl

I had a situation where I was using a DataSourceControl (actually an LLBLGenProDataSource2,which inherits from DataSourceControl) bound to a grid, and I wanted the grid to refresh its contents whenever I added or deleted an item from it.  I discovered a neat trick noted here, which is that if you touch the SelectParameters collection of the DataSourceControl, it will cause it to DataBind in its PreRender stage.  So in my case, wherever I wanted it to rebind my grid, I would simply do this:

PaymentDataSource.SelectParameters.Add("foo", "0"); // hack to force grid to rebind

In my case I wasn't using any parameters, and this is probably not the *best* way to do this, but it's 2am and it worked for me so I'm going to keep it until some noble commenter offers the proper way to do this.

Published Saturday, August 25, 2007 1:58 AM by ssmith
Filed under: , ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Force Refresh of DataSourceControl

Isn't it easier to call the DataBind() method on the grid instead?

Tuesday, August 28, 2007 4:53 PM by Ron

# re: Force Refresh of DataSourceControl

Easier yes, but it didn't work.  The DataSourceControl kept the same data (and would bind it to the grid as many times as you like) unless you forced it to refetch the data.  The hack described here made that happen.

Tuesday, August 28, 2007 10:31 PM by ssmith

# re: Force Refresh of DataSourceControl

use gridview's databind method can cause objectdatasource refetch the data

Tuesday, September 25, 2007 10:11 AM by jecray

# re: Force Refresh of DataSourceControl

Set EnableViewState="false" on the ObjectDataSource that you want to refresh.  This basically turns off all caching on the object, requiring a select every time access the object.

Wednesday, October 31, 2007 8:37 PM by Selrach

# re: Force Refresh of DataSourceControl

To invalidate the cache you set the ObjectDataSource.CacheKeyDependency property then call the Cache.Remove Method with the current key as a parameter.

Friday, December 21, 2007 3:02 PM by Alexander Wykel

# re: Force Refresh of DataSourceControl

Cache.Remove doesn't work

Calling .DataBind() on the GridView does work.

Tuesday, February 05, 2008 7:09 PM by BluFire

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below