Frequently you want to make sure that while Caching the User Control you have successfuly went through all the code paths in the code behind file. For e.g: you don't want to cache the User Control if there was an Error connecting to database that fills the DataGrif i.e. an empty resultset or worse an error is thown.
What you really want is to try to regenerate the output on next request i.e Invalidate the Cache for current request. Whidbey will not have this issue and you could progamitically control User Control cache as well. But as of Now there is no easy way to invalidate user Control Cache. So I came up with this solution
Put a flag on the User Control code behind some thing like.
bool AllWentWell = false;
and when I have processed every thing and the control flow through
normally through all if conditions then at the end set this flag too:
AllWentWell = true;
Now at the very end of my User Control PreRender event I will set a
Dependency which will expire after a second if AllWentWell is still
false .... That should do the trick ....
private void Navig_PreRender(object sender, EventArgs e)
{
...... Do what ever need to be
done ....
...... At the end
.................
if ( !AllWentWell )
{
BasePartialCachingControl c
= Parent as BasePartialCachingControl;
if (c != null) {
c.Dependency = new
CacheDependency(on another data Cache Key that expires in a second );
}
}