Get output cache for working in ASP.net using webusercontrol

I have one webusercontrolwith a public property int SelectedCatID. I use this control on other pages and in other controls as follows:

<NewStore:LeftMenuLinks runat="server" SelectedCatID="<%#CatIDToSelect%>" />

How to infer the cache of this control based on SelectedCatID? Everything I tried fails.

The closest I got is to cache it, but it doesn't change to SelectedCatID, leaving the same menu item selected until the cache expires. Without caching, management works as expected.

+4
source share
3 answers

, VaryByControls, , . , , . : : http://tabeokatech.blogspot.be/2014/09/outputcache-on-user-controls.html.

, , VaryByControls VaryByParams : POST. , - , , - VaryByControls . : PartialCaching ASP.NET.

.

, , , , - , . , PartialCachingControl . PartialCachingControl , .

, :

  • 1 , VaryByCustom. , , , , , :

    interface INumberProvider
    {
        int GetNumber();
    }
    
    // and the page:
    public partial class _Default : Page, INumberProvider
    {
        public int GetNumber()
        {
            return this.SomeNumberPropertyOrWhatever;
        }
    ...
    

    Global.asax INumberProvider :

        public override string GetVaryByCustomString(HttpContext context, string custom)
        {
            if (custom == "INumberProvider")
            {
                var page = context.CurrentHandler as INumberProvider;
    
                if (page != null)
                {
                    return page.GetNumber().ToString();
                }
            }
            return base.GetVaryByCustomString(context, custom);
        }
    

    , , :

    OutputCache Duration = "180" VaryByCustom = "INumberProvider" VaryByParam = "None" Shared = "true"

    , . , :

  • , WebControl. , HttpContext.Current.Cache , SelectedCatID. PartialCachingControl. 3:
  • ,
+2
<%@ OutputCache Duration="60" VaryByParam="SelectedCatID" %>

youre <% # CatIDToSelect% > ex? SelectedCatID = 12 UserControl , , , Request.Param [ "SelectedCatID" ].

- ( )

/usercontrol, :

<%@ OutputCache duration="120" varybyparam="None" varybycustom="SelectedCatID" %>

Gloabal.asax:

public override string GetVaryByCustomString(HttpContext context, string custom)
{
    if(custom == "SelectedCatID")
    {
        return CatIDToSelect;
    }
    return String.Empty;
}
0

, 500 . , .

. Render, . UserControl . :

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TestUC.ascx.cs" 
    Inherits="Webforms_Test.UserControls.TestUC" %>
<div>
    <asp:Label ID="curTime" runat="server"></asp:Label>
</div>

, ​​ DateTime.Now, . :

public partial class TestUC : System.Web.UI.UserControl
{
    private string cachedOutput = null;
    public bool RenderFromCache = true; // set to false in containing page if this control needs to be re-rendered

    protected void Page_Load(object sender, EventArgs e)
    {
        cachedOutput = HttpContext.Current.Cache["key"] as string;
        if (cachedOutput == null)
        {
            // not found in cache, do the heavy lifting here to setup the control
            curTime.Text = "UC:" + DateTime.Now.ToString("yy-MM-dd hh:mm:ss");
        }
    }
    protected void Page_PreRender(object sender, EventArgs e)
    {
        if (cachedOutput == null || !RenderFromCache)
        {
            RenderFromCache = false;
            StringBuilder b = new StringBuilder();
            HtmlTextWriter h = new HtmlTextWriter(new StringWriter(b));
            this.RenderControl(h);
            cachedOutput = b.ToString();
            HttpContext.Current.Cache.Insert("key", cachedOutput, null, DateTime.UtcNow.AddSeconds(10), TimeSpan.Zero);
            RenderFromCache = true;
        }
    }
    protected override void Render(HtmlTextWriter writer)
    {
        if (!RenderFromCache)
            base.Render(writer);
        else
            writer.Write(cachedOutput);
    }
}

, , , Render . , PreRender Render .

, , , , , .

. . , , .. , ...

0

All Articles