Like a LoadControl control that uses a VaryByControl OutputCache by setting values ​​for properties

I have a user control that should use caching, c VaryByControl. The file is .ascxas follows:

<%@ 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 class TestControlin the code file has a property int Test {...}and an event handler Page_Load()that fills the paragraph SomeText:

SomeText.InnerText = string.Format(@"Test={0} at {1}", Test, DateTime.Now)

I have a file .aspxthat 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>

Two tags <xxx:TestControl>correctly load instances TestControlwith Testset to the expected value, I can update the browser several times, and I see how the cache does this job correctly.

<asp:PlaceHolder ID="Suport" /> TestControl, Test, . LoadControl, Test. , , asp.net .aspx . , , 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

, . , , .

+5
3

, VarByControl, , , . MSDN:

VaryByControl , , ($).

VaryByCustom VaryByControl Test-property , .

+2

, , Init CreateChildControls, Load. VaryByControl , .

- :

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;
}

, , . , , ( ID). - VaryByCustom , , Test.

INamingContainer , . , :

public class TestControl: WebControl, INamingContainer {}
+3

2 , :

PartialCachingControl tc = (PartialCachingControl) LoadControl(@"Controls\TestControl.ascx");
Suport.Controls.Add(tc);
if (tc.CachedControl != null)
    ((TestControl)tc.CachedControl).Test = 67;            

, .

.

+3

All Articles