【发布时间】:2011-09-28 07:21:41
【问题描述】:
我有一个应该使用缓存的用户控件,带有VaryByControl。 .ascx 文件如下所示:
<%@ 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 类有一个int Test {...} 属性和一个Page_Load() 事件处理程序,它用以下内容填充SomeText 段落:
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>
两个<xxx:TestControl> 标签正确加载TestControl 的实例并将Test 设置为预期值,我可以刷新浏览器几次,我可以看到缓存正常工作。
现在我想用TestControl 的一些实例填充<asp:PlaceHolder ID="Suport" />,使用不同的Test 值,这都应该受益于适当的缓存。我正在尝试使用LoadControl 方法,但我找不到为Test 属性指定值的方法。我希望存在这样的方法,毕竟加载.aspx 页面的asp.net 代码设法找到适当的缓存控件。我得到的只是PartialCachingControl 的实例,而CachedControl 没有初始化,在运行时呈现的TestControl 显示Test 的默认值为0。
这就是我的 .aspx Page_Load() 事件处理程序的样子:
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 文件调用控件按预期工作(证明有办法)。
编辑 2
嗯,目前还没有答案。我开始了一个赏金,希望它得到更多的关注。
【问题讨论】:
标签: asp.net caching user-controls runtime loadcontrol