Hi
I'm currently using a WebGrid in LoadOnDemand mode, and as such need to handle to InitializeDataSource event for every time the page requests further data.
The problem i'm having is that I have a few search fields on the screen, and if the user chooses to modify these search parameters I get a postback. When that happens, the very first thing that happens (prior to Page_Load) is that InitializeDataSource fires. Having read other posts I inderstand why this is necessary, however what I want to do is modify the DataSource and then have the InitializeDataSource fire again to take into account the new data. I've tried several combinations of modifying the datasource outside of the InitializeDataSource event, and whilst it does actually work the displayed grid only shows the pre-modification data (ie. the data that existed when the InitializeDataSource event last fired). If i scroll the grid, it posts back again and gets the new data, which is great but I dont want the users to have to do the scroll to get the new data.
Is there any way to get the grid to reconsider the data once the InitializeDataSource event has fired?
Chris
InitializeDataSource wouldn't be called again in this scenario. My apologies for not mentioning this in my previous post.
One solution is to encapsulate the functionality you need called in both circumstances into a method, and call that method both from the InitializeDataSource event handler and after you manually re-bind the grid. Another solution is to use a different event, if there's one that makes sense; for example, InitializeLayout may be useful for processes that need to be run as the grid is bound but before any rows are created in the grid, especially for anything that applies to the grid's DisplayLayout, including band and column definitions. Which approach you take will depend on what you're currently doing in InitializeDataSource and whether or not there's a reasonable, other place to do the same tasks.
You can still re-bind the grid later in the page lifecycle.
It's important to note that the grid will need to bind twice. The grid will need to handle InitializeDataSource once with its "original" data source, particularly to handle any changes that may have been made to that data source. Later in the lifecycle (such as a button click), you'd re-set the grid's DataSource or DataSourceID, then call its DataBind() method.
It's also important to note that you'll need to somehow track which data source your grid is bound to, and to re-establish that connection in the InitializeDataSource event. However you store it, the value must be available during the grid's AJAX callbacks so that it can load additional rows on-demand. Implementations I've seen usually involve using Session variables, either to store the whole data source (such as for small DataTable controls) or a string that allows the program to identify what it needs to bind to (such as a table name and/or specific filter conditions). This value needs to be used during the grid's InitializeDataSource event, and needs to be updated whenever the user changes what data source to bind to