Fetch cached control in ASP.NET

174 views Asked by At

I am using the following in ASP.NET webcontrols :

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

This means that the control will be null on reload if it is already added to the cache. The problem is that on some page I want to hide this control and it would be great if this could be done from the MasterPage codebehind file(where it is loaded).

I have tried this :

if (Request.AppRelativeCurrentExecutionFilePath.ToLower().EndsWith("/sites/MySite/default.aspx") || Request.AppRelativeCurrentExecutionFilePath.ToLower().EndsWith("MySite.net"))
{
   if(topGames_Mini1 != null)
   { 
       //Load control 
        topGames_Mini1.visible=true; 
   }
} 
else
{
    Page.LoadControl("topGames_Mini1").Visible = false;
}

It will however throw the following exception in the else :

The file '/Bradspel/sites/MySite/community/topGames_Mini1' does not exist.

1

There are 1 answers

0
R.C On BEST ANSWER

you should better place the UserControl inside a Placeholder control. Then simply hide/show the Placeholder depending on your conditions.

The Placeholder does not render any tags for itself, so there is NO overhead of outer HTML tags.

I Assume you must have registered your UserControl in your Master page. So, place the userControl now inside a PlaceHolder control.

<asp:ContentPlaceHolder ID="MainContent" runat="server"><!-- Of Master Page -->
             <asp:PlaceHolder ID="place1" runat="server">
                  <uc1:Test ID="Test1" runat="server" /><!-- Our User Control-->
             </asp:PlaceHolder>
</asp:ContentPlaceHolder>

and in Code behind::

protected void Page_Load(object sender, EventArgs e)
    {
      if( _Some_Condition_)
       place1.Visible = true; 
      else
      // Hide PlaceHolder and thus all controls inside it
       place1.Visible = false; 

    }