Caching issue: ascx/usercontrol's programmatic Page.Title ignored during cached load of page

531 views Asked by At

The problem is: When a user vists Default.aspx, the page loads the cached version of the Blog.ascx content (because it has hit the same page again within 600 seconds), the Page.Title code is not executed therefore the title remains empty instead of having <title>Title of Blog Post</title> like when it freshly loads the page the first time.


My asp.net website has Blog.ascx to handle loading the content of an individual blog post. Default.aspx contains the reference to and uses the Blog.ascx The Blog.ascx page has custom caching:

    <%@ OutputCache Duration="600" VaryByParam="None" VaryByCustom="Url" %>

The custom caching, located at global.asax.cs is:

    public override string GetVaryByCustomString(HttpContext context, string custom)
    {
        switch (custom.ToUpper())
        {
            case "URL":
                return context.Request.Url.ToString().ToLower().Trim();
            default:
                throw new NotImplementedException();
        }
    }

The Blog.ascx.cs Page_Load event handles the programmatic tag's value/content

    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Title = "Title of Blog Post";
    }

Any suggestions?

1

There are 1 answers

4
the_hoof On

Have you considered using UserControls? If you created a UserControl for the content of you page, you could move the OutputCache declaration to the control, thus freeing up the Page_Load event to set the Page title every visit.