簡體   English   中英

如何在動態創建的控件中使用部分緩存?

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

我有一個用PartialCaching屬性聲明的控件,如下所示:

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

但是我使用new關鍵字在代碼中創建它。 問題是,如果控件在高速緩存中,則下次不能再創建該控件,而是需要將控件添加到頁面層次結構中,否則將無法呈現任何內容。 我需要的偽代碼是這樣的:

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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM