【问题标题】:ASP.NET Menu Control - Keeping parent item selectedASP.NET 菜单控件 - 保持选中父项
【发布时间】:2011-03-19 11:40:56
【问题描述】:

我在一个页面上有两个 ASP.NET 菜单控件。

顶部页面的标题(标签)中的一个,例如~/Default.aspx

还有一个在我的侧边栏中用于子页面,例如~/Products/SomeProduct.aspx

我正在使用selected CSS 类来确保选定的选项卡是不同的颜色。

适用于顶级页面,但如果我查看子页面,该选项卡未分配selected 的 CSS 类。

在查看子页面时,如何确保顶级菜单项的 CSS 类为 selected

【问题讨论】:

    标签: asp.net css menu


    【解决方案1】:

    我的菜单没有数据绑定,所以 MenuItemDataBound 永远不会被触发。以下是我在有子项和没有子项时突出显示父菜单项的解决方案:

    在菜单中添加一个 StaticSelectedStyle-xxx,例如 StaticSelectedStyle-BackColor="#757588"

    在 Page_Load 上调用此方法:

    protected void SetSelectedMenuItem()
            {
                string pageViewed = this.Page.AppRelativeVirtualPath;
    
                foreach (MenuItem item in Menu.Items)
                {
                    if (item.ChildItems.Count > 0)
                    {
                        if (pageViewed == item.Value)
                        {
                            item.Selected = true;
                        }
                        else
                        {
                            foreach (MenuItem childItem in item.ChildItems)
                            {
                                if (pageViewed == childItem.Value)
                                {
                                    if (childItem.Parent != null)
                                    {
                                        childItem.Parent.Selected = true;
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        if (pageViewed == item.Value)
                        {
                            item.Selected = true;
                        }
                    }
                }
            }
    

    【讨论】:

      【解决方案2】:

      在相关问题的帮助下弄清楚了:

      Set item.selected in ASP.NET Menu Control

      protected void Page_Load(object sender, EventArgs e)
      {
          MenuControl.MenuItemDataBound += new MenuEventHandler(MenuControl_MenuItemDataBound);
      }
      
      void MenuControl_MenuItemDataBound(object sender, MenuEventArgs e)
      {
          if (SiteMap.CurrentNode != null)
          {
              if (SiteMap.CurrentNode.ParentNode.Url == e.Item.NavigateUrl)
              {
                  e.Item.Selected = true;
              }
          }
       }
      

      【讨论】:

        猜你喜欢
        • 2016-12-15
        • 1970-01-01
        • 1970-01-01
        • 2010-09-19
        • 1970-01-01
        • 2011-08-15
        • 2010-10-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多