Hide surrounding panel if cms:listmenu has no items in aspx template

Tim G. asked on May 10, 2018 10:22

I'm trying to hide a wrapping panel (div) if the contained cmslistmenu item has no items in it. I've set the property for 'HideControlForZeroRows' to true for the cmslistmenu and this seems to be showing and hiding as expected.

In my code behind (.aspx.cs) I have the following logic in the pageLoad event:

if (subNavigation.Visible == false)
    subNavigationWrapper.Visible = false;

However the subnavigation wrapper always shows regardless of the visibility of the 'subNavigation'.

Is there an approach for this that would work? The whole code block looks something like this:

<asp:Panel ID="subNavigationWrapper" runat="server" CssClass="sub-navigation--wrapper" Visible="true">
                <div class="inner">
                    <div class="sub-navigation">
                        <span class="breadcrumb"><cms:CMSBreadCrumbs ID="CMSBreadCrumbs1" runat="server" /></span>
                        <div class="sub-navigation-nav">
                            <cms:listmenu runat="server" ID="subNavigation" WhereCondition="MenuItemGroup LIKE 'subnav'" Path="./%"  MaxRelativeLevel="4" RenderCssClasses="false" HideControlForZeroRows="true" ZeroRowsText="" CacheDependencies="##DEFAULT##" />
                        </div>
                    </div>
                </div>
            </asp:Panel>

Recent Answers


Peter Mogilnitski answered on May 10, 2018 16:00 (last edited on May 10, 2018 16:03)

I think you need to clone cmslistmenu control and customize it (or use the condition below in your logic). In the code behind there is an event in cmslistmenu.acx.cs which sets visiblilty:

/// <summary>
/// OnPrerender override (Set visibility).
/// </summary>
protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);

    Visible = menuElem.Visible;

    if (DataHelper.DataSourceIsEmpty(menuElem.DataSource) && (menuElem.HideControlForZeroRows))
    {
        Visible = false;
    }
}  

Just add you subNavigationWrapper.Visible = false; in there if youare customizing it or use the condition above in your code

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.