简体   繁体   English

如何在动态创建的控件中使用部分缓存?

[英]How to use Partial Caching with dynamically created controls?

I have a control declared with PartialCaching attribute, like this: 我有一个用PartialCaching属性声明的控件,如下所示:

[PartialCaching(60 * 60 * 12)]
public class MyControl : Control {
    // control contents ...
}

but I create it in code, using new keyword. 但是我使用new关键字在代码中创建它。 The problem is that if the control is in cache I must not create the control again the next time, but I need to add the control to the page hierarchy, otherwise nothing is going to be rendered. 问题是,如果控件在高速缓存中,则下次不能再创建该控件,而是需要将控件添加到页面层次结构中,否则将无法呈现任何内容。 What I need in pseudo-code is something like this: 我需要的伪代码是这样的:

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 );
}

What is the correct way of doing it? 正确的做法是什么?

Thanks people. 谢谢大家

I believe you are looking to do something like this: 我相信您正在寻找做这样的事情:

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM