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

C

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
 
K

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
 
C

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top