I am using the PartialCaching attribute on the base class of a user control.
I would like the cached controls to vary based on the properties set on the control inst
I'm posting a new answer to this very old question because the accepted answer is woefully inaccurate. This correct answer is:
I blogged about this recently right here: http://tabeokatech.blogspot.be/2014/09/outputcache-on-user-controls.html .
A way you could make this work is call the builder method for the PartialCachingControl yourself in code-behind, and embed the property value you want to vary by in the guid parameter:
// PhControls is an asp:PlaceHolder
protected void Page_Init(object sender, EventArgs e)
{
for (int i = 0; i < 3; i++)
{
System.Web.UI.StaticPartialCachingControl.BuildCachedControl(PhControls, String.Format("Test{0}", i), String.Format("1234567{0}", i), 180, null, null, null, null, new System.Web.UI.BuildMethod(GetBuilderDelegate(i)), null);
}
}
public Func GetBuilderDelegate(int number)
{
return delegate()
{
var control = (UserControls.Test)LoadControl("~/UserControls/Test.ascx");
control.Number = number;
return control;
};
}
That neatly takes care of varying caching duration in code-behind as well. Make sure you remove the OutputCache directive from the markup in the ascx though when you do this. Otherwise the LoadControl gets you another PartialCachingControl and the cast fails.