Asp.net 如何对动态创建的控件使用部分缓存?

Asp.net 如何对动态创建的控件使用部分缓存?,asp.net,caching,Asp.net,Caching,我使用PartialCaching属性声明了一个控件,如下所示: [PartialCaching(60 * 60 * 12)] public class MyControl : Control { // control contents ... } if (myControlIsCached) { var ctl = ???; // something that represents the cached control // e.g. c

我使用PartialCaching属性声明了一个控件,如下所示:

[PartialCaching(60 * 60 * 12)]
public class MyControl : Control {
    // control contents ...
}
if (myControlIsCached) {
    var ctl = ???; // something that represents the cached control
                   // e.g. could be: new LiteralControl( myControlCachedData )
    this.Controls.Add( ctl );
}
else {
    var ctl = new MyControl();
    // setup control ...
    this.Controls.Add( ctl );
}
Control possiblyCachedControl = LoadControl("path to control");
MyControlType control = null;
if (possiblyCachedControl is MyControlType)
{
    //control wasn't cached
    control = possiblyCachedControl as MyControlType;
}
else if (possiblyCachedControl is PartialCachingControl && ((PartialCachingControl)possiblyCachedControl).CachedControl != null)
{
    //control was cached
    control = (MyControlType)((PartialCachingControl)possiblyCachedControl).CachedControl;
}
if (control != null)
{
    //use the control
}
但我在代码中创建它,使用new关键字。问题是,如果控件在缓存中,我下次不能再创建该控件,但我需要将该控件添加到页面层次结构中,否则将不会呈现任何内容。我在伪代码中需要如下内容:

[PartialCaching(60 * 60 * 12)]
public class MyControl : Control {
    // control contents ...
}
if (myControlIsCached) {
    var ctl = ???; // something that represents the cached control
                   // e.g. could be: new LiteralControl( myControlCachedData )
    this.Controls.Add( ctl );
}
else {
    var ctl = new MyControl();
    // setup control ...
    this.Controls.Add( ctl );
}
Control possiblyCachedControl = LoadControl("path to control");
MyControlType control = null;
if (possiblyCachedControl is MyControlType)
{
    //control wasn't cached
    control = possiblyCachedControl as MyControlType;
}
else if (possiblyCachedControl is PartialCachingControl && ((PartialCachingControl)possiblyCachedControl).CachedControl != null)
{
    //control was cached
    control = (MyControlType)((PartialCachingControl)possiblyCachedControl).CachedControl;
}
if (control != null)
{
    //use the control
}
正确的做法是什么


谢谢大家。

我相信您希望做这样的事情:

[PartialCaching(60 * 60 * 12)]
public class MyControl : Control {
    // control contents ...
}
if (myControlIsCached) {
    var ctl = ???; // something that represents the cached control
                   // e.g. could be: new LiteralControl( myControlCachedData )
    this.Controls.Add( ctl );
}
else {
    var ctl = new MyControl();
    // setup control ...
    this.Controls.Add( ctl );
}
Control possiblyCachedControl = LoadControl("path to control");
MyControlType control = null;
if (possiblyCachedControl is MyControlType)
{
    //control wasn't cached
    control = possiblyCachedControl as MyControlType;
}
else if (possiblyCachedControl is PartialCachingControl && ((PartialCachingControl)possiblyCachedControl).CachedControl != null)
{
    //control was cached
    control = (MyControlType)((PartialCachingControl)possiblyCachedControl).CachedControl;
}
if (control != null)
{
    //use the control
}

就是这样,但我所需要的几乎没有差别。在我的案例中,我需要将“MyControl”加载到我的另一个控件中。LoadControl方法是页面的成员,所以我是这样做的:this.Page.LoadControl(typeof(MyControl),null)。。。现在我的问题解决了,谢谢!我使用了相同的代码,但是我的控件没有加载,页面是空白的,这可能是什么问题?