Better Control over the ASP.NET Sitemap UI

June 27th, 2008 by Joe

kick it on DotNetKicks.com

I’m a fan of using ASP.NET’s web.sitemap feature, but I’m less impressed with the level of control that you have (rather, you DON’T have) over the menu controls that consume the web.sitemap datasource (http://www.BuyLifetime.com uses three menu controls that consume the same sitemap from MS Commerce Server 2007, which look 90% like what we want them to).

Wouldn’t it be nice if you could just make it into a navigation list? We all know (or should know by now) that a navigation list (whether ordered or unordered) should be just that, a hierarchical (“nested”) list of pages in your site.

To do this we can use a Repeater control and use the web.sitemap datasource as the contents.

It’s easy enough to make a one-level-deep list, and not too much more difficult to do a two-level-deep list.

   1: <asp:Repeater visible="false" runat="server" ID="siteMapAsBulletedList" DataSourceID="SiteMapDataSource1">
   2:     <HeaderTemplate><ul></HeaderTemplate>
   3:     <ItemTemplate>
   4:         <li>
   5:             <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("Url") %>' Text='<%# Eval("Title") %>' />
   6:             <asp:Repeater runat="server" id="SecondLevel" DataSource='<%# ((SiteMapNode) Container.DataItem).ChildNodes %>'>
   7:                 <HeaderTemplate><ul></HeaderTemplate>
   8:                 <ItemTemplate>
   9:                     <li><asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("Url") %>' Text='<%# Eval("Title") %>'></asp:HyperLink></li>
  10:                 </ItemTemplate>
  11:                 <FooterTemplate></ul></FooterTemplate>
  12:             </asp:Repeater>    
  13:         </li>
  14:     </ItemTemplate>
  15:     <FooterTemplate></ul></FooterTemplate>
  16: </asp:Repeater>

Posted in Joe, Web Development

One Response

  1. matthew

    Another suggestion is to use CSS Friendly Adapters and a TreeView. It renders a nice, if css-class verbose, unordered list.

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

About Greener Living thru Technology

JoeLevi.com is the personal web log of Joe Levi -- an ASP.NET Web Developer by trade and by hobby. Joe's love of technology isn't just limited to the web, he's also interested in green and environmentally friendly technology and technological solutions. If it has to do with technology, improving the quality of life, geek humor, tech politics, self-defense, environmental stewardship, or anything related, you'll probably find it at www.JoeLevi.com.

Site statistics:
Average: ~1.3 P/V; Visits: ~3,000; Pageviews: ~3,600; Google PR: 4; TechnoratiAuthority: 17; Technorati Rank: 487,964





Watch the latest videos on YouTube.com