How to cache user controls that are inside code blocks?

67 Views Asked by At

I have an aspx webpage with embedded code block that displays one of the two instances of the same user control. Simplified code:

    <form id="form1" runat="server">
        <div>
            <%if (true)
                { %>
            <UC:UserControl runat="server" ID="ucFirst"></UC:UserControl>
            <%}
                else
                { %>
            <UC:UserControl runat="server" ID="ucSecond"></UC:UserControl>
            <%} %>
        </div>
    </form>

Registered user control is cached:

<%@ OutputCache Duration="60" VaryByParam="none" %>

However, only ucFirst gets cached, while ucSecond goes through Page_Load and Page_PreRender each time I refresh the page, even though it doesn't appear in rendered HTML code at all. Is it possible to either prevent ucSecond entirely from loading, or load it just once like ucFirst and keep it cached?

0

There are 0 best solutions below