Menu Control - current node

A

AG

Using the new ASP.NET 2.0 menu control on a master page.
Is there any way to NOT have the current page be a live link and ideally
also have it appear in a different style?

Similar to the sitemappath control's RenderCurrentNodeAsLink property.

TIA
 
M

Mark Rae

Using the new ASP.NET 2.0 menu control on a master page.
Is there any way to NOT have the current page be a live link and ideally
also have it appear in a different style?

Similar to the sitemappath control's RenderCurrentNodeAsLink property.

I don't think it's possible just with the <asp:Menu>'s properties.

Dead easy in code, though...
 
W

Walter Wang [MSFT]

Hi AG,

If you are using DataBinding on the Menu control, you can handle it's
DataBound event and change current item:

protected void Menu1_DataBound(object sender, EventArgs e)
{
if (Menu1.SelectedItem.NavigateUrl == Request.Path)
{
Menu1.SelectedItem.Selectable = false;
}
}

Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

AG

Thanks for the reply Walter.
That did not work. It generated an exception 'object reference not set to
instance of object'.

However, you got me on the right track.
The following did work:

Protected Sub Menu1_MenuItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.MenuEventArgs) Handles mnuTop.MenuItemDataBound
If e.Item.NavigateUrl = Request.Path Then
e.Item.Selectable = False
End If
End Sub
 
W

Walter Wang [MSFT]

Hi AG,

Thanks for the correctness.

I forgot to check for null reference on Menu1.SelectedItem:

if (Menu1.SelectedItem != null && Menu1.SelectedItem.NavigateUrl ==
Request.Path)

Your code works, however, please note: MenuItemDataBound event will be
fired for every menu item in your menu control; while DataBound event will
be fired only once for the menu control. So I still recommend you do this
in the DataBound event.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

AG

Thanks Walter.

I understand what you are saying and it does work providing the user arrived
at the page by clicking on that particular menu.
If user arrived by any other means, like another menu control, sitemappath,
sitemap or any other link, then the menu link for the current page is still
active.
 
W

Walter Wang [MSFT]

When the navigation is made by another controls, we need to recursively
walk the Menu's items and determine which item's NavigateUrl is equal to
current URL:

protected void Menu1_DataBound(object sender, EventArgs e)
{
UnselectCurrentMenuItem(Menu1.Items);
}

private bool UnselectCurrentMenuItem(MenuItemCollection
menuItemCollection)
{
foreach (MenuItem mi in menuItemCollection)
{
if (mi.NavigateUrl == Request.Path)
{
mi.Selectable = false;
return true;
}
if (UnselectCurrentMenuItem(mi.ChildItems))
return true;
}
return false;
}

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

AG

Thanks Walter.

This method accomplishes the same end result as mine.
What are the advantages/disadvantages of each method?
 
W

Walter Wang [MSFT]

Hi AG,

After doing more research, actually your way handling in the ItemDataBound
event is the best one, because multiple MenuItem may have the same
NavigateUrl. My approach will not handle this situation correctly. Sorry
for my mistake.



Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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,777
Messages
2,569,604
Members
45,212
Latest member
BrennaCaba

Latest Threads

Top