Friday, August 29, 2008

Refreshing the Data in ObjectDataSource Dynamically

In my previous post, you have seen the paging example, but there is one small problem you may face, if you are using ControlParameter or QueryStringParameter, for instance take a scenario when you are using ASP.NET AJAX and you have GridView and Search button in update panel.

On page load, the ObjectDataSource will take the default parameters from the DropDownList and QueryString, but problems comes when we want to click Search Button to get the changed values of the DropDownList or the QueryString without posting the page again.

Generally to refresh the data of ObjectDataSource you often write


On Click of Search Button.

But hold on this is only good if you want to see the changed data from the database or Rebind the ObjectDataSource, but this will not pass the current value from the controls or query string as a parameter to the database Query or Procedure. Resulting in which you will keep getting the existing query result only, so here is the actual code below for which I have written such a big story.


Yes just one line code, don't worry this internally calls the ObjectDataSource1.Select(), but will give you the desired result, I hope this will help you.




Anonymous said...

It does not work for me. I have a GridView (GridView1) connected to an ObjectDataSource. Calling GridView1.DataBinding() keeps the old data.

Anonymous said...

Same here. When the the grid is a user control on the parent page this does not work along with object data source... still trying...

Anonymous said...

You need both.

I have an ObjectDataSource with SessionParameters for SelectParameters, and it doesn't automatically requery if its session values don't change. (Which is good, as some postbacks might be for purposes other than refreshing a grid.)

In your Search button handler you need the Select() to force the ObjectDataSource to requery the business object, and the DataBind() to force your ListView in my case to requery the ObjectDataSource.

Post a Comment