I have to create dynamic menus from the database. I used the following example which only shows the parent menus but doesn't show the child menu. http://www.dotnetfunda.com/articles/article1477-how-to-create-a-menu-in-aspnet-using-aspmenu-control.aspx
Code snippet I am using
<asp:Menu ID="Menu1" runat="server" Orientation="Horizontal" >
// I am convert ds to table for now.
DataTable table = dsMenu.Tables[0]; ;
DataView dvMenu = new DataView(table);
dvMenu.RowFilter = "PageInheritance is NULL";
foreach (DataRowView row in dvMenu)
{
MenuItem menuItem = new MenuItem(row["PageName"].ToString(), row["PageId"].ToString());
menuItem.NavigateUrl = row["PageURL"].ToString() + "?PageId=" + row["PageId"] + "&Language=" + sLangCode;
Menu1.Items.Add(menuItem);
AddChildItems(dvMenu.Table, menuItem);
}
//Function to look for child menu
private static void AddChildItems(DataTable table, MenuItem menuItem)
{
DataView viewItem = new DataView(table);
viewItem.RowFilter = "PageInheritance = " + menuItem.Value;
foreach (DataRowView childView in viewItem)
{
MenuItem childItem = new MenuItem(childView["PageName"].ToString(),
childView["PageId"].ToString());
childItem.NavigateUrl = childView["PageURL"].ToString();
menuItem.ChildItems.Add(childItem);
AddChildItems(table, childItem);
}
}
I am not sure what I am doing wrong. Based on my database it should show me child menus for row xyz. when AddChildItems function is called for the match child row it just skill the loop and doesn't show any thing from teh child rows.
OUTPUT with Current Code
HOME | Page2 | Page3 | Page4
It should show me sub menus for the Page2 based on my database. rather it only shows parent page links as above.