首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >仅缓存子用户控件

仅缓存子用户控件
EN

Stack Overflow用户
提问于 2012-10-11 00:14:18
回答 1查看 454关注 0票数 1

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

代码语言:javascript
复制
<%@ 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>

子The控件具有在父控件的Page_Load中设置的公共属性MatchDescription。我希望基于MatchDescription属性缓存子控件的多个版本。

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

我如何解决这个问题?

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2012-10-19 16:46:51

看起来使用GetVaryByCustomString才是最好的选择。我的概念证明包括以下几点:

  • WebUserControl.ascx:测试控件。它有一个公共属性MatchDescription.
  • Global.asax:来重写method.
  • WebForm.aspx:的GetVaryByCustomString,这是一个承载控件的简单窗体。

WebUserControl.ascx

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

代码语言:javascript
复制
<%@ OutputCache Duration="120" VaryByParam="none" VaryByCustom="MatchDescription" %>

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

WebUserControl.ascx.cs

代码语言:javascript
复制
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文件添加到您的项目中,并添加以下方法:

代码语言:javascript
复制
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文件之间传递description值的方法。

WebForm.aspx.cs

代码语言:javascript
复制
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方法中访问它将需要以下形式的代码:

代码语言:javascript
复制
    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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12823867

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档