ASP.NET Slide Menu - Knowledge Base

« Back to Knowledge Base list

Bind to SiteMapDataSource

Q:

I want to use Slide Menu tool with dataSource as my web sitemap file.
How can I do it?

A:

Here is the code sample:

void Page_Load(Object o, EventArgs e)
 {
    if (!Page.IsPostBack)
    {
         // get DataSource View
         HierarchicalDataSourceView view = (SiteMapDataSource1 as IHierarchicalDataSource)
                                                             .GetHierarchicalView(SiteMapDataSource1.StartingNodeUrl);
         // get objects collection (level 0)
         IHierarchicalEnumerable enumerable = view.Select();
         // counter for the level 0
         int parentCount = 0;
         // go through the level 0
         foreach (object parentOnject in enumerable)
         {
             // get data on each collection member
             IHierarchyData parentHierarchyData = enumerable.GetHierarchyData(parentOnject);
             INavigateUIData parent = (INavigateUIData)parentHierarchyData;
             // info for menu item
             string parentText = parent.Name;
             string parentUrl = parent.NavigateUrl;
             // ID
             string parentId = SlideMenu1.ID + "_p_" + parentCount.ToString();
             // add parent to menu
             SlideMenu1.AddParent(parentId, parentText, parentUrl);
             // counter for the level 1
             int childCount = 0;
             // get objects collection (level 1)
             IHierarchicalEnumerable children = parentHierarchyData.GetChildren();
             // go through the level 1
             foreach (object childObject in children)
             {
                 // get data on each collection member
                 IHierarchyData childHierarchyData = children.GetHierarchyData(childObject);
                 INavigateUIData child = (INavigateUIData)childHierarchyData;
                 // info for menu item
                 string childText = child.Name;
                 string childUrl = child.NavigateUrl;
                 // ID
                 string childId = parentId + "_" + childCount.ToString();
                 // add child to menu
                 SlideMenu1.AddChild(childId, childText, childUrl);
                 childCount++;
             }
             parentCount++;
         }
    }
}
...
<asp:SiteMapDataSource ID="SiteMapDataSource1" ShowStartingNode="false" runat="server" />
<osm:SlideMenu Id="SlideMenu1" runat="server" StyleFolder = "styles/pro_5" />

Result sample: