So the past few weeks I’ve been working on a page that pulls from a database and creates a gridview based off of drop down lists i had in place, so using ajax the gridview would change dependent on the ddls. The problem I was having was that while testing I’d change the ddl value, I’d follow a link in the given gridview and then i’d press back to check to see if the same gridview was displayed.
The gridview displayed however would be a different dataset than what i had just linked from, what would be displayed is a dataset that i had linked from earlier in my testing or none at all if it was my first attempt. I’d have to reload the page to view what i had cached programmatically.
So to fix this, i made an addendum to my ddl event- i tried to store the gridview and the ddl value in the cache and then on page load I checked to see if there existed a cache with those key values and databound those values: hoping that when the user pressed back the correct gridview would be displayed. It didn’t work; i had to force the cache to expire before my algorithm worked.
So were the two gridviews both being stored in the cache then and the browser was merely using it’s own key/value mapping initially? If so, how come even though i’m forcing the cache to expire, my programmatic caching works? Any clarifications?
as requested, here’s the code involved,
//here's where i forced the cache to expire
protected override void OnInit(EventArgs e)
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
Response.Cache.SetExpires(DateTime.MinValue);
base.OnInit(e);
}
And here’s another relevant snippet
//Here's the code i used to check the value's i put in the cache
if (Cache.Get("gvClassSchedule") != null)
{
IQueryable CachedgvClassSchedule = (IQueryable)Cache.Get("gvClassSchedule");
CachedClassScheduleGv(CachedgvClassSchedule);
}
private void CachedClassScheduleGv(IQueryable CachedgvClassSchedule)
{
gvClassSchedule.DataSource = CachedgvClassSchedule;
ddlClass.SelectedValue = Cache.Get("ddlClassValue").ToString();
gvClassSchedule.DataBind();
gvClassSchedule.Visible = true;
divClassSchedule.Visible = true;
}
where i’m storing my ddl value
protected void OnChange_Class(object sender, EventArgs e)
{
if (ddlClass.SelectedValue != "-1")
{
classChanged();
Cache.Insert("ddlClassValue", ddlClass.SelectedValue);
}
else
{
divClassSchedule.Visible = false;
divExperiment.Visible = false;
}
}
where i’m storing my dataset
private void classChanged()
{
if (ddlClass.SelectedValue != "-1")
{
Class course = dc.Classes.Single(p => p.ID == Convert.ToInt32(ddlClass.SelectedValue));
ProfHasClassInSemester phcs = dc.ProfHasClassInSemesters.Single(p => p.ClassID.ToString() == ddlClass.SelectedValue && p.SemesterID.ToString() == ddlSemester.SelectedValue);
lblClassAndProfessor.Text = ddlSemester.SelectedItem.Text + ": " + course.ClassName + " - " + phcs.Professor.ProfessorName;
divClassSchedule.Visible = true;
divExperiment.Visible = true;
divAssign.Visible = false;
addMode();
IQueryable queryClassSchedule = fillClassScheduleGv(course);
fillExperimentsddl();
Session["ddlClass"] = ddlClass.SelectedValue;
Cache.Remove("gvClassSchedule");
Cache.Insert("gvClassSchedule", queryClassSchedule);
}
}
You are using different caches in the different code examples.
The
Response.Cacheis aHttpCachePolicyclass – it sets HTTP headers that govern caching – this controls proxies and the browser cache.The second code snippet in the in memory cache – the
Cacheclass from theSystem.Web.Cachingnamespace. This lives in the IIS memory space.In regards to the first – if you didn’t originally set the cache headers, you were probably loading the page from proxy/browser caches all this time. The server would not have been queries at all.