Monday, February 21, 2011

How do I create a custom menu navigation in ASP.NET?

UPDATE

Here is my final working version:

<%              
Dim theUrl As String = Request.Url.Segments(Request.Url.Segments.Count - 1).ToLower
Dim oList As New List(Of String()), openTag As String = ""
oList.AddRange(sqlStuff.getNavPages())
For Each oItem As String() In oList
    If oItem(1) = theUrl Then
        openTag = String.Format("<li id={0}>", functions.addQuotes("current"))
    Else
        openTag = "<li>"
    End If
    Response.Write(String.Format("{0}<a href={1}>{2}</a></li>{3}", _
                                  openTag, _
                                  functions.addQuotes(oItem(1)), _
                                  oItem(0), _
                                  ControlChars.NewLine))
Next
%>

sqlStuff.getNavPages() is a function that queries my navigation database for pages and returns a list item with information about the page (URL, title, etc.)

From stackoverflow
  • Check out Site Maps.

    Anders : I have a site map made, but the control insists on displaying the pages underneath the default page. I want it to behave just like the navigation I have currently, which is a styled unordered list.
  • Your CSS will be something like this:

    ul
    {
    margin:0;
    padding:0;
    background:silver;
    }
    
    li
    {
    float:left;
    }
    
    a
    {
    display:block;
    padding:5px;
    color:gray;
    }
    
    li#current a
    {
    color:blue;
    }
    

    Obviously you'll want to either give your menu ul an id/class or wrap it in a container div so that you can target just that, rather than targeting all uls on the page. For example

    <ul id="MyMenu">...</ul>
    

    and

    ul#MyMenu
    {
    ...
    }
    
    ul#MyMenu li
    {
    ...
    }
    

    etc.

    Anders : I think you misunderstood my question, i have the css etc. I am unsure of how to make menu control look & feel like the list i have now
    Kon : Maybe you should rephrase or elaborate on the question, because neither of us understood it.
    Herb Caudill : This CSS will make your menu look like the image you provided. If that's not what you're asking for, please clarify.
  • SiteMaps will do the trick, if you take a look at CSSFriendly you can see how to alter the output to suit something a bit more "standard" that can be skinned easily using CSS.

  • Definitely take a look at the CSSFriendly Control Adapters that @Beepcake mentions. These will alter the rendering of the built-in ASP.NET Menu control (among others) to produce plain-old unordered lists. THen you can use your CSS to style them like you want.

0 comments:

Post a Comment