【问题标题】:Fetch cached control in ASP.NET在 ASP.NET 中获取缓存的控件
【发布时间】:2013-12-25 17:57:41
【问题描述】:

我在 ASP.NET webcontrols 中使用以下内容:

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

这意味着如果控件已经添加到缓存中,则重新加载时控件将为空。问题是在某些页面上我想隐藏这个控件,如果这可以从 MasterPage 代码隐藏文件(加载它的位置)中完成,那就太好了。

我已经试过了:

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

但是它会在 else 中抛出以下异常:

文件“/Bradspel/sites/MySite/community/topGames_Mini1”没有 存在。

【问题讨论】:

    标签: asp.net caching webforms outputcache web-controls


    【解决方案1】:

    您最好将 UserControl 放在 Placeholder 控件中。然后根据您的条件简单地隐藏/显示占位符。

    占位符不会为自己呈现任何标签,因此没有外部 HTML 标签的开销。

    我假设您必须在您的母版页中注册您的 UserControl。因此,现在将 userControl 放置在 PlaceHolder 控件中。

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

    在后面的代码中::

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

    【讨论】:

      猜你喜欢
      • 2010-10-08
      • 1970-01-01
      • 2011-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多