Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
280
WebGrid: Any way to call get datasource to rebind?
posted

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

Parents
  • 45049
    posted

    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

Reply Children