如何LoadControl使用VaryByControl的OutputCache,对于指定的属性值的控制 [英] How to LoadControl a control that uses VaryByControl OutputCache, specifying values for properties
问题描述
我已经得到了应使用缓存,使用 VaryByControl
用户控件。在的.ascx
文件看起来是这样的:
I've got a user control that should use caching, with VaryByControl
. The .ascx
file looks like this :
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TestControl.ascx.cs" Inherits="mynamespace.TestControl" %>
<%@ OutputCache Duration="10" Shared="true" VaryByControl="Test" %>
<p id="SomeText" runat="server">Nothing</p>
在系统testControl
类code隐藏文件中有一个 INT测试{...}
财产和的Page_Load()
事件处理程序,填补与 SomeText
段落:
The TestControl
class in the code-behind file has a int Test {...}
property and an Page_Load()
event handler that fills the SomeText
paragraph with:
SomeText.InnerText = string.Format(@"Test={0} at {1}", Test, DateTime.Now)
我有一个看起来像这样的的.aspx
文件:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestPage.aspx.cs" Inherits="mynamespace.TestPage" %>
<%@ Register TagPrefix="xxx" TagName="TestControl" Src="Controls\TestControl.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<xxx:TestControl Test="6" runat="server" />
<xxx:TestControl Test="7" runat="server" />
<hr />
<asp:PlaceHolder ID="Suport" runat="server" />
</body>
</html>
两个&LT; XXX:系统testControl&GT;
标签正确加载系统testControl
的实例与测试
设置为预期值,我可以刷新浏览器几次,我可以看到缓存适当做它的工作。
The two <xxx:TestControl>
tags properly load instances of TestControl
with Test
set to the expected value, I can refresh the browser a few times and I can see the cache properly doing it's job.
现在我想填补了&LT; ASP:占位符ID =询问服务/&GT;
与的某些情况下,系统testControl
,使用不同的测试
值,即应该从正确的缓存中受益。我试图使用 LoadControl
方法,但我不能找到一种方法来指定测试
属性。我希望这样的方法存在,所有的 asp.net后
code加载的.aspx
页设法找到合适的缓存控制。我得到的是 PartialCachingControl
的一个实例,而不 CachedControl
初始化,并在运行时呈现的系统testControl
显示测试
有默认值 0
。
Now I'd like to fill the <asp:PlaceHolder ID="Suport" />
with some instances of TestControl
, using varying Test
values, that should all benefit from proper caching. I'm trying to use the LoadControl
method, but I can't find a way to specify a value for the Test
property. I expect such a method to exist, after all asp.net
code loading the .aspx
page manages to find the proper cached control. All I get is an instance of PartialCachingControl
without CachedControl
initialized and at runtime the rendered TestControl
shows Test
has the default value of 0
.
这是我的的.aspx
的Page_Load()
事件处理程序的样子:
This is how my .aspx
Page_Load()
event handler looks like:
protected void Page_Load(object sender, EventArgs e)
{
PartialCachingControl tc = (PartialCachingControl) LoadControl(@"Controls\TestControl.ascx");
if (tc.CachedControl != null)
((TestControl)tc.CachedControl).Test = 67;
Suport.Controls.Add(tc);
}
修改
我可以解决该问题通过缓存整个页面,但它只是似乎很奇怪,我无法找到一个方法来做到这一点的这样的。特别是由于通过ASPX文件的工作调用控制预期(证明有一种方法)。
Edit
I could work around the problem by caching the whole page, but it just seems odd that I can't find a way to do it this way. Especially since invoking the control through the ASPX file works as expected (proving there's a way).
嗯,没有答案为止。我开始赏金,希望它得到多一点的关注。
Hmm, no answers so far. I started a bounty, hopefully it gets a bit more attention.
推荐答案
我想你误会了VarByControl属性,它并没有告诉缓存后,在控制的属性改变,但对控制上的ID页。 <一href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.outputcacheparameters.varybycontrol.aspx\"相对=nofollow>这是从MSDN 文:
I think you have misunderstood the VarByControl-property, it does not tell the cache to change upon a property on the control, but on the ID of controls on the page. Here is the text from MSDN:
该VaryByControl属性设置为完全限定的控件标识符,其中标识符是从顶级父控制开始控件ID的串联,并以美元符号($)字符分隔。
The VaryByControl property is set to fully qualified control identifiers, where the identifier is a concatenation of control IDs starting from the top-level parent control and delimited with a dollar sign ($) character.
在你的情况下,你也许可以设置VaryByCustom是不是VaryByControl并生成从测试属性值缓存键并改变它,如果它改变。
In your case you can maybe set VaryByCustom instead of VaryByControl and generate a cache key from the Test-property-value and vary it if it changes.
这篇关于如何LoadControl使用VaryByControl的OutputCache,对于指定的属性值的控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!