ASP.NET 2.0 and Web Standards – <UL> Menu

Danny Chen [MSFT] has posted some excellent sample code for those of us looking to use the new ASP.NET 2.0 menu control but that really hate the excessive HTML that this control renders. Danny’s post explains how to use a simple unordered list <UL> and the “template” capabilities of the menu control to create a very cool menu structure without tables. This isn’t the approach that I took but Danny’s code is very clean, easy to understand and flexible.


Now, why didn’t this get implemented in the first place instead of the (extremely ugly) nested table structure that the menu control currently emits? Maybe we should all ping Scott Guthrie and request this be included in the VS2005 SP1 bits.



Jeff

This entry was posted in ASP.NET. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

11 Responses to ASP.NET 2.0 and Web Standards – <UL> Menu

  1. Jeff Lynch says:

    Mark,

    I’ll post how I accomplished this (with source code) this evening. Feel free to contact me via my blog if you’d like the compete source code.


    Jeff

  2. Mark Petersen says:

    Sorry, I’m confused.

    Danny’s article shows you how to inherit from HierarchicalDataBoundControl to create a custom server control that would render a UL list. I can’t find any mention in his article or anywhere else on how to force the built-in menu server control to render a UL list.

    I do now understand how to use the security trimming now, because it’s based on the SiteMapProvider not on the actual control. All I have to do is pass in the “already trimmed” set of site map nodes to the control, I didn’t see the distinction before.

  3. Jeff Lynch says:

    Mark,

    No it’s very possible and not too difficult either. Danny Chen’s original post (see my original link here under “sample code”) shows one method and there are several others also. The Security Trimming will work with any of these techniques.

  4. Mark Petersen says:

    I noticed on:
    http://www.aspnetresources.com/blog/aspnet_for_designers.aspx

    you mentioned that you used the ASP.NET menu control to emit an unordered list. Are you saying that’s not actually possible now?

    I’d like to take advantage of the built in role feature of the menu control. Danny Chen’s article doesn’t seem to have anything to do with using the menu control at all — it uses a HierarchicalDataBoundControl. Am I missing something here? Is there some way to tie in the role feature into the custom control Danny explained?

  5. I realize you can, say, have a sub folder under your site root that can contain multiple sibling pages as well as other sub folders. What you can’t do is physically define a sitemap to match the structure of a site with more than a single page because you’ll get a schema error. I’m sure you can build a work-around but that seems like a hack to me. Maybe I’m just overlooking something though?

  6. Jeff Lynch says:

    Chris,

    Actually the Web.sitemap xml is pretty flexible and works just fine for sites with a flat hierarchy. Danny Chen [MSFT] has a great post at http://weblogs.asp.net/dannychen/archive/2005/10/20/428036.aspx you should look at also.


    Jeff

  7. Makes sense.

    One thing I did find is the default sitemap schema can only have a single parent siteMapNode element (generally this is the default page) which means it doesn’t work for a site that has multiple pages in the root. The default xml element is the siteMap element so it doesn’t make much sense to me as to why there has to be a single siteMapNode parent. Maybe it’s just me..

    On a side note, I was able to code up a server control (using Danny’s post as a guide) that renders a menu using an unordered list and can be bound to any control that implements the appropriate hierarchial interfaces. I have to make a few more additions and make sure it’s all clean and then I’ll be posting it as open source via my blog.

  8. Jeff Lynch says:

    Chris,

    It doesn’t and won’t as far as I can tell. I didn’t need more than a single level since I use this as a main site menu and use another in the left column as a sectional menu.


    Jeff

  9. And how does that work for you with menus that have more than a single level?

  10. Jeff Lynch says:

    Chris,

    No I haven’t run through Danny’s code yet as I used a simple templated approach using a repeater control as shown below. No embedded CSS is generated so I’m free to use my own to format this as required.

  11. Chris Wallace says:

Jeff,

Did you test Danny’s code? I haven’t tested it myself and while I definitely want my menus rendered as unordered lists, it’s the CSS that the default menu embeds in your page that causes it to be invalid and I suspect that Danny’s code still renders that. I, for one, wish the default hierarchial controls (menu, sitemappath, and treeview) all just rendered a straight unordered list stubbed for CSS so I could make them appear however I want via my style sheets. I’ve been working on this myself, now I guess I have to see what MS is going to do so I don’t wind up blowing a bunch of time.

-Chris

Leave a Reply