Home All Groups Group Topic Archive Search About

asp.net menu to show only parent, siblings and children as static menus

Author
3 May 2006 7:09 PM
Clint
Anyone have any idea how to have an asp.net menu show only the parent,
siblings and immediate children of the current node?

It seems like the ultimate way to navigate a large sitemap.  I've set
the StartFromCurrentNode="true" and StartingNodeOffset="-1" on the
SitemapDataSource and set the number of static menu levels to 3 on the
menu.  However, this ends up displaying the children of all the
siblings as well (which makes the menu much too large).

Ideas?

Thanks,
Clint

Author
4 May 2006 6:10 AM
Kelly Leahy
Clint,

I'm sure this isn't the best solution, but it's what I had time to come up
with.  It could definitely use a better test in the IsSamePage method, but I
couldn't come up with one at the moment....  Hopefully this will get you on
the right track.

    protected void Page_Load(object sender, EventArgs e)
    {
        // Find the menu item (if any) that corresponds to this page...
        MenuItem theItem = FindMenuItem(Menu1.Items);

        // Now, figure out which parent item it belongs to.
        MenuItem theParent = GetItemParent(theItem);

        // Finally, remove all children from the other top-level menu items.
        RemoveChildrenOfOthers(Menu1.Items, theParent);
    }

    private bool IsSamePage(string TargetUrl)
    {
        return Server.MapPath(Request.Url.LocalPath) ==
Server.MapPath(TargetUrl);
    }

    private MenuItem FindMenuItem(MenuItemCollection mis)
    {
        foreach (MenuItem mi in mis)
        {
            if (IsSamePage(mi.NavigateUrl))
                return mi;
            else
            {
                MenuItem mi2 = FindMenuItem(mi.ChildItems);
                if (mi2 != null) return mi2;
            }
        }
        return null;
    }

    private MenuItem GetItemParent(MenuItem mi)
    {
        if (mi.Parent == null)
            return mi;
        else
            return GetItemParent(mi.Parent);
    }

    private void RemoveChildrenOfOthers(MenuItemCollection all, MenuItem keep)
    {
        foreach (MenuItem mi in all)
        {
            if (mi != keep)
                mi.ChildItems.Clear();
        }
    }

Cheers,
Kelly

Show quoteHide quote
"Clint" wrote:

> Anyone have any idea how to have an asp.net menu show only the parent,
> siblings and immediate children of the current node?
>
> It seems like the ultimate way to navigate a large sitemap.  I've set
> the StartFromCurrentNode="true" and StartingNodeOffset="-1" on the
> SitemapDataSource and set the number of static menu levels to 3 on the
> menu.  However, this ends up displaying the children of all the
> siblings as well (which makes the menu much too large).
>
> Ideas?
>
> Thanks,
> Clint
>
>
Author
5 May 2006 12:39 PM
Clint
That's awesome Kelly.  Thank you!

I just need to figure out how to encapsulate that with a master page.

Any ideas where to start?  The best I can come up with is to put this
in a subclass of Page that all my pages inherit from.

Thanks again,
Clint