Highlighting current navigation link?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chris@chrisbeach.co.uk

    #1

    Highlighting current navigation link?

    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

  • chris@chrisbeach.co.uk

    #2
    Re: Highlighting current navigation link?

    Right -- I've solved my own problem.

    ..ASCX:

    <div id="menu" class="menu" runat="server">
    <a href="/travel_insuranc e_quote.aspx" runat="server"> get a quote</a>
    <a href="/summary_of_cove r.aspx" runat="server"> summary of cover</a>
    <a href="/rate_compare.as px" 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_affiliat e.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.C ontrol control in menu.Controls )
    {
    HtmlAnchor link = control as HtmlAnchor;
    if ( link == null ) continue;
    if( Request.Path.In dexOf( link.HRef ) != -1 )
    {
    // Link is active (the href is a substring of the current address)
    link.Attributes .Add( "class", "active" );
    link.HRef = "";
    }
    }

    Comment

    Working...