Highlighting current navigation link?

C

chris

Hi there,

I'd like ASP.NET (C#, preferably) to highlight the active link in the
navigation menu of my site. At present the links are anchors within a
..ascx user control, which appears on all the pages of the site.

What I'd like to do is compare the current URL to each of the link
HREFs and change the CSS class of the link that matches.

I've done this before in PHP, but I'm not sure of an elegant approach
in ASP.NET.

I'd be grateful if you could help out.

Thanks

Chris Beach
 
C

chris

Right -- I've solved my own problem.

..ASCX:

<div id="menu" class="menu" runat="server">
<a href="/travel_insurance_quote.aspx" runat="server">get a quote</a>
<a href="/summary_of_cover.aspx" runat="server">summary of cover</a>
<a href="/rate_compare.aspx" runat="server">rate comparisons</a>
<a href="/claims.aspx" runat="server">claims</a>
<a href="/policy_wording.aspx" runat="server">policy wording</a>
<a href="/who_are_we.aspx" runat="server">who are we?</a>
<a href="/contact_us.aspx" runat="server">contact us</a>
<a href="/faq.aspx" runat="server">faq</a>
<a href="/become_affiliate.aspx" runat="server">become an
affiliate</a>
</div>


..CSS:

..menu a.active { font-weight: bold }


..CS (the following is inserted in Page_Load):

foreach( System.Web.UI.Control control in menu.Controls )
{
HtmlAnchor link = control as HtmlAnchor;
if ( link == null ) continue;
if( Request.Path.IndexOf( link.HRef ) != -1 )
{
// Link is active (the href is a substring of the current address)
link.Attributes.Add( "class", "active" );
link.HRef = "";
}
}
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top