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;
2:
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 asp.net SiteMap Development*/
7:
8: SiteMapNode node = SiteMap.RootNode;
9: SearchForDesc(node, title);
10: return menuDescription;
11: }
12:
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″ StaticSubMenuIndent=”0px”> <StaticItemTemplate> <%# Eval(“Text”) %> <br /> <small> <%# GetDescriptionByTitle(((MenuItem)Container.DataItem).Text) %> </small></StaticItemTemplate><DynamicItemTemplate> <%# Eval(“Text”) %> <br /> <small> <%# GetDescriptionByTitle(((MenuItem)Container.DataItem).Text) %> </small></DynamicItemTemplate></asp:Menu>
And here is the output:

Hope it helps!.



Leave a Reply