仅缓存子用户控件

时间:2012-10-10 16:14:18

标签: asp.net caching user-controls outputcache

有父用户控件,如下所示。

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="TestUserControl.ascx.cs" Inherits="TestUserControl" %>
<%@ Register Src="~/UserControls/ChildUserControl.ascx" TagName="ChildUserControl" TagPrefix="FLI" %>
<div>    
    <FLI:ChildUserControl ID="child1" runat="server"/>    
</div>

子usecontrol具有pulic属性MatchDescription,它在父控件的Page_Load中设置。我想根据MatchDescription属性缓存多个版本的子控件。

问题是,MatchDescription属性不能在Page_Load中设置,因为子控件的缓存副本一旦可用就会被使用。

我该如何解决这个问题?

谢谢!

1 个答案:

答案 0 :(得分:1)

看起来使用GetVaryByCustomString是这样的方式。我的概念证明包括以下内容:

  • WebUserControl.ascx:测试控件。它有一个公共财产MatchDescription
  • Global.asax:覆盖GetVaryByCustomString方法。
  • WebForm.aspx:托管控件的简单表单。

<强> WebUserControl.ascx

将以下内容添加到控件的标记中:

<%@ OutputCache Duration="120" VaryByParam="none" VaryByCustom="MatchDescription" %>

指定缓存控件的持续时间(以秒为单位),VaryByCustom="MatchDescription"指定我们将缓存的参数的名称。

<强> WebUserControl.ascx.cs

public partial class WebUserControl1 : System.Web.UI.UserControl
{
    public string MatchDescription { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        object description = this.Context.Application["MatchDescription"];

        if (description != null)
        {
            this.MatchDescription = description.ToString();
        }
        else
        {
            this.MatchDescription = "Not set";
        }

        Label1.Text = "Match description: " + this.MatchDescription;
    }
}

这将检查MatchDescription值是否存在。由于父页面中的代码的工作方式,你永远不应该看到“未设置”,但在你的实现中,它可能是有用的,以防万一没有设置值。

<强> Global.asax中

Global.asax文件添加到项目中,并添加以下方法:

public override string GetVaryByCustomString(HttpContext context, string custom)
{
    if (custom == "MatchDescription")
    {
        object description = context.Application["MatchDescription"];

        if (description != null)
        {
            return description.ToString();
        }
    }

    return base.GetVaryByCustomString(context, custom);
}

这是检查与缓存控件关联的MatchDescription的位。如果未找到,则将按正常方式创建控件。使用context.Application是因为我们需要一种方法来在父页面,用户控件和global.asax文件之间传递描述值。

<强> WebForm.aspx.cs

public partial class WebForm : System.Web.UI.Page
{
    private static string[] _descriptions = new string[]
    {
        "Description 1",
        "Description 2",
        "Description 3",
        "Description 4"
    };

    protected override void OnPreInit(EventArgs e)
    {
        //Simulate service call.
        string matchDescription = _descriptions[new Random().Next(0, 4)];
        //Store description.
        this.Context.Application["MatchDescription"] = matchDescription;

        base.OnPreInit(e);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        var control = LoadControl("WebUserControl.ascx") as PartialCachingControl;
        this.Form.Controls.Add(control);

        //Indicate whether the control was cached.
        if (control != null)
        {
            if (control.CachedControl == null)
            {
                Label1.Text = "Control was cached";
            }
            else
            {
                Label1.Text = "Control was not cached";
            }
        }
    }
}

请注意,在此代码中,我使用OnPreInit方法进行/模拟服务调用。这是必要的,因为它发生在GetVaryByCustomString方法之前的页面生命周期中。

请注意,如果控件已被缓存,例如,在Page_Load方法中访问它将需要此格式的代码:

    if (control is PartialCachingControl &&
        ((PartialCachingControl)control).CachedControl =!= null)
{
    WebUserControl1 userControl = (WebUserControl1)((PartialCachingControl)control).CachedControl;
}

<强>参考文献:

我的答案灵感来自:Any way to clear/flush/remove OutputCache?

我在这个问题中找到了Pre_Init提示: Output Caching - GetVaryByCustomString based on value set in PageLoad()

此知识库文章讨论了PartialCachingControl.CachedControl属性始终返回null的原因: http://support.microsoft.com/kb/837000

相关问题