Can't select a disabled or unselectable menu item

Viewed 352

I am deploying a solution that I've been working on without issue in debug with the Visual Studio built in IIS. I have two similar projects deployed on the same server with similar menu structures which work fine ASP4.0 MSServer2012R2 SQL2018. For some reason, with this new project, whether I run in localhost or access the new site by url, I am able to hit the main page and login (indicating my DB connection and permissions are all ok), but when I click on any nested link in my menu bar, I get the error:

Can't select a disabled or unselectable menu item. Error

I have found a little bit on this error, but everything points towards issues where "Selected" and "Enabled" were expressly set. This is not the case here. I do have role based authentication enabled, but that works IF it is a top level menu item. As soon as I nest a menu, I get this issue.

This below will cause the issue

<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
  <siteMapNode title="Home" url="~/">
  <!--  <siteMapNode title="Home" url="~/Default.aspx">
    </siteMapNode>   -->
    <siteMapNode title="Get Started" roles="Recruiter,Student,SuperUser">
      <siteMapNode title="About us" url="~/AboutUs.aspx"></siteMapNode>
      <siteMapNode title="Help" url="~/Help.aspx" roles="Recruiter"></siteMapNode>
      <siteMapNode title="Resources" url="~/Resources.aspx"></siteMapNode>
    </siteMapNode>
    <siteMapNode title="Manage Users" url="~/ManageUsers.aspx" roles="Administrator,SuperUser">
    </siteMapNode>
  </siteMapNode>
</siteMap>

This Below will work:

<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
  <siteMapNode title="Home" url="~/">
  <!--  <siteMapNode title="Home" url="~/Default.aspx">
    </siteMapNode>   -->

  <siteMapNode title="About us" url="~/AboutUs.aspx"     roles="Administrator,User,SuperUser"></siteMapNode>
  <siteMapNode title="Help" url="~/Help.aspx" roles="Administrator,SuperUser,User"></siteMapNode>
  <siteMapNode title="Resources" url="~/Resources.aspx"></siteMapNode>

  <siteMapNode title="Manage Users" url="~/ManageUsers.aspx" roles="Administrator,SuperUser">
  </siteMapNode>
</siteMapNode>

The Site map is bound:

<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" ShowStartingNode="false" />

<div class="navbar navbar-inverse navbar-fixed-top">
    <div class="container">
        <div class="navbar-brand navbar-header">
                <asp:Menu ID="MasterPageMenu" runat="server" DataSourceID="SiteMapDataSource1" Orientation="Horizontal" OnMenuItemDataBound="MasterPageMenu_MenuItemDataBound">
                    <DynamicHoverStyle ForeColor="White"  />
                    <LevelMenuItemStyles>
                        <asp:MenuItemStyle CssClass="main_menu" />
                        <asp:MenuItemStyle CssClass="level_menu" />
                    </LevelMenuItemStyles>
                    <StaticHoverStyle ForeColor="White" />
               </asp:Menu>
        </div>

Here is the code behind for that:

protected void MasterPageMenu_MenuItemDataBound(object sender, MenuEventArgs e)
{
    if (SiteMap.CurrentNode != null)
    {
        if (e.Item.Text == SiteMap.CurrentNode.Title)
        {
            if (e.Item.Parent != null)
            {
                e.Item.Parent.Selected = true;
            }
            else
            {
                e.Item.Selected = true;
            }
        }
    }
}

I have spent a couple of days on this now and know it will be something simple. . .but Im banging my head against the wall. . .any thoughts?

--Adding 3/10/20 Here is the complete IIS error:

[InvalidOperationException: Can't select a disabled or unselectable menu item.]
   System.Web.UI.WebControls.Menu.SetSelectedItem(MenuItem node) +3099842
   myNewSite.SiteMaster.MasterPageMenu_MenuItemDataBound(Object sender, MenuEventArgs e) in C:\Users\Gary\Documents\Gary\Trusted\uShine\Site.Master.cs:94
   System.Web.UI.WebControls.Menu.OnMenuItemDataBound(MenuEventArgs e) +139
   System.Web.UI.WebControls.Menu.DataBindRecursive(MenuItem node, IHierarchicalEnumerable enumerable) +2950
   System.Web.UI.WebControls.Menu.DataBindRecursive(MenuItem node, IHierarchicalEnumerable enumerable) +3030
   System.Web.UI.WebControls.Menu.DataBindItem(MenuItem item) +428
   System.Web.UI.WebControls.Menu.PerformDataBinding() +142
   System.Web.UI.WebControls.HierarchicalDataBoundControl.PerformSelect() +141
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +134
   System.Web.UI.WebControls.Menu.EnsureDataBound() +28
   System.Web.UI.WebControls.Menu.OnPreRender(EventArgs e) +37
   System.Web.UI.Control.PreRenderRecursiveInternal() +132
   System.Web.UI.Control.PreRenderRecursiveInternal() +227
   System.Web.UI.Control.PreRenderRecursiveInternal() +227
   System.Web.UI.Control.PreRenderRecursiveInternal() +227
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4005
1 Answers

I have found a solution that works for me. I encountered this post which in and of itself did not directly help me:

ASP.net Menu CSS parent node selected style without URL

But I noticed that there were some indications that the item had to first be set selectable. I had seen this in other posts, but ignored it because when I searched I found I didn't explicitly set ANYTHING as un-selectable.

Nevertheless, when I add this line, everything works, all pages are reachable and all errors go away. Added lines marked as new below. . .

if (SiteMap.CurrentNode != null)
{
  if (e.Item.Text == SiteMap.CurrentNode.Title)
  {
     if (e.Item.Parent != null)
     {
        e.Item.Parent.Selectable = true; //New GCS20200310
        e.Item.Parent.Selected = true;
     }
     else
     {
         e.Item.Selectable = true;  //New GCS20200310
         e.Item.Selected = true;
     }
   }
}

What is interesting to me. . .and I still don't understand. . . is still WHY are these pages unselectable if I am not explicitly setting them as such. . .and again, only if they are nested sitenodes. Absent further evidence, it would seem that the act of nesting a sitenode makes it unselectable by default.

Related