I've a Gridview control using an ODS(ObjectDataSource) to fetch data. For the best performance and efficiency, I've turned-off the view state of Gridview (i.e. EnableViewstate = "false".
And I've also enabled caching in the associated Objectdatasource. This eliminates as much as 50-60% performance optimization because it eliminates the DB round-trip .. courtesy ODS Caching.
So, after this I got stuck into the famous "ODS sorting" issue but I managed to invent a tricky solution for it and its working fine:
Optimize Pagination & Sorting with ObjectDataSource having EnableCaching = true
Next pagination, it is also working fine. Now, I need to display "Total records: X" at the top of the Gridview. Well, I deployed the following method:
protected void ods_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
if(e.ReturnValue != null && e.ReturnValue.GetType() == typeof(int))
base.setTotalLabel(lblTotal, e.ReturnValue);
}
Don't confuse - base.setTotalLabel is my own method to set the label text with the count. This is also working fine but the issue is that -
Whenever, the ODS fetches data from its Cache it won't trigger the ODS_Selecting or ODS_Select events. They are simply "by-passed" because it takes data from cache. This is when I fail to refresh the Total records count!
I hope I've explained my problem good, this is tricky. I'm ready to do any trick or dirty coding for this because I want to maintain the ODS-caching and I can't rollback changes just because of a few incidental "mis-updates".
Pls help!
I solved this by storing the record count in a session variable during the selected event:
Then in Page_Load I display the record count if the session var is not null:
Now lblRecordCount will show the record count if the data comes from the cache or not
This worked for me I set the CacheKeyDependency and I explicitly set it to a new value whenever I want to refresh. I pass &refresh=1 in querystring if I want to explicitly refresh.
In .aspx -
In code behind -
I have the exact same problem?
A solution I have been doing is to call the select count method again (via BLL) in the gridview databinding method and storing this in a viewstate var then using it to display the number of records. But this leads to two things: