InvalidCastException:System.Web.UI.PartialCachingControl - > OutputCaching时的MyCustomControl

时间:2009-11-21 15:05:41

标签: asp.net user-controls outputcache invalidoperationexception

问题:我无法将OutputCaching与从MyCustomControl派生的控件一起使用。使用Page.LoadControl方法使用数据库中的定义动态加载控件。

当我添加到ascx<%@ OutputCache VaryByParam =“*”Duration =“3600”%>该 抛出“InvalidCastException:System.Web.UI.PartialCachingControl - > MyCustomControl”异常。

我无法修改程序集包含动态加载控制逻辑。有没有办法在派生控件中修复它?

第二个问题是关于iis7和本机输出缓存 - 是否解决了这个问题? (我试图设置几个性能计数器,我看到缓存没有被击中......)

1 个答案:

答案 0 :(得分:0)

如果要动态加载控件,则将它们加载为System.Web.UI.PartialCachingControl。

Control control = Page.LoadControl("/somecontrol.ascx");
this.Controls.Add(control);
MyCustomControl myControl = ((PartialCachingControl)control).CachedControl as MyCustomControl 

此行为在IIS7中完全相同