How to LoadControl a control that uses VaryByControl OutputCache, specifying values for properties
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>
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)
I've got a .aspx
file that looks like this:
<%@ 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>
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.
Now I'd like to fill the &开发者_运维百科lt;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
.
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);
}
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).
Edit 2
Hmm, no answers so far. I started a bounty, hopefully it gets a bit more attention.
To get a control participate in the full page life cycle it shall be added in the Init event or the CreateChildControls method rather than to add it on Load. Since VaryByControl needs fully qualified control identifiers to work it must be initialized before the page cycle begins.
Something similar to this:
protected override void OnInit(EventArgs e) {
var testControl = LoadControl(@"TestControl.ascx");
testControl.ID = "TestControl";
Suport.Controls.Add(testControl);
base.OnInit(e);
}
protected override void OnLoad(EventArgs e) {
TestControl testControl = GetTestControl("TestControl");
if(testControl != null){ //If it is null it is cached and can not be changed
testControl.Test = 242;
}
base.OnLoad(e);
}
private TestControl GetTestControl(string name) {
var control = this.Suport.FindControl(name);
var partialCachedControl = control as PartialCachingControl;
if(partialCachedControl != null) {
control = partialCachedControl.CachedControl;
}
return control as TestControl;
}
Since the output is cached per control you can not change the control until the cache is cleared. If you want to change the value and regenerate the content you either have to get the cache cleared or create a new control (with a new ID). One way to clear the cache is to use VaryByCustom instead and generates a cache key that changes if your Test-value is changing.
Also remember to implement INamingContainer interface on your test-control to avoid naming conflicts between the different objects. To do this, just add the interface to the control, like this:
public class TestControl: WebControl, INamingContainer {}
You have to swap 2 lines in order to make your code work :
PartialCachingControl tc = (PartialCachingControl) LoadControl(@"Controls\TestControl.ascx");
Suport.Controls.Add(tc);
if (tc.CachedControl != null)
((TestControl)tc.CachedControl).Test = 67;
As soon as you add the control, the cached control is initialized.
E.G.
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:
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.
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.
精彩评论