0
votes

I have a GridView bound to ObjectDataSource. I see, the SelectMethod and the SelectCountMethod are fired twice. In the GridView RowDataBound I have gv.ShowFooter = false; When I comment this line, the events are fires only once. Why is that happening? How to work around it? I don't understand, why hiding one element in the databound control results is rebinding the ObjectDataSource?

1

1 Answers

0
votes

RowDataBound event gets fired when GridView gets data bound (that means firing of SelectMethod).

Now, toggling properties like ShowFooter requires grid to re-create rows and it means binding the data again. That's why object data source will get triggered again.

Solution will be to set ShowFooter property earlier (instead of RowDataBound). If that's not feasible then put the logic in your object data source class to the cache the data so that you don't have to visit data store twice. For example,

// Code Behind Class
public partial class MyPage : System.Web.UI.Page
{

  private object _data;

  public static object SelectData()
  {
     // get the current page instance
     var page = HttpContext.Current.CurrentHandler as MyPage;
     if (null != page._data)
     {
         return page._data;
     }

     // logic to retrieve the data
     ...
     _data = ...
     return _data;
  }

...


  private void RefreshGrid()
  {
     _data = null; // force the data-source to go to database again
     grid.DataBind();
  }
}

Disclaimer: un-tested code only for illustration purpose

So, in above code, a static method for page code-behind is used to getting the data. And a local variable in the page class is used for caching the data. Also note for refreshing the grid, you may need to clear the variable before calling DataBind method on grid.