Highlighting current navigation link?

Discussion in 'ASP .Net' started by chris@chrisbeach.co.uk, Jul 28, 2005.

  1. Guest

    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
     
    , Jul 28, 2005
    #1
    1. Advertising

  2. Guest

    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 = "";
    }
    }
     
    , Jul 28, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Framework fan

    Curious link button navigation problem

    Framework fan, Mar 6, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    402
    Scott Allen
    Mar 8, 2004
  2. Kevin Spencer

    Re: Link Link Link DANGER WILL ROBINSON!!!

    Kevin Spencer, May 17, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    843
    Kevin Spencer
    May 17, 2005
  3. Leeza

    CSS Navigation Rel Link

    Leeza, Jul 28, 2003, in forum: HTML
    Replies:
    5
    Views:
    535
    E-Guy
    Aug 14, 2003
  4. Laurahn
    Replies:
    3
    Views:
    908
    Steven Cheng[MSFT]
    Feb 6, 2007
  5. jonefer

    Highlighting he current page

    jonefer, Feb 25, 2008, in forum: ASP .Net
    Replies:
    3
    Views:
    417
    Peter Bucher [MVP]
    Feb 27, 2008
Loading...

Share This Page