Main menu

Show Site Map Description on ASP.Net 2.0 Menu Control

Recently one of my Friend Asked me, how do I display both the title and the description of the SiteMapNode in the ASP.Net menu control ? well unfortunately by default ASP.Net Menu doesn’t support this.because the menu binds directly to a MenuItem object. and the MenuItem does expose a DataItem Property,but when it is added to the menu, it has no longer reference to the SiteMapNode which is used to populate it.

But we can write our own custom methods in your classes to Achieve this requirement, by looking at the SiteMapNode based on its title or URI. a sample is given below:

   1:  private string menuDescription=string.Empty;
   3:  protected string GetDescriptionByTitle(string title)
   4:  {
   5:  /*this assumes that there is only one node with this title.
   6:  which is a standard in SiteMap Development*/
   8:  SiteMapNode node = SiteMap.RootNode;
   9:  SearchForDesc(node, title);
  10:  return menuDescription;
  11:  }
  13:  private void SearchForDesc(SiteMapNode node,string title)
  14:  {
  15:  if (node.Title == title)
  16:  {
  17:  menuDescription = node.Description;
  18:  return;
  19:  }
  20:  else
  21:  {
  22:  foreach (SiteMapNode childnode in node.ChildNodes)
  23:  {
  24:  //search within childnodes
  25:  SearchForDesc(childnode, title);
  26:  }
  27:  }
  28:  }


And we can use the StaticItemTemplate and the DynamicItemTemplate to Display this information and use the GetDescriptionByTitle() method inside the templates as given below:

<asp:Menu ID="Menu1" runat="server" Height="24px"
Width="109px" DataSourceID="SiteMapDS1" StaticDisplayLevels="2"
<StaticItemTemplate>    <%# Eval("Text") %> <br />
<%# GetDescriptionByTitle(((MenuItem)Container.DataItem).Text) %>
<%# Eval("Text") %> <br />   <small>
<%# GetDescriptionByTitle(((MenuItem)Container.DataItem).Text) %>

And here is the output:


Hope it helps!.

Aneef Fashir (38 Posts)

Software Architect @ Assette, Sri Lanka