有父用户控件,如下所示。
<%@ 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属性,因为子控件的缓存副本一旦可用就会使用。
我如何解决这个问题?
谢谢!
发布于 2012-10-19 16:46:51
看起来使用GetVaryByCustomString才是最好的选择。我的概念证明包括以下几点:
MatchDescription.GetVaryByCustomString,这是一个承载控件的简单窗体。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文件之间传递description值的方法。
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
https://stackoverflow.com/questions/12823867
复制相似问题